Structural typing means that TypeScript checks if two types are compatible based on their shape or structure, not their names. If the properties and types match, they are considered compatible.
Click to reveal answer
intermediate
How does structural typing differ from nominal typing?
Structural typing compares the actual content (properties and methods) of types, while nominal typing compares types by their names or explicit declarations.
Click to reveal answer
beginner
Example: Are these two types compatible in TypeScript?
type A = { name: string };
type B = { name: string; age: number };
No, type A is not assignable to type B because it lacks the 'age' property. However, type B is assignable to type A because it has all required properties of A plus extra ones, which are allowed in structural typing.
Click to reveal answer
beginner
Why is structural typing useful in TypeScript?
It allows flexible and easy code reuse by focusing on what data looks like rather than what it is called. This helps when working with different objects that share the same shape.
Click to reveal answer
intermediate
Can two different classes be compatible in TypeScript under structural typing?
Yes, if their public properties and methods match in shape, TypeScript considers them compatible even if they come from different classes.
Click to reveal answer
What does structural typing check in TypeScript?
AThe names of types
BThe shape or structure of types
CThe file location of types
DThe order of declaration
✗ Incorrect
Structural typing compares the properties and their types, not the names or locations.
If two objects have the same properties and types, TypeScript considers them:
AIncompatible
BOnly compatible if declared with the same name
COnly compatible if from the same class
DCompatible
✗ Incorrect
TypeScript uses structural typing, so matching shapes mean compatibility.
Which typing system compares types by their names rather than structure?
AStructural typing
BDuck typing
CNominal typing
DDynamic typing
✗ Incorrect
Nominal typing checks type names explicitly.
In TypeScript, can an object with extra properties be assigned to a type with fewer properties?
AYes, if the required properties match
BNo, extra properties cause errors
COnly if explicitly cast
DOnly if using interfaces
✗ Incorrect
Extra properties are allowed if the required ones match the target type.
Structural typing helps TypeScript by:
AFocusing on data shape for compatibility
BMaking code less flexible
CRequiring explicit type names
DPreventing object reuse
✗ Incorrect
Structural typing focuses on the shape of data to allow flexible compatibility.
Explain in your own words what structural typing means in TypeScript and why it matters.
Think about how two objects with the same properties can be used interchangeably.
You got /3 concepts.
Describe a real-life example where structural typing helps when working with different objects.
Imagine two different forms that have the same fields.
You got /3 concepts.
Practice
(1/5)
1. What does structural typing in TypeScript primarily check for?
easy
A. If an object inherits from a specific parent class
B. If an object is declared with the exact same class name
C. If an object has the required properties regardless of its explicit type
D. If an object uses the same variable names as another object
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 C
Quick Check:
Structural typing = property shape check [OK]
Hint: Focus on properties, not class names or inheritance [OK]
Common Mistakes:
Confusing structural typing with class inheritance
Thinking variable names must match exactly
Assuming explicit type declarations are required
2. Which of the following TypeScript code snippets correctly demonstrates structural typing?
easy
A. interface Point { x: number; y: number; } const p: Point = { x: 1, y: 2 };
B. class Point { x: number; y: number; } const p: Point = new Point();
C. type Point = { x: string; y: string }; const p: Point = { x: 1, y: 2 };
D. interface Point { x: number; y: number; } const p = { a: 1, b: 2 };
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 A
Quick Check:
Matching properties and types = correct structural typing [OK]
Hint: Match property names and types exactly for structural typing [OK]
Common Mistakes:
Using wrong property names
Mismatching property types
Confusing class instances with structural typing
3. What will be the output of this TypeScript code?
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));
medium
A. Runtime error
B. Error: Object literal may only specify known properties
C. Hello, undefined
D. Hello, Alice
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 D
Quick Check:
Extra properties allowed if required ones exist [OK]
Hint: Extra properties don't block structural typing [OK]
Common Mistakes:
Thinking extra properties cause errors
Expecting undefined for missing properties
Confusing compile-time errors with runtime
4. Identify the error in this TypeScript code related to structural typing:
A. vehicle is missing the 'model' property required by Car
B. vehicle has extra properties not allowed by Car
C. printCar function is missing a return type
D. No error, code runs fine
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 A
Quick Check:
Missing required properties cause errors [OK]
Hint: Check all required properties exist in the object [OK]
Common Mistakes:
Ignoring missing properties
Thinking extra properties cause errors
Confusing missing return types with structural typing errors
5. Given these interfaces:
interface A { id: number; name: string; } interface B { id: number; name: string; age: number; }
Which assignment is valid under TypeScript's structural typing rules?
hard
A. const a: A = { id: 1, name: 'John' }; const b: B = a;
B. const b: B = { id: 2, name: 'Jane', age: 25 }; const a: A = b;
C. const a: A = { name: 'Joe', age: 30 };
D. const b: B = { id: 4, name: 'Jill' }; const a: A = b;
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 B
Quick Check:
More properties to fewer properties assignment is valid [OK]
Hint: Assign from bigger shape to smaller shape, not vice versa [OK]
Common Mistakes:
Assigning smaller shape to bigger shape without optional properties
Confusing optional properties as required
Adding extra properties to interface A directly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.