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?
AIf an object is explicitly declared with a type
BIf an object inherits from a specific class
CIf an object has the required properties and methods
DIf an object has no extra properties
✗ 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?
AAn object must explicitly implement an interface to be valid
BTypeScript does not allow extra properties in objects
CDuck typing requires inheritance
DAn object with matching properties can be used even without explicit implementation
✗ 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?
ATypeScript always allows it
BTypeScript gives an error due to excess property checks
CTypeScript ignores extra properties silently
DThe object is automatically cast to 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?
ABecause if it looks like a duck and quacks like a duck, it is treated as a duck
BBecause it uses ducks in examples
CBecause it requires inheritance from a Duck class
DBecause it only works with animals
✗ 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?
AStructural typing
BNominal typing
CClass inheritance
DType casting
✗ 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.
Practice
(1/5)
1. What does duck typing mean in TypeScript?
easy
A. Only objects created from the same class are compatible.
B. If an object has the required properties or methods, it is accepted regardless of its explicit type.
C. TypeScript requires explicit class inheritance to match types.
D. TypeScript checks the runtime type of objects strictly.
Solution
Step 1: Understand duck typing concept
Duck typing means an object is accepted if it has the needed properties or methods, regardless of its declared type.
Step 2: Compare with other options
The other options describe strict or class-based typing, which is not how TypeScript works.
Final Answer:
If an object has the required properties or methods, it is accepted regardless of its explicit type. -> Option B
Quick Check:
Duck typing = structural compatibility [OK]
Hint: Focus on properties, not explicit types, for duck typing [OK]
Common Mistakes:
Thinking TypeScript requires class inheritance
Confusing runtime type checks with compile-time checks
Assuming exact type names must match
2. Which of the following is a correct TypeScript syntax to define a type that matches any object with a name string property and a quack() method returning void?
easy
A. type Duck = { name: string; quack: () => void; };
B. interface Duck { name: string; quack: void; }
C. type Duck = { name: string; quack(): string; };
The name property must be a string, and quack must be a method returning void.
Step 2: Validate each option
type Duck = { name: string; quack: () => void; }; correctly defines quack as a function returning void. The other options have incorrect method signatures or return types.
Final Answer:
type Duck = { name: string; quack: () => void; }; -> Option A
Which call to testDuck will NOT cause a TypeScript error?
hard
A. testDuck(c);
B. testDuck(b);
C. testDuck(a);
D. testDuck(d);
Solution
Step 1: Check each object's quack property type
a.quack is a function returning void (correct). b.quack is a string (wrong). c.quack is a function returning string (wrong return type). d.quack is missing (wrong).
Step 2: Determine which matches Duck interface
Only object a matches the Duck interface exactly.
Final Answer:
testDuck(a); -> Option C
Quick Check:
Correct method signature only in a [OK]
Hint: Method must be function returning void to match Duck [OK]
Common Mistakes:
Accepting string or wrong return type as method
Ignoring missing methods
Confusing method return types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.