Bird
Raised Fist0
Typescriptprogramming~20 mins

Duck typing mental model in TypeScript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Duck Typing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this duck typing example?

Consider this TypeScript code using duck typing. What will be printed?

Typescript
interface Quack {
  quack(): void;
}

function makeItQuack(duck: Quack) {
  duck.quack();
}

const notADuck = {
  quack: () => console.log('I am quacking!')
};

makeItQuack(notADuck);
Aundefined
BI am quacking!
CType error: notADuck is not of type Quack
DRuntime error: quack is not a function
Attempts:
2 left
💡 Hint

Duck typing means the object just needs to have the right shape, not the exact type.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes duck typing in TypeScript?

Choose the best description of duck typing in TypeScript.

ATypeScript checks only the names and types of properties and methods, not the explicit type names.
BTypeScript requires explicit inheritance to match types.
CTypeScript uses runtime type checking to enforce types.
DTypeScript only allows objects to match types if they have the same prototype.
Attempts:
2 left
💡 Hint

Think about how TypeScript compares object shapes rather than names.

🔧 Debug
advanced
2:30remaining
Why does this code cause a TypeScript error?

Look at this code snippet. Why does TypeScript report an error?

Typescript
interface Flyer {
  fly(): void;
}

const bird = {
  fly: () => console.log('Flying'),
  swim: () => console.log('Swimming')
};

function makeFly(f: Flyer) {
  f.fly();
}

makeFly({swim: () => console.log('Swimming')});
AThe object passed to <code>makeFly</code> lacks the required <code>fly</code> method.
BThe object has extra properties which are not allowed.
CThe interface <code>Flyer</code> is not implemented explicitly.
DThe <code>fly</code> method is private and cannot be accessed.
Attempts:
2 left
💡 Hint

Check if the object passed has the required method.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a duck-typed object matching this interface?

Given this interface, which object correctly matches it by duck typing?

interface Runner {
  run(speed: number): string;
}
A{ run(speed: string) { return 'Fast'; } }
B{ run: (speed) => speed + ' km/h' }
C{ run: (speed: number) => `Running at ${speed} km/h` }
D{ run: () => 'Running' }
Attempts:
2 left
💡 Hint

Check the method signature matches the interface exactly.

🚀 Application
expert
3:00remaining
What is the value of result after this duck typing code runs?

Analyze the code and find the value of result.

Typescript
interface Speaker {
  speak(): string;
}

const obj1 = { speak: () => 'Hello' };
const obj2 = { speak: () => 'World' };

function combineSpeakers(a: Speaker, b: Speaker): string {
  return a.speak() + ' ' + b.speak();
}

const result = combineSpeakers(obj1, obj2);
A"World Hello"
BType error: obj1 or obj2 does not match Speaker
C"HelloHello"
D"Hello World"
Attempts:
2 left
💡 Hint

Both objects have a speak method returning strings.

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

  1. 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.
  2. Step 2: Compare with other options

    The other options describe strict or class-based typing, which is not how TypeScript works.
  3. Final Answer:

    If an object has the required properties or methods, it is accepted regardless of its explicit type. -> Option B
  4. 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; };
D. interface Duck { name: string; quack: () => string; }

Solution

  1. Step 1: Check property and method types

    The name property must be a string, and quack must be a method returning void.
  2. 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.
  3. Final Answer:

    type Duck = { name: string; quack: () => void; }; -> Option A
  4. Quick Check:

    Correct method signature = type Duck = { name: string; quack: () => void; }; [OK]
Hint: Method type must be a function signature with correct return type [OK]
Common Mistakes:
  • Using method return type as property type
  • Confusing void and string return types
  • Incorrect interface method syntax
3. What will be the output of this TypeScript code?
interface Duck { name: string; quack: () => void; }

function makeItQuack(duck: Duck) {
  duck.quack();
}

const obj = { name: 'Daffy', quack: () => console.log('Quack!'), swim: () => console.log('Swim') };
makeItQuack(obj);
medium
A. No output
B. Swim
C. Error: obj does not match Duck interface
D. Quack!

Solution

  1. Step 1: Check if obj matches Duck interface

    obj has name (string) and quack (function returning void), so it matches by duck typing.
  2. Step 2: Call makeItQuack with obj

    Calling duck.quack() runs console.log('Quack!'), printing "Quack!".
  3. Final Answer:

    Quack! -> Option D
  4. Quick Check:

    Duck typing allows extra properties, output = Quack! [OK]
Hint: Extra properties don't block duck typing; focus on required ones [OK]
Common Mistakes:
  • Thinking extra properties cause errors
  • Expecting 'Swim' output instead of 'Quack!'
  • Assuming runtime type errors
4. Identify the error in this TypeScript code using duck typing:
interface Duck { name: string; quack: () => void; }

const notADuck = { name: 'Donald' };

function makeItQuack(duck: Duck) {
  duck.quack();
}

makeItQuack(notADuck);
medium
A. notADuck is missing the quack method required by Duck interface.
B. The name property should be a number, not a string.
C. makeItQuack should accept any object without type restrictions.
D. There is no error; code runs fine.

Solution

  1. Step 1: Check properties of notADuck

    notADuck has name but lacks quack method required by Duck interface.
  2. Step 2: Understand TypeScript type checking

    TypeScript will report an error because notADuck does not satisfy the Duck type.
  3. Final Answer:

    notADuck is missing the quack method required by Duck interface. -> Option A
  4. Quick Check:

    Missing required method = Type error [OK]
Hint: Check all required properties/methods exist for the type [OK]
Common Mistakes:
  • Ignoring missing methods in objects
  • Assuming extra properties cause errors
  • Thinking TypeScript allows missing required members
5. Given this code, which object will TypeScript accept as a valid Duck type?
interface Duck { name: string; quack: () => void; }

const a = { name: 'Daisy', quack: () => console.log('Quack!') };
const b = { name: 'Goose', quack: 'loud' };
const c = { name: 'Swan', quack: () => 'quack' };
const d = { name: 'Mallard' };

function testDuck(duck: Duck) { duck.quack(); }

Which call to testDuck will NOT cause a TypeScript error?
hard
A. testDuck(c);
B. testDuck(b);
C. testDuck(a);
D. testDuck(d);

Solution

  1. 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).
  2. Step 2: Determine which matches Duck interface

    Only object a matches the Duck interface exactly.
  3. Final Answer:

    testDuck(a); -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard