Typescript
November 14, 2026 4 min read

Object type

HS
HEMANT SINGH
Technical Writer & Educator

In TypeScript, an Object is a collection of properties (key-value pairs).


Each property has:

  1. A property name (key).
  2. A property value.
  3. A data type.

For example, in a Student Management System, a student can have a name, age, email, address, and admission status.

let student = {
name: "Aman",
age: 32,
email: "aman@example.com",
isActive: true
};

Here, student is an object.


1. What is an Object?

Consider these individual variables:

let name: string = "Aman";
let age: number = 32;
let email: string = "aman@example.com";
let isActive: boolean = true;

The problem is that all student information is stored in separate variables.

Instead, we can group related information into one object.

let student = {
name: "Aman",
age: 32,
email: "aman@example.com",
isActive: true
};

2. Object Type Annotation

In TypeScript, you can explicitly define the structure of an object.

Syntax:

let variableName: {
propertyName: type;
propertyName: type;
} = {
propertyName: value,
propertyName: value
};

Example:

let student: {
name: string;
age: number;
email: string;
isActive: boolean;
} = {
name: "Aman",
age: 32,
email: "aman@example.com",
isActive: true
};



3. Accessing Object Properties

There are two common ways to access object properties.

A. Dot Notation

let student = {
name: "Aman",
age: 32
};

console.log(student.name);
console.log(student.age);

Output:

Aman
32

B. Bracket Notation

let student = {
name: "Aman",
age: 32
};

console.log(student["name"]);
console.log(student["age"]);

Output:

Aman
32

5. Optional Properties

Sometimes an object doesn't need to contain every property.

For example, a student may or may not have a phone number.

TypeScript provides ? for optional properties.

let student: {
name: string;
age: number;
phone?: string;
} = {
name: "Aman",
age: 32
};

6. Readonly Properties

Sometimes you want a property to be assigned once and then prevent reassignment.

Use readonly.

let student: {
readonly id: number;
name: string;
} = {
id: 101,
name: "Hemant"
};

You can read the ID:

console.log(student.id);

But you cannot modify it:

student.id = 102; // Error


7. Nested Objects

An object can contain another object as a property.

For example, a student may have an address.

let student: {
name: string;
age: number;

address: {
city: string;
state: string;
pincode: number;
};

} = {
name: "Aman",
age: 32,

address: {
city: "Almora",
state: "Uttarakhand",
pincode: 263601
}
};

Access nested properties:

console.log(student.address.city);
console.log(student.address.state);


8. Object Inside an Array

You can store multiple objects inside an array.

For example, you want to store 3 students.

let students: {
id: number;
name: string;
age: number;
}[] = [

{
id: 1,
name: "Rahul",
age: 20
},

{
id: 2,
name: "Aman",
age: 22
},

{
id: 3,
name: "Priya",
age: 21
}

];

Now you can loop through students:

students.forEach((student) => {
console.log(student.name);
});

9. Object as a Function Parameter

This is extremely useful in real applications.

Suppose you want to create a function that accepts student information.

function createStudent(student: {
name: string;
age: number;
email: string;
}): void {

console.log(student.name);
console.log(student.age);
console.log(student.email);

}

Call the function:

createStudent({
name: "Aman",
age: 32,
email: "hemant@example.com"
});

10. Object as a Function Return Type

A function can also return an object.

function getStudent(): {
id: number;
name: string;
age: number;
} {

return {
id: 101,
name: "Aman",
age: 32
};

}

Use it:

let student = getStudent();

console.log(student.name);


11. Type Alias for Objects

Now imagine you have 20 functions that work with student objects.

Writing the same object structure repeatedly is not a good approach.

TypeScript provides Type Aliases.

type Student = {
id: number;
name: string;
age: number;
email: string;
};

Now use it anywhere:

let student: Student = {
id: 101,
name: "Aman",
age: 32,
email: "hemant@example.com"
};

12. Interface for Objects

TypeScript also provides interface to define object structures.

Example:

interface Student {
id: number;
name: string;
age: number;
}

Use it:

let student: Student = {
id: 101,
name: "Aman",
age: 32
};


14. Index Signatures

Sometimes you don't know the property names in advance.

For example, suppose you want to store student marks for different subjects.

let marks: {
[subject: string]: number;
} = {
maths: 90,
science: 85,
english: 80
};

Here:

[subject: string]: number;

Means:

  1. Property names can be strings.
  2. Property values must be numbers.

You can add:

marks.computer = 95;
marks.hindi = 88;

Another practical example: Dynamic settings

let settings: {
[key: string]: string | number | boolean;
} = {
theme: "dark",
fontSize: 16,
notifications: true
};

This is useful when working with dynamic object properties.



15. Object Destructuring

Destructuring allows you to extract properties from an object into separate variables.

let student = {
name: "Aman",
age: 32,
city: "Almora"
};

let { name, age, city } = student;

console.log(name);
console.log(age);
console.log(city);

16. Spread Operator with Objects

The spread operator (...) copies enumerable own properties from an object into another object.

A. Copying an object

let student = {
name: "Aman",
age: 32
};

let newStudent = { ...student };

B. Adding new properties

let student = {
name: "Aman",
age: 32
};

let updatedStudent = {
...student,
city: "Almora"
};

Result:

{
name: "Aman",
age: 32,
city: "Almora"
}

C. Updating existing properties

let student = {
name: "Aman",
age: 32
};

let updatedStudent = {
...student,
age: 33
};

The new age overrides the old value.