Exercise
Let's do a quick TypeScript exercise to put in practice what we've learned.
Open the TypeScript Playground where you should already see the following code (if not, copy and paste this in):
type Pet = { // TODO: implement the rest of this type name: string;};
const pets: Pet[] = [ { name: 'Fluffy', kind: 'cat', age: 10, favouriteFoods: ['fish', 'chicken'], speak: () => console.log('meow'), owner: { name: 'Sarah', age: 18, }, }, { name: 'Teddy', kind: 'dog', age: 2, favouriteFoods: ['anything off your table'], speak: () => console.log('woof'), owner: { name: 'Mary', age: 24, }, }, { name: 'Bubbles', kind: 'fish', age: 1, favouriteFoods: ['algae'], owner: { name: 'Steve', age: 30, }, },];
Implement the Pet type based on the pets array it's applied to. The playground will help you spot the errors via the red underlines, and you can hover over the underlines to read the error messages.