"Hello, world!" // string42 // numbertrue // booleannullundefined
Type inference assumes the expected type of the variable throughout a program based on the data type of the value initially assigned to it at declaration. Type inference will log a complaint if the variable is later reassigned to a different type.
let first = 'Anders';first = 1337; // Type 'number' is not assignable to type 'string'
TypeScript knows the shape of an object—what member properties it does or doesn’t contain. TypeScript will log an error if the code attempts to access members of an object known not to exist. It may even suggest possible corrections.
let firstName = 'muriel!';console.log(firstName.toUppercase());// error: Property 'toUppercase' does not exist on type 'string'. Did you mean 'toUpperCase'?
When a variable is declared without being assigned an initial value, TypeScript considers it to be of type
any. A variable of this type can be reassigned without generating any error from TypeScript.
let first;first = 'Anders';first = 1337;
:) and the type (eg.
let first: string;first = 'Anders';// Error: Type 'number' is not assignable to type 'string'first = 1337;