bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/TypeScript/TypeScript Core
TypeScript•TypeScript Core

TypeScript Object Types

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind TypeScript Object Types?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ car: { type: string, model: string, year: number } = {
3Order

Put the learning moves in the order that makes the concept easiest to apply.

TypeScript has a specific syntax for typing objects.
Optional Properties
TypeScript Object Types

TypeScript has a specific syntax for typing objects.

Read more about objects in our JavaScript Objects chapter .

Example

const car: { type: string, model: string, year: number } = {
  type: "Toyota", model: "Corolla", year: 2009
};

Object types like this can also be written separately, and even be reused, look at interfaces for more details.

Type Inference

TypeScript can infer the types of properties based on their values.

Example

const car = {
  type: "Toyota", };
car.type = "Ford"; // no error
car.type = 2; // Error: Type 'number' is not assignable to type 'string'.

Optional Properties

Optional properties are properties that don't have to be defined in the object definition.

Example without an optional property

const car: { type: string, mileage: number } = { // Error: Property 'mileage' is missing in type '{ type: string; }' but required in type '{ type: string; mileage: number; }'.
 type: "Toyota",
};
car.mileage = 2000;

Example with an optional property

const car: { type: string, mileage?: number } = { // no error
  type: "Toyota"
};
car.mileage = 2000;

Index Signatures

Index signatures can be used for objects without a defined list of properties.

Example

const nameAgeMap: { [index: string]: number } = {};
nameAgeMap.Jack = 25; // no error
nameAgeMap.Mark = "Fifty"; // Error: Type 'string' is not assignable to type 'number'.

Index signatures like this one can also be expressed with utility types like Record<string, number> .

Learn more about utility types like this in our TypeScript Utility Types chapter.

Previous

TypeScript Tuples

Next

TypeScript Enums