0
0
Typescriptprogramming~20 mins

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

Choose your learning style9 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.