What structural typing means in Typescript - Time & Space Complexity
We want to understand how the cost of checking types grows when using structural typing in TypeScript.
How does the program decide if two types match as the size of the types grows?
Analyze the time complexity of comparing two object types structurally.
function areTypesCompatible(a: object, b: object): boolean {
for (const key in a) {
if (!(key in b)) return false;
}
return true;
}
This code checks if all keys in object a exist in object b, a simple structural type check.
Look at what repeats when comparing types.
- Primary operation: Looping over all keys in object
a. - How many times: Once for each key in
a.
As the number of keys in a grows, the number of checks grows the same way.
| Input Size (n keys in a) | Approx. Operations |
|---|---|
| 10 | About 10 key checks |
| 100 | About 100 key checks |
| 1000 | About 1000 key checks |
Pattern observation: The work grows directly with the number of keys in the first object.
Time Complexity: O(n)
This means the time to check grows in a straight line with the number of keys we compare.
[X] Wrong: "Checking types is instant no matter how big the objects are."
[OK] Correct: The program must look at each key to be sure, so more keys mean more work.
Understanding how structural typing checks scale helps you explain how TypeScript works behind the scenes and shows you think about efficiency in real code.
"What if we also checked that the values for each key have compatible types? How would the time complexity change?"
Practice
structural typing in TypeScript primarily check for?Solution
Step 1: Understand structural typing concept
Structural typing checks the shape of an object, meaning its properties and their types.Step 2: Compare with other typing methods
Unlike nominal typing, it does not require the same class or inheritance, only matching properties.Final Answer:
If an object has the required properties regardless of its explicit type -> Option CQuick Check:
Structural typing = property shape check [OK]
- Confusing structural typing with class inheritance
- Thinking variable names must match exactly
- Assuming explicit type declarations are required
Solution
Step 1: Check interface and object matching
interface Point { x: number; y: number; } const p: Point = { x: 1, y: 2 }; defines an interface with x and y as numbers and assigns an object with matching properties.Step 2: Verify other options
class Point { x: number; y: number; } const p: Point = new Point(); uses class instantiation, not structural typing demonstration. type Point = { x: string; y: string }; const p: Point = { x: 1, y: 2 }; mismatches types. interface Point { x: number; y: number; } const p = { a: 1, b: 2 }; has different property names.Final Answer:
interface Point { x: number; y: number; } const p: Point = { x: 1, y: 2 }; -> Option AQuick Check:
Matching properties and types = correct structural typing [OK]
- Using wrong property names
- Mismatching property types
- Confusing class instances with structural typing
interface User { name: string; age: number; }
const user = { name: 'Alice', age: 30, city: 'NY' };
function greet(u: User) { return `Hello, ${u.name}`; }
console.log(greet(user));Solution
Step 1: Understand structural typing with extra properties
The object has extra property 'city', but TypeScript allows it because it has required properties 'name' and 'age'.Step 2: Check function usage
The function greet uses only 'name', which exists, so it returns 'Hello, Alice'.Final Answer:
Hello, Alice -> Option DQuick Check:
Extra properties allowed if required ones exist [OK]
- Thinking extra properties cause errors
- Expecting undefined for missing properties
- Confusing compile-time errors with runtime
interface Car { make: string; model: string; }
const vehicle = { make: 'Toyota' };
function printCar(c: Car) { console.log(c.make + ' ' + c.model); }
printCar(vehicle);Solution
Step 1: Check object properties against interface
vehicle only has 'make', but Car requires both 'make' and 'model'.Step 2: Understand structural typing requirements
All required properties must be present; missing 'model' causes error.Final Answer:
vehicle is missing the 'model' property required by Car -> Option AQuick Check:
Missing required properties cause errors [OK]
- Ignoring missing properties
- Thinking extra properties cause errors
- Confusing missing return types with structural typing errors
interface A { id: number; name: string; }
interface B { id: number; name: string; age: number; }Which assignment is valid under TypeScript's structural typing rules?
Solution
Step 1: Analyze required property in interface B
B has a required 'age' property.Step 2: Check assignment compatibility
const a: A = { id: 1, name: 'John' }; const b: B = a; fails: 'a' lacks 'age' required by B. const b: B = { id: 2, name: 'Jane', age: 25 }; const a: A = b; succeeds: 'b' matches B, extra 'age' OK for A. const a: A = { name: 'Joe', age: 30 }; misses required 'id' for A. const b: B = { id: 4, name: 'Jill' }; const a: A = b; misses 'age' required by B.Final Answer:
const b: B = { id: 2, name: 'Jane', age: 25 }; const a: A = b; -> Option BQuick Check:
More properties to fewer properties assignment is valid [OK]
- Assigning smaller shape to bigger shape without optional properties
- Confusing optional properties as required
- Adding extra properties to interface A directly
Ready to prove it?
Pass the test and this pattern moves unknown
