April 2018
Beginner
536 pages
13h 21m
English
Objects can be initialized using new Object(), Object.create(), or using the object literal notation, also known as initializer notation. An object initializer is a comma-delimited list of zero or more pairs of property names and values of an object, enclosed in curly braces:
let person = { name: "Remo", age: 28 };
The type inference system can automatically infer the type of object literals. The inferred type for the variable person declared in the preceding code snippet is { name: string, age: number }. Alternatively, we can explicitly declare the type of an object literal:
interface User { name: string; age: number;}let person: User = { name: "Remo", age: 28 }; // OK
It is also possible to declare optional properties: