1// simple type
2type Websites = 'www.google.com' | 'reddit.com';
3let mySite: Websites = 'www.google.com' //pass
4//or
5let mySite: Website = 'www.yahoo.com' //error
6// the above line will show error because Website type will only accept 2 strings either 'www.google.com' or 'reddit.com'.
7// another example.
8type Details = { id: number, name: string, age: number };
9let student: Details = { id: 803, name: 'Max', age: 13 }; // pass
10//or
11let student: Details = { id: 803, name: 'Max', age: 13, address: 'Delhi' } // error
12// the above line will show error because 'address' property is not assignable for Details type variables.
13//or
14let student: Details = { id: 803, name: 'Max', age: '13' }; // error
15// the above line will show error because string value can't be assign to the age value, only numbers.
16
17
18
11) Type guards narrow down the type of a variable within a conditional block.
22) Use the 'typeof' and 'instanceof operators to implement type guards in the
3 conditional blocks.
4
5//example(typeof)
6if (typeof a === 'number' && typeof b === 'number') {
7 return a + b;
8}
9
10(instanceof)
11if (job instanceof detail){
12 jobless = false ;
13}
14