Complete the code to define an object that matches the interface by duck typing.
interface Bird {
fly(): void;
}
const duck: Bird = {
[1]() {
console.log('Flying');
}
};The object must have a method named fly to match the Bird interface by duck typing.
Complete the code to assign an object with extra properties to a variable typed by an interface.
interface Person {
name: string;
}
const user: Person = {
name: 'Alice',
[1]: 30
};name.Extra properties like age are allowed in duck typing as long as required properties exist.
Fix the error by completing the code to match the interface using duck typing.
interface Logger {
log(message: string): void;
}
const consoleLogger: Logger = {
[1](msg: string) {
console.log(msg);
}
};The method name must be log to match the Logger interface for duck typing.
Fill both blanks to create a function that accepts any object with a 'speak' method and calls it.
function makeSpeak(obj: { [1](): void }) {
obj.[2]();
}The function expects an object with a method named speak and calls that method.
Fill all three blanks to create a duck-typed object with a method and an extra property.
interface Animal {
[1](): void;
}
const cat: Animal = {
[2]() {
console.log('Meow');
},
[3]: 'black'
};The interface requires a method makeSound. The object has that method and an extra property color.