Object Types — TypeScript

An object type is written { name: string; age: number } after the colon, then the object { name: "Budi", age: 17 } after =. A property with the wrong type…

In JavaScript a typo in a property name isn't an error. user.nmae just gives undefined, the page shows "Hello, undefined", and you only find out when a user complains. TypeScript can catch a typo like that before the code runs, as long as it knows the object's shape.

You write that shape as an object type: which properties it must have, and the type of each one's value. Picture a sign-up form. Some fields are required, some are marked "optional", and some are locked once they're filled in. The object type is the blank form, the object is the filled-in form, and TypeScript checks that what was filled in fits.

Writing an object's shape

let user: { name: string; age: number } = { name: "Budi", age: 17 };

console.log(user.name); // Budi

Missing, wrong, or extra

Here's what TypeScript says about four objects under the type { name: string; age: number }, the same as in the Lab:

Object What TypeScript says
{ name: "Budi", age: 17 } no error
{ name: "Budi", age: "17" } Type 'string' is not assignable to type 'number'. ts(2322)
{ name: "Budi" } Property 'age' is missing in type '{ name: string; }' but required in type '{ name: string; age: number; }'. ts(2741)
{ name: "Budi", age: 17, hobby: "futsal" } Object literal may only specify known properties, and 'hobby' does not exist in type '{ name: string; age: number; }'. ts(2353)

Optional properties with ?

let user: { name: string; age?: number } = { name: "Budi" };

console.log(user.age); // undefined

readonly: locked once it's filled in

let user: { readonly name: string; age: number } = { name: "Budi", age: 17 };

user.age = 18;    // allowed
user.name = "Ani";

⚠️ Why is my object rejected, or let through?

TL;DR: An object type is written { name: string; age: number } after the colon, then the object { name: "Budi", age: 17 } after =. A property with the wrong type (ts(2322)), a missing one (ts(2741)), or an extra one (ts(2353)) is rejected. age?: number makes a property optional, and its type becomes number | undefined, so check it before use. readonly name: string stops name from being changed (ts(2540)). All of these rules are only checked before the code runs.

⚡ Write the object's shape

Write:

let user: { name: string; age: number } = { name: "Budi", age: 17 };

console.log(user.name); // Budi

Meaning: After the colon comes the object type, its properties separated by semicolons. After = comes the object itself, separated by commas. Because TypeScript knows the shape, a typo like user.nmae is rejected straight away.

⚡ Missing or extra, both are rejected

Guess first: { name: "Budi", age: 17, hobby: "futsal" } already has name and age. Does it pass?

Rejected: 'hobby' does not exist in type. An object written out directly can't have properties the type doesn't know, because a stray property is usually a typo.

Meaning: An object without age is rejected too, because age is required. age: "17" is rejected because it's text, and age wants a number.

⚡ Optional properties with ?

Write:

let user: { name: string; age?: number } = { name: "Budi" };

console.log(user.age); // undefined

Meaning: ? means the property may be there or not. The type of user.age becomes number | undefined, so TypeScript wants a check before it's used as a number.

What you will learn