Nested object types help you describe objects inside other objects clearly. This makes your code easier to understand and safer to use.
Nested object types in Typescript
type Outer = {
inner: {
property1: type1;
property2: type2;
};
};You define an object type inside another by writing one object type inside the other.
Each nested object can have its own properties and types.
type User = {
name: string;
address: {
street: string;
city: string;
};
};type Product = {
id: number;
details: {
price: number;
manufacturer: string;
};
};type BlogPost = {
title: string;
author: {
name: string;
email: string;
};
};This program defines a Person type with a nested contact object. It creates a person and prints the name, email, and phone.
type Person = {
name: string;
contact: {
email: string;
phone: string;
};
};
const person: Person = {
name: "Alice",
contact: {
email: "alice@example.com",
phone: "123-456-7890"
}
};
console.log(`Name: ${person.name}`);
console.log(`Email: ${person.contact.email}`);
console.log(`Phone: ${person.contact.phone}`);You can nest objects as deep as you want, but keep it simple to avoid confusion.
Using nested types helps catch mistakes early because TypeScript checks the structure.
Nested object types describe objects inside other objects.
They make your data structure clear and safe.
Use them when your data naturally groups inside other data.
Practice
What does a nested object type in TypeScript represent?
For example, { user: { name: string; age: number } }
Solution
Step 1: Understand object properties
In TypeScript, an object type can have properties that are themselves objects.Step 2: Analyze the example type
The example{ user: { name: string; age: number } }shows a propertyuserwhich is an object with its own properties.Final Answer:
An object that contains another object as a property -> Option AQuick Check:
Nested object = object inside object [OK]
- Thinking nested means array
- Confusing functions with objects
- Assuming primitive types can be nested objects
Which of the following is the correct syntax to define a nested object type in TypeScript?
type Profile = {
user: {
id: number;
name: string;
}
};Solution
Step 1: Check object type syntax
TypeScript uses curly braces to define object types with properties and their types.Step 2: Match nested object syntax
type Profile = { user: { id: number; name: string } } correctly nests an object type inside theuserproperty withidandname.Final Answer:
type Profile = { user: { id: number; name: string } } -> Option DQuick Check:
Nested object uses braces, not parentheses or arrays [OK]
- Using parentheses instead of braces
- Using arrays instead of objects
- Separating properties incorrectly
What will be the output of the following TypeScript code?
type Data = {
info: {
count: number;
label: string;
}
};
const data: Data = {
info: { count: 5, label: 'items' }
};
console.log(data.info.label);Solution
Step 1: Understand the nested object structure
The variabledatahas a propertyinfowhich is an object withcountandlabel.Step 2: Access the nested property
The code logsdata.info.label, which is the string "items".Final Answer:
"items" -> Option BQuick Check:
Access nested property label = "items" [OK]
- Confusing property names
- Trying to access without the nested path
- Expecting number instead of string
Identify the error in this TypeScript nested object type definition:
type Config = {
settings: {
theme: string;
notifications: boolean
}
};
const config: Config = {
settings: {
theme: 'dark',
notifications: 'yes'
}
};Solution
Step 1: Check type definition for 'notifications'
The type saysnotifications: boolean, so it must be true or false.Step 2: Check assigned value in object
The value assigned is the string 'yes', which is not a boolean, causing a type error.Final Answer:
The property 'notifications' should be a boolean, not a string -> Option CQuick Check:
Type mismatch boolean vs string causes error [OK]
- Ignoring type mismatch errors
- Assuming strings can replace booleans
- Missing semicolons but they don't cause error here
Given this nested object type, how can you define a type for a user profile that includes an address object with street and city?
type UserProfile = {
id: number;
name: string;
address: {
street: string;
city: string;
}
};Which of the following correctly creates a variable profile of type UserProfile?
Solution
Step 1: Review the UserProfile type
UserProfile requiresid,name, and anaddressobject withstreetandcityas strings.Step 2: Check each option against the type
const profile: UserProfile = { id: 1, name: 'Alice', address: { street: 'Main St', city: 'Wonderland' } }; provides all required properties with correct types. Options B, C, and D miss or mis-type theaddressproperty.Final Answer:
const profile: UserProfile = { id: 1, name: 'Alice', address: { street: 'Main St', city: 'Wonderland' } }; -> Option AQuick Check:
Complete nested object matches type [OK]
- Assigning string instead of nested object
- Omitting nested properties
- Leaving out required properties
Ready to prove it?
Pass the test and this pattern moves unknown
