TypeScript · Junior · Question 32 of 75
What is structural typing in TypeScript?
- ✓Correct answer: Type compatibility is determined by shape (the set of properties), not by name
- 2Types are compatible only if they're declared with the same name
- 3Typing where every object must inherit from a class
Explanation
If an object has all the properties a type requires, it fits - even if its type has a different name (duck typing). That's why Point2D and Vector2D with the same fields are interchangeable. Nominal behavior can be emulated with branded types.