For :
abc:number|string;
Use the JavaScript operator typeof:
if (typeof abc === "number") {
// do something
}
TypeScript understands typeof 🌹
This is called a typeguard.
More
For classes you would use instanceof e.g.
class Foo {}
class Bar {}
// Later
if (fooOrBar instanceof Foo){
// TypeScript now knows that `fooOrBar` is `Foo`
}
There are also other type guards e.g. in etc https://basarat.gitbooks.io/typescript/content/docs/types/typeGuard.htmlhttps://basarat.gitbook.io/typescript/type-system/typeguard