Object literals

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:

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.