Skip to main content

TypeScript Object Types

TypeScript has a special syntax for typing objects: you list each property name and its type inside curly braces.

If objects are new to you, read about JavaScript objects first.

Note: Object types like this can also be written separately and reused — see interfaces for more details.

Example

The type of car says it has a string type, a string model and a number year.

Prompt
Show the syntax for typing an object literal inline. Declare a constant car whose type is { type: string, model: string, year: number } and assign an object with type "Toyota", model "Corolla" and year 2009. Nothing is printed.
Output: TypeScript file and captured output

Type Inference

TypeScript can infer the types of an object's properties from their values.

Example

car.type is inferred as string, so assigning another string is fine but assigning a number is an error.

Prompt
Show TypeScript inferring property types from an object literal. Create car with only type: "Toyota", reassign car.type = "Ford" (no error), then assign the number 2 to car.type — that line must fail with "Type 'number' is not assignable to type 'string'".
Output: TypeScript file and captured output

Optional Properties

Optional properties are properties that do not have to be present when the object is created. Mark them with a ? after the name.

Without the ?, leaving a property out is an error, as the snippet shows.

Example with an optional property

mileage? makes the property optional, so the object can be created without it and given a value later.

Prompt
Show an optional property in an object type. Declare car with the type { type: string, mileage?: number }, initialize it with only type: "Toyota" (no error because mileage is optional), then set car.mileage = 2000. Nothing is printed.
Output: TypeScript file and captured output

Missing required property

Because mileage is required here, creating car without it is an error: "Property 'mileage' is missing in type '{ type: string;}' but required in type '{ type: string; mileage: number; }'".

script.ts
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;

Index Signatures

Index signatures type objects that do not have a fixed list of properties. They say what type the keys have and what type the values have.

Note: An index signature like this can also be written with a utility type such as Record<string, number>. Learn more in the TypeScript Utility Types chapter.

Example

Any string key is allowed on nameAgeMap, but every value must be a number, so "Fifty" is an error.

Prompt
Show an index signature for an object with arbitrary string keys and number values. Declare an empty constant nameAgeMap typed { [index: string]: number }, set nameAgeMap.Jack = 25 (no error), then set nameAgeMap.Mark = "Fifty" — that line must fail with "Type 'string' is not assignable to type 'number'".
Output: TypeScript file and captured output