Using An Interface In Typescript
interface PlushyDetails {
name: String
animal: String
price: Number
}
class Plushy {
details: PlushyDetails
constructor(details) {
this.details = details
}
listingDetails() {
return `${this.details.name || "Name"} the ${this.details.animal || "Animal"}: $${this.details.price || 12.34}`
}
}
let grove = new Plushy({name: "Grove", animal: "Duck"});
let ragu = new Plushy({name: "Ragu", animal: "Dragon", price: 16.23})
let blank = new Plushy({});
console.log(grove.listingDetails());
console.log(ragu.listingDetails())
console.log(blank.listingDetails())
Output:
"Grove the Duck: $12.34"
"Ragu the Dragon: $16.23"
"Name the Animal: $12.34"