Object type annotation inline in Typescript - Time & Space Complexity
When we write object type annotations inline in TypeScript, we want to know how this affects the time it takes for the program to run.
We ask: How does the program's work grow as the object size or usage grows?
Analyze the time complexity of the following code snippet.
function printUser(user: { name: string; age: number; active: boolean }) {
console.log(`Name: ${user.name}`);
console.log(`Age: ${user.age}`);
console.log(`Active: ${user.active}`);
}
const user = { name: "Alice", age: 30, active: true };
printUser(user);
This code defines a function with an inline object type annotation and prints its properties.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and printing each property of the object once.
- How many times: Exactly three times, once per property.
As the number of properties in the object grows, the number of operations remains constant (always 3 accesses).
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 |
| 10 | 3 |
| 100 | 3 |
Pattern observation: The work stays constant regardless of the number of properties since only specific named properties are accessed.
Time Complexity: O(1)
This means the time to run remains constant as the number of object properties increases.
[X] Wrong: "Using inline object type annotations makes the code slower because it adds extra work at runtime."
[OK] Correct: Type annotations are only for the developer and compiler; they do not affect the running speed of the program.
Understanding how your code scales with input size helps you write clear and efficient programs, a skill valued in many coding challenges and real projects.
"What if the function processed an array of such objects instead of just one? How would the time complexity change?"
Practice
let user: { name: string; age: number };Solution
Step 1: Understand inline object type annotation
Inline object type annotation specifies the properties and their types directly in the variable declaration.Step 2: Analyze the example
The example{ name: string; age: number }tells TypeScript thatusermust have a stringnameand a numberage.Final Answer:
It defines the shape of the object directly where it is used. -> Option AQuick Check:
Inline type annotation = direct object shape [OK]
- Thinking it creates a class
- Confusing with JSON conversion
- Assuming it declares a function
Solution
Step 1: Identify correct inline object type syntax
Inline object type annotation uses curly braces with property names and types, like{ id: number; title: string }.Step 2: Check each option
let data: { id: number; title: string } = { id: 1, title: 'Book' }; correctly uses this syntax and assigns a matching object. Options A, B, and D use incorrect syntax or types.Final Answer:
let data: { id: number; title: string } = { id: 1, title: 'Book' }; -> Option CQuick Check:
Curly braces with property types = correct syntax [OK]
- Using parentheses instead of braces
- Assigning types inside object literal
- Confusing arrays with objects
function printUser(user: { name: string; age: number }) {
console.log(`${user.name} is ${user.age} years old.`);
}
printUser({ name: 'Alice', age: 30 });Solution
Step 1: Understand the function parameter type
The function expects an object withnameas string andageas number.Step 2: Check the argument passed
The argument{ name: 'Alice', age: 30 }matches the expected type exactly.Final Answer:
Alice is 30 years old. -> Option AQuick Check:
Matching object type = correct output [OK]
- Passing missing or extra properties
- Using wrong types for properties
- Ignoring inline type annotation
let product: { id: number; name: string } = { id: 101, name: 'Pen', price: 5 };Solution
Step 1: Check the declared type
The type annotation allows onlyidandnameproperties.Step 2: Compare with assigned object
The object has an extra propertypricewhich is not declared in the type, causing an error.Final Answer:
Extra property 'price' not allowed in inline type annotation. -> Option BQuick Check:
Extra properties cause errors in inline types [OK]
- Adding extra properties not in type
- Ignoring TypeScript error messages
- Confusing semicolon placement
title and an optional number year. Which inline type annotation is correct?Solution
Step 1: Understand optional properties in inline types
Optional properties are marked with a question mark?after the property name.Step 2: Analyze each option
function showBook(book: { title: string; year?: number }) { console.log(book.title); } correctly marksyearas optional withyear?: number. Other options either maketitleoptional or use incorrect types.Final Answer:
function showBook(book: { title: string; year?: number }) { console.log(book.title); } -> Option DQuick Check:
Optional property uses ? after name [OK]
- Making required properties optional
- Using union types incorrectly
- Confusing optional with undefined type
Ready to prove it?
Pass the test and this pattern moves unknown
