Lesson 29 of 60 – Object Type Annotations
48%

Object Type Annotations in TypeScript

In TypeScript, object type annotations allow us to describe the structure of an object. We can specify the names and types of its properties.

Note: Object type annotations help TypeScript check that objects contain the expected properties and correct data types.

1. What is an Object Type Annotation?

An object type annotation describes the properties that an object should contain.

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

Here, name must be a string and age must be a number.

2. Creating an Object with Type Annotation

We can declare the object type and assign values to it.

let student: {
    name: string;
    age: number;
} = {
    name: "Amit",
    age: 20
};

console.log(student);

3. String Property

An object property can be declared as a string.

let user: {
    name: string;
} = {
    name: "Rahul"
};

The name property must contain a string value.

4. Number Property

An object can contain number properties.

let student: {
    age: number;
} = {
    age: 21
};

The age property must contain a number.

5. Boolean Property

Boolean properties can contain true or false.

let student: {
    active: boolean;
} = {
    active: true
};

6. Multiple Object Properties

An object type can contain multiple properties.

let student: {
    name: string;
    age: number;
    active: boolean;
} = {
    name: "Amit",
    age: 20,
    active: true
};

7. Accessing Object Properties

Object properties can be accessed using dot notation.

let student: {
    name: string;
    age: number;
} = {
    name: "Amit",
    age: 20
};

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

8. Updating Object Properties

Properties can be changed as long as the new value matches the declared type.

let student: {
    name: string;
    age: number;
} = {
    name: "Amit",
    age: 20
};

student.name = "Rahul";
student.age = 21;

9. Object with an Array Property

An object property can also be an array.

let student: {
    name: string;
    marks: number[];
} = {
    name: "Amit",
    marks: [80, 75, 90]
};

console.log(student.marks);

10. Object with a String Array

A string array can be used as an object property.

let user: {
    name: string;
    skills: string[];
} = {
    name: "Rahul",
    skills: [
        "HTML",
        "CSS",
        "TypeScript"
    ]
};

11. Object with a Nested Object

An object can contain another object as a property.

let student: {
    name: string;
    address: {
        city: string;
        pin: number;
    };
} = {
    name: "Amit",
    address: {
        city: "Aurangabad",
        pin: 824101
    }
};

12. Accessing Nested Properties

Nested properties can be accessed using multiple dots.

console.log(
    student.address.city
);

console.log(
    student.address.pin
);

13. Object Property with Union Type

An object property can use a union type.

let user: {
    id: number | string;
    name: string;
} = {
    id: 101,
    name: "Amit"
};

user.id = "ST101";

The id property can contain either a number or a string.

14. Object Property with any

The any type allows a property to contain different types of values.

let data: {
    value: any;
} = {
    value: 100
};

data.value = "Hello";
data.value = true;

Although possible, using any should be limited when type safety is important.

15. Optional Object Property

A property can be made optional using ?.

let student: {
    name: string;
    age?: number;
} = {
    name: "Amit"
};

The age property does not have to be provided.

16. Required vs Optional Properties

Required properties must be present, while optional properties may be omitted.

let user: {
    name: string;
    email: string;
    phone?: string;
} = {
    name: "Amit",
    email: "amit@example.com"
};

Here, name and email are required, while phone is optional.

17. Object Type as a Function Parameter

Object type annotations can be used for function parameters.

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

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

showStudent({
    name: "Rahul",
    age: 21
});

18. Object Type as a Function Return Type

A function can return an object with a defined structure.

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

    return {
        name: "Amit",
        age: 20
    };
}

console.log(
    getStudent()
);

19. Object Type with a Function Property

An object can contain a function as one of its properties.

let user: {
    name: string;
    greet: () => void;
} = {
    name: "Amit",

    greet: () => {
        console.log("Hello");
    }
};

user.greet();

20. Object with a Function Parameter

An object property can contain a function that accepts parameters.

let calculator: {
    add: (
        a: number,
        b: number
    ) => number;
} = {

    add: (
        a,
        b
    ) => a + b

};

console.log(
    calculator.add(10, 20)
);

21. Object Type with Date

An object property can use built-in TypeScript types such as Date.

let student: {
    name: string;
    admissionDate: Date;
} = {
    name: "Amit",
    admissionDate: new Date()
};

console.log(
    student.admissionDate
);

22. Object Type in React

Object type annotations are useful when working with structured data in React.

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

const user: User = {
    name: "Amit",
    age: 20
};

This structure can later be used for component props.

23. Object Type in React Native

React Native applications commonly work with objects representing users, products, API data, and application state.

const user: {
    name: string;
    email: string;
} = {
    name: "Rahul",
    email: "rahul@example.com"
};

Type annotations help keep this data structure predictable.

24. Object Type with API Data

Object annotations can describe data received from an API.

const product: {
    id: number;
    title: string;
    price: number;
} = {
    id: 101,
    title: "Mobile Phone",
    price: 15000
};

console.log(
    product.title
);

25. Object Type with Array of Objects

We can define an array containing objects with a specific structure.

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

    {
        name: "Amit",
        age: 20
    },

    {
        name: "Rahul",
        age: 21
    }

];

console.log(
    students[0].name
);

26. Common Object Annotation Mistake

A common mistake is assigning a value that does not match the declared property type.

let student: {
    name: string;
    age: number;
} = {
    name: "Amit",
    age: 20
};

The following would be incorrect:

// Incorrect

// student.age = "Twenty";

The age property was declared as a number.

27. Object Type and Extra Properties

When an object is checked against an explicitly written object type, TypeScript can detect properties that are not part of the expected structure in certain contexts.

let student: {
    name: string;
    age: number;
} = {
    name: "Amit",
    age: 20
};

Keeping the object structure clear makes the code easier to understand and maintain.

28. When to Use Object Type Annotations

Object type annotations are useful when:

  • Creating structured application data.
  • Defining function parameters.
  • Defining function return values.
  • Working with API responses.
  • Creating arrays of objects.
  • Working with React props and state.
  • Working with React Native application data.

29. Complete Practical Example

Here is a complete student object example using multiple object properties.

let student: {
    id: number;
    name: string;
    age: number;
    course: string;
    marks: number[];
    active: boolean;
} = {

    id: 101,

    name: "Amit",

    age: 20,

    course: "TypeScript",

    marks: [
        80,
        75,
        90
    ],

    active: true
};

console.log(
    student.name
);

console.log(
    student.marks
);

console.log(
    student.active
);

30. Object Type Annotations Summary

Object type annotations define the expected structure of an object in TypeScript.

let student: {
    name: string;
    age: number;
    course: string;
} = {

    name: "Amit",

    age: 20,

    course: "TypeScript"

};

They make object-based code safer, clearer, and easier to maintain.

📌 Key Points

  • Object type annotations describe the structure of objects.
  • Each property can have its own type.
  • Objects can contain string, number, boolean, array, and other properties.
  • Nested objects can also be typed.
  • Properties can be optional using ?.
  • Object types can be used with function parameters and return values.
  • Objects can contain function properties.
  • Object annotations are useful for API data.
  • They are useful when working with React and React Native.
  • Object type annotations improve type safety and code readability.

🧠 Quick Quiz

Question: Which syntax correctly defines an object with a string name and number age?