I am unable to figure out the reason why summary()
when destructured in printVehicle returns undefined
. Whereas it works fine and returns Civic
without destructuring by using oldVehicle.summary()
.
interface Vehicle {
name: string;
year: number;
broken: boolean;
summary(): string;
}
const oldCivic = {
name: 'Civic',
year: 2000,
broken: true,
summary(): string {
return this.name;
}
};
console.log(oldCivic.summary());
const printVehicle = ({ name, year, broken, summary }: Vehicle): void => {
console.log(`
Name: ${name}
Year: ${year}
broken: ${broken}
summary: ${summary()}
`);
};
printVehicle(oldCivic);