Welcome back! I am Mihir, and in this lesson we will learn object types in TypeScript.
Objects are everywhere in JavaScript. TypeScript lets you describe the shape of those objects clearly.
Inline Object Types
You can type an object directly.
let user: {
name: string;
age: number;
isAdmin: boolean;
} = {
name: "Mihir",
age: 25,
isAdmin: true,
};TypeScript checks every property.
Invalid:
user.age = "twenty five";age must be a number.
Missing Properties
If a required property is missing, TypeScript warns.
let user: {
name: string;
email: string;
} = {
name: "Mihir",
};The email property is required.
Extra Properties
TypeScript also checks extra properties in object literals.
let user: {
name: string;
} = {
name: "Mihir",
age: 25,
};age is not part of the expected object shape.
Use Type Aliases for Reusable Objects
Inline object types become messy when repeated.
Better:
type User = {
name: string;
email: string;
age: number;
};
const user: User = {
name: "Mihir",
email: "mihir@example.com",
age: 25,
};Now User can be reused.
Object Types in Functions
type Product = {
title: string;
price: number;
};
function printProduct(product: Product): void {
console.log(`${product.title}: ${product.price}`);
}The function clearly expects a product-shaped object.
Nested Object Types
Objects can contain other objects.
type User = {
name: string;
address: {
city: string;
country: string;
};
};
const user: User = {
name: "Mihir",
address: {
city: "Mumbai",
country: "India",
},
};TypeScript checks nested properties too.
Arrays of Objects
type Course = {
id: number;
title: string;
};
const courses: Course[] = [
{ id: 1, title: "JavaScript" },
{ id: 2, title: "TypeScript" },
];Invalid:
courses.push({ id: "three", title: "React" });The id must be a number.
Object Methods
Objects can include methods.
type Counter = {
count: number;
increment: () => void;
};
const counter: Counter = {
count: 0,
increment() {
this.count += 1;
},
};The increment property is a function that returns void.
Optional Object Properties
Some properties may not exist.
type User = {
name: string;
phone?: string;
};The ? means phone is optional.
We will cover optional properties in detail in the next lesson.
Common Mistake: Using object Too Broadly
This is usually not very helpful:
let user: object = {
name: "Mihir",
};TypeScript only knows it is an object. It does not know about name.
Better:
type User = {
name: string;
};
let user: User = {
name: "Mihir",
};Quick Reference Summary
| Concept | Example |
|---|---|
| Inline object | { name: string; age: number } |
| Type alias | type User = { name: string } |
| Function parameter | function print(user: User) |
| Nested object | address: { city: string } |
| Array of objects | User[] |
| Object method | save: () => void |
Practice
Create a typed blog post:
type BlogPost = {
title: string;
slug: string;
views: number;
author: {
name: string;
email: string;
};
};
const post: BlogPost = {
title: "Object Types",
slug: "object-types",
views: 1000,
author: {
name: "Mihir",
email: "mihir@example.com",
},
};Try removing author.email and see what TypeScript says.
What You've Learned
You now understand:
- How to type objects
- How required properties work
- How TypeScript catches missing and extra properties
- Why type aliases are useful
- How object types work in functions
- How to type nested objects
- How to type arrays of objects
- How to type object methods
What's Next?
In the next lesson, we will learn Optional Properties in TypeScript.
We will handle object fields that may or may not exist.
Need Help?
- Have questions, confusion, or want to know more? Contact me
Object types are where TypeScript starts to feel practical, because most real data is object-shaped.