Recall & Review
beginner
What is duck typing in TypeScript?
Duck typing means that if an object has all the required properties and methods, it is considered to be of that type, regardless of its actual class or inheritance.Click to reveal answer
beginner
How does TypeScript check types with duck typing?
TypeScript checks if an object has the necessary shape (properties and methods) to match a type, not if it explicitly declares that type.
Click to reveal answer
beginner
Example: If an object has a 'name' string property and a 'speak()' method, can it be used where a type with those members is expected?
Yes, because the object matches the shape required by the type, TypeScript allows it even if the object does not explicitly implement that type.
Click to reveal answer
intermediate
Why is duck typing useful in TypeScript?
It allows flexible and easy code reuse by focusing on what an object can do rather than its exact type or class.
Click to reveal answer
intermediate
Can duck typing cause errors if an object has extra properties?
No, TypeScript allows extra properties as long as the required ones exist, but excess property checks apply in some cases like object literals.
Click to reveal answer
In TypeScript, what does duck typing check for?
✗ Incorrect
Duck typing checks if the object has the needed shape, ignoring inheritance or explicit declarations.
Which of these is true about duck typing in TypeScript?
✗ Incorrect
TypeScript allows objects with matching properties to be used without explicit interface implementation.
What happens if an object literal has extra properties not in the expected type?
✗ Incorrect
Excess property checks cause errors when object literals have extra properties not in the target type.
Why is duck typing called that way?
✗ Incorrect
The name comes from the saying: if it looks like a duck and quacks like a duck, it is treated as a duck.
Which TypeScript feature relies on duck typing?
✗ Incorrect
TypeScript uses structural typing, which is a form of duck typing based on object shape.
Explain the duck typing mental model in TypeScript and how it affects type checking.
Think about how TypeScript decides if an object fits a type.
You got /4 concepts.
Describe a situation where duck typing in TypeScript can cause an error and why.
Consider when you write an object literal with more properties than expected.
You got /4 concepts.