typescript typeof code example
Example 1: typescript check type
mySprite instanceof Sprite;
Example 2: typescript get type
if (typeof abc === "number") {
}
Example 3: use type as value typescript
Typescript interfaces aren''t being compiled into the js output, and you can not use them at runtime
Example 4: custom types in typescript
type Websites = 'www.google.com' | 'reddit.com';
let mySite: Websites = 'www.google.com'
let mySite: Website = 'www.yahoo.com'
type Details = { id: number, name: string, age: number };
let student: Details = { id: 803, name: 'Max', age: 13 };
let student: Details = { id: 803, name: 'Max', age: 13, address: 'Delhi' }
let student: Details = { id: 803, name: 'Max', age: '13' };
Example 5: typescript object type
let endedCoord: {x: number, y: number} = {
x: -1,
y: -1,
}
Example 6: type gurad
1) Type guards narrow down the type of a variable within a conditional block.
2) Use the 'typeof' and 'instanceof operators to implement type guards in the
conditional blocks.
if (typeof a === 'number' && typeof b === 'number') {
return a + b;
}
(instanceof)
if (job instanceof detail){
jobless = false ;
}