let a:number;let b:boolean;let c:string;let d:any;let e:number[] = [1,2,3];let f:any[] = [1,true,'a'];enumColor { Red =0, Green =1, Blue =2};let backgroundColor =Color.Red;
By default the type is any. To use as a type, (message as string) purely a way to tell Typescript compiler what it is(and intelligent IDEs)
When you define and assign value, type is autoinferred
classCar {printCar= () => {console.log("this is my car") }};interfaceNewCarextendsCar { name:string;};classNewestCarimplementsNewCar { name:"Car";constructor(engine:string) {this.name = name }printCar= () => {console.log("this is my car") }};
Use unknown as it blocks fields unless narrowed, as any lets you do untypesafe things
Never
Never is the empty set and will never have a value
functiontimeout(ms:number):Promise<never> {returnnewPromise((_, reject) =>setTimeout(() =>reject(newError("Timeout elapsed")), ms) )}//useful because will only be return type of fetchStock as T | never = Tconststock=awaitPromise.race([fetchStock(tickerSymbol),timeout(3000)])
This works because conditional types distribute over type unions. Given any type of the form T extends U ? X : Y when a union type is substituted for T the type expands to distribute the condition to each branch of that union type: