TypeScript · Exercise 6 of 6 Previous
Intermediate
A Typed Inventory
Model a small shop. Declare three types: an interface Item (readonly text sku, text name, number price, number qty), an interface Perishable that extends Item and adds a number expiresInDays, and a type Inventory that maps each sku text to an Item.
The code at the bottom creates a perishable milk, an ordinary pen, and totals the value of the inventory (price * qty for each item). Your types must make it type-check and print Total: 300.
What Check tests
- Your code has no type errors
- The console prints
Total: 300 PerishableextendsItem- A sku cannot be changed
- A perishable needs an expiry
- An inventory accepts items under any sku
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Press Run to see the page.
Console
Nothing printed yet.