Duck typing focuses on what an object can do, not what it is called.
It helps write flexible and reusable code without strict class hierarchies.
TypeScript uses this to check types by structure, not by name.
Summary
Duck typing means checking if an object has needed properties or methods.
TypeScript uses structural typing to allow flexible code.
If an object looks like a duck and quacks like a duck, TypeScript treats it as a duck.
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.