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;
}
More Related Contents:
- TypeScript: Object.keys return string[]
- Create a global variable in TypeScript
- Way to tell TypeScript compiler Array.prototype.filter removes certain types from an array?
- Difference between Variance, Covariance, Contravariance and Bivariance in TypeScript
- What does the “as const” mean in TypeScript and what is its use case?
- Angular2 – should private variables be accessible in the template?
- Typescript check object by type or interface at runtime with typeguards in 2020+
- How to use fetch in TypeScript
- Typescript property does not exist on union type
- Typescript primitive types: any difference between the types “number” and “Number” (is TSC case-insensitive)?
- Specify method’s type without converting the method to property
- Default parameters with generics in TypeScript
- How to watch and reload ts-node when TypeScript files change
- Angular 2 View will not update after variable change in subscribe
- Can’t import CSS/SCSS modules. TypeScript says “Cannot Find Module”
- Changing Property Name in Typescript Mapped Type
- Type for “every possible string value except …”
- How to combine known interface properties with a custom index signature?
- Unable to import svg files in typescript
- Typescript 2.0. “types” field in tsconfig.json
- tsc throws `TS2307: Cannot find module` for a local file
- Merge Two Interfaces
- Typescript: ReturnType of overloaded function
- What TypeScript version is Visual Studio Code using? How to update it?
- Why does TypeScript track mutation of function static properties?
- Array VS Type[] in Typescript
- How to use array.map with tuples in typescript?
- how to remove properties via mapped type in TypeScript
- What is a TypeScript Map file?
- Angular2 – root relative imports