Nested object types in Typescript - Time & Space Complexity
When working with nested object types in TypeScript, it's important to understand how accessing or processing these objects affects the time it takes for your program to run.
We want to know how the time needed grows as the nested objects get bigger or deeper.
Analyze the time complexity of the following code snippet.
interface User {
id: number;
profile: {
name: string;
address: {
street: string;
city: string;
};
};
}
function printUserCities(users: User[]) {
for (const user of users) {
console.log(user.profile.address.city);
}
}
This code loops through an array of users and prints the city from each user's nested address object.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the users array once.
- How many times: Once for each user in the array (n times).
As the number of users grows, the time to print all cities grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 city accesses and prints |
| 100 | 100 city accesses and prints |
| 1000 | 1000 city accesses and prints |
Pattern observation: Doubling the number of users doubles the work done.
Time Complexity: O(n)
This means the time grows directly with the number of users; more users mean more time, but only in a simple, straight line.
[X] Wrong: "Accessing nested objects makes the time grow much faster, like squared or worse."
[OK] Correct: Accessing nested properties is a fixed number of steps per user, so it does not multiply with input size. The main time cost is still just looping through the users.
Understanding how nested object access affects time helps you explain your code clearly and shows you can think about efficiency even in simple tasks.
"What if we added a nested loop inside to process each user's friends list? How would the time complexity change?"
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
