Accessing properties in union types

You can write a custom type guard to achieve this:

function isAlarm(a: Alarm | Car): a is Alarm {
    // Some check to see if this is an Alarm
    return (<Object>a).hasOwnProperty('alarmText');
}


if (isAlarm(bar)) {
    bar.alarmText;
}

Leave a Comment