TypeScript Object Type

Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.


An object is a collection of key-value pairs.

In TypeScript, an object type lets you define the structure of an object — what properties it should have, and what types those properties should be.

Here's a simple example of an object. You can read the rest of the tutorial to learn more.

Example

let user: { name: string; age: number } = {
    name: "Alice",
    age: 25
};

Here, user is an object with two properties: name (of string type) and age (of number type).


Defining Object Types

In TypeScript, you can define object types in three main ways:

1. Inline Object Types

An inline object type is when you define the object's structure directly when declaring the variable (like you saw in the intro example).

let user: { name: string; age: number } = {
    name: "Alice",
    age: 25
};

Here, { name: "Alice", age: 25 } is an object assigned to the user variable.

2. Using Type Alias

You can also create an object type using the type keyword.

type Person = {
    name: string;
    age: number;
};

let user: Person = { name: "Sara", age: 22 };

Here, type Person = { ... } creates a type alias named Person and defines what a valid Person object should look like: it must have two properties — name (a string) and age (a number).

Then, we assign the object { name: "Sara", age: 22 } to the user variable, which is typed as Person.

Note: Type alias is used when you want to reuse the object type in multiple places.

3. Using Interface

You can also define the structure of an object using the interface keyword.

interface Person {
    name: string;
    age: number;
}

let user: Person = { name: "Tom", age: 28 };

Here, Person is an interface that describes an object with two properties: name (of string type) and age (of number type).


Optional Properties (?)

You can use ? to mark a property as optional — meaning the object may include it, but it's not required to. For example,

type User = { name: string; age?: number };

type Person = { name: string; age?: number };

// Valid
let user1: Person = { name: "Alice", age: 25 };

// Valid even though age is missing
let user2: Person = { name: "Bob" };

In this example, age is optional. So, both user1 and user2 are valid — one includes age, the other doesn't.


Object Type as Function Parameter

You can also use object types for function parameters to define exactly what properties an object should have when it's passed into a function. For example,

function greet(user: { name: string; age: number }) {
    console.log(`Hello, ${user.name}. You are ${user.age} years old.`);
}

greet({ name: "Tyler", age: 43 });  

Here, the greet() function expects an object with two properties: name (string) and age (number).

Frequently Asked Questions

Object Destructuring with Defaults.

When passing an object as a parameter to a function, you can use destructuring to extract specific properties — and also provide default values in case those properties are missing.

function draw({ x = 0, y = 0 }: { x?: number; y?: number }) {
    console.log(x, y);
}

draw({ x: 10 });  // Output: 10 0
draw({});  // Output: 0 0

Here, the draw() function takes an object with optional x and y properties. Using destructuring with x = 0 and y = 0 provides default values.

If x or y is not passed, it will default to 0 instead of being undefined.


Readonly Property of an Object

The readonly keyword makes a property read-only, meaning you can't reassign it. For example,

type Home = {
    readonly resident: { name: string };
};

const house: Home = {
    resident: { name: "Alice" }
};

// Error: Cannot assign to 'resident' because it is a read-only property
house.resident = { name: "Bob" };

Here, the resident property is marked as readonly, so when we try to reassign the resident object, like this:

house.resident = { name: "Bob" };

TypeScript throws an error because readonly prevents reassigning the property.

However, since the inner name property is not marked as readonly, we can still modify it:

house.resident.name = "Charlie"; // This works

Define Object With Index Signature

Index signatures allow you to define objects with dynamic property names — meaning you don't need to know the property names in advance, but you can still specify the type of the values.

type Scores = { [subject: string]: number };

const mathScores: Scores = {
    math: 95,
    science: 88,
    english: 76
};

Here, [subject: string]: number means that the object can have any number of string keys, and each key must have a number value.


Frequently Asked Questions

Combining Object Types with & (Intersection Types)

You can combine multiple object types into one using & (Intersection Type). The resulting type must satisfy all the properties from the combined types. For example,

type User = { name: string };
type Admin = { role: string };

type AdminUser = User & Admin;

const admin: AdminUser = {
    name: "Alice",
    role: "Super Admin"
};

Here, AdminUser combines both User and Admin using &, so it must have both the name and role properties.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges