Command Palette

Search for a command to run...

Hectal
Phase 3: Type Aliases and Interfaces
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

  1. Your code has no type errors
  2. The console prints Total: 300
  3. Perishable extends Item
  4. A sku cannot be changed
  5. A perishable needs an expiry
  6. 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.

Console

Nothing printed yet.