TypeScript for real code · Chapter 8 of 36
Sizes, statuses and menus
By the end of this chapter you can write a fixed set of values once, and get both the data and its type from that one place. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Modelling Tapri
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.
Why you need this
Anjali has a customer whose delivered chai shows "Unknown" on the tracking screen: the status was typed as string, and someone wrote 'delivred' in the code. Ravi wants a new cup size, regular, added in one place, not in five. And the order form sends the size as text, which must become one of the real sizes or nothing.
A string accepts every misspelling, so TypeScript cannot help with the typo. Writing the sizes as a union by hand works, but then the list for the dropdown and the type are two copies that drift apart. And Bunty's favourite fix, text as Size, lets any text through.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
OrderStatus: the four real statuses
Bunty typed an order's status as string, so typos slip through, and there is one in statusLabel. Make OrderStatus exactly 'placed' | 'brewing' | 'on-the-way' | 'delivered', so a status like 'shipped' is a type error, then fix what typecheck finds.
statusLabel('brewing') is 'Brewing your chai' and statusLabel('delivered') is 'Delivered, enjoy'.
- Task 2
Size: derived from a SIZES list that cannot change
The cup sizes are written once, in SIZES, for the app and for the types. SIZES stays ['small', 'regular', 'large'] when the file runs.
Make SIZES readonly with its exact values, so SIZES.push('jumbo') is a type error, and derive Size from it, so Size is exactly 'small' | 'regular' | 'large'. Do not write the sizes out a second time in the type.
- Task 3
MenuName: derived from the PRICES object
PRICES holds the menu. priceOf('bun maska') is 30 and priceOf('masala chai') is 40.
Make PRICES readonly with its exact values, so the app cannot change a price (PRICES['masala chai'] = 10 is a type error), and derive MenuName from its keys, so MenuName is exactly 'masala chai' | 'cutting chai' | 'bun maska' and priceOf('filter coffee') is a type error. Leave priceOf as it is.
- Task 4 · interview question
Interview: parseSize, text from the form as a Size or null
The size box on the order form sends text. parseSize('large') is 'large', parseSize('small') is 'small' and parseSize('jumbo') is null.
Its return type must be exactly Size or null, the three sizes or null, never a plain string. text stays a string, so parseSize(2) is a type error. Do it without a cast (as): a cast checks nothing. Dev will ask why includes refuses the text, and how to get a Size out without lying to the compiler.
What you will learn
- Literal unions: a closed set of exact values, like 'dairy' | 'oat' | 'almond', instead of an enum.
- as const: keeps a list's or an object's exact values and makes it readonly.
- Deriving the type: typeof, [number] and keyof turn that one value into a union, so the type grows with the data.
More in Modelling Tapri
Build "Sizes, statuses and menus" yourself
Tapri’s codebase grows, and types keep it honest. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.