"Hello, world!" // string 42 // number true // boolean null undefined
TypeScript Type Inference
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'
The Shape of an Object
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;
TypeScript Supports Type Annotations
:) and the type (eg.
let first: string; first = 'Anders'; // Error: Type 'number' is not assignable to type 'string' first = 1337;