Skip to Content
Learning TypeScript 2.x - Second Edition
book

Learning TypeScript 2.x - Second Edition

by Remo H. Jansen
April 2018
Beginner content levelBeginner
536 pages
13h 21m
English
Packt Publishing
Content preview from Learning TypeScript 2.x - Second Edition

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:

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering TypeScript - Fourth Edition

Mastering TypeScript - Fourth Edition

Nathan Rozentals
Learning TypeScript

Learning TypeScript

Josh Goldberg
TypeScript for Beginners

TypeScript for Beginners

Bharath Thippireddy

Publisher Resources

ISBN: 9781788391474Supplemental Content