Recall & Review
beginner
What is structural typing in TypeScript?
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?<br>
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?
✗ 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:
✗ Incorrect
TypeScript uses structural typing, so matching shapes mean compatibility.
Which typing system compares types by their names rather than structure?
✗ Incorrect
Nominal typing checks type names explicitly.
In TypeScript, can an object with extra properties be assigned to a type with fewer properties?
✗ Incorrect
Extra properties are allowed if the required ones match the target type.
Structural typing helps TypeScript by:
✗ 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.