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.
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
const car: { type: string, model: string, year: number } = {
type: "Toyota",
model: "Corolla",
year: 2009
};
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.
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
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 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.
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
const car: { type: string, mileage?: number } = { // no error
type: "Toyota"
};
car.mileage = 2000;
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; }'".
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.
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
const nameAgeMap: { [index: string]: number } = {};
nameAgeMap.Jack = 25; // no error
nameAgeMap.Mark = "Fifty"; // Error: Type 'string' is not assignable to type 'number'.