TypeScript for real code · Chapter 36 of 36
The interview
By the end of this chapter you can answer Dev's five interview questions with real code: model data in states, wrap it generically, check what comes from outside, build types from types, and take out the casts that hide bugs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The interview
- 5 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
Meera says the investors will ask one thing about the tech team: can the new developers be trusted with the code? Dev answers it the way a TypeScript interview does. Not with syntax questions, but with real code: Bunty's order API, which runs, and whose types let every bug through. The compiler is his second interviewer.
Bunty's file has one loose Order type where everything is optional, a result type whose data is any, a guard that only asks "is it an object?", an as Order on a delivered order with no rider, and a menu typed any. Each of these compiles. Each hides a bug that a customer would find first.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: Order, one shape per state
Every order has an id (string) and items (string[]). By status: a 'placed' order has nothing else, a 'delivered' one has rider (string), and a 'cancelled' one has reason (string). Write Order as a union of those three shapes instead of one loose type, so a delivered order without a rider, a cancelled one without a reason, a placed one with a rider, a status like 'lost', and an order without items are all type errors, and rider can only be read after checking that the status is 'delivered'.
orderSummary keeps working: a placed A1 with two items is 'A1: placed, 2 items', A2 delivered by Salim is 'A2: delivered by Salim', and A3 cancelled for rain is 'A3: cancelled (rain)'. With every status handled, typecheck flags its default (the order is never there): take it out. Dev will ask what a union of states gives you that optional fields do not.
- Task 2 · interview question
Interview: ApiResult, one envelope for any data
ApiResult<T> is a union on ok: either { ok: true; data: T } or { ok: false; error: string; status: number }. Then a failed result without its status, a successful one without data, and an ApiResult<Order> whose data is 'A1' are type errors, and data can only be read after checking ok.
unwrap(result) gives back the data with its type T: for an ApiResult<number> it returns a number. unwrap({ ok: true, data: 40 }) is 40, and unwrap({ ok: false, error: 'not found', status: 404 }) throws 'not found'. No any. Dev will ask why data is not simply an optional field.
- Task 3 · interview question
Interview: parseOrder checks instead of trusting any
isOrder(value) takes unknown, not any, and is a type guard (value is Order) that really checks the shape from task 1: an object, not null, with id a string, items a list of strings, and status 'placed', or 'delivered' with a rider string, or 'cancelled' with a reason string. isOrder({ id: 'A5', items: [], status: 'cancelled', reason: 'rain' }) is true, the same without the reason is false, and isOrder('A5') is false.
parseOrder(json) returns exactly ApiResult<Order>: { ok: true, data: order } for a real order, and { ok: false, error: 'not an order', status: 400 } for anything else. That includes a delivered order with no rider, the status 'lost', items holding a number, no id, 'null', and text that is not JSON at all, like 'masala chai': JSON.parse throws on it. Dev will ask why unknown and not any.
- Task 4 · interview question
Interview: OrderIn, the order type for a status
OrderIn<S> is the order type for one status, or for a union of statuses, and S must be a status. OrderIn<'cancelled'> has reason, a string; OrderIn<'delivered'>['status'] is 'delivered'; OrderIn<'placed' | 'delivered'>['status'] is 'placed' | 'delivered'; and OrderIn<'cancelled'>['rider'] and OrderIn<'lost'> are type errors.
byStatus(orders, status) keeps the orders in that status, with that type: for A1 placed and A2 and A3 delivered, byStatus(orders, 'delivered') gives A2 and A3, and each one's rider is a string; byStatus(orders, 'cancelled') is []; and byStatus(orders, 'lost') is a type error. Dev will ask how the status you pass picks the type that comes back.
- Task 5 · interview question
Interview: no as, no any, and the bugs they hid
sampleDelivered() is the example order on the design page: A9, with ['masala chai'], delivered by Salim. Bunty's as Order hid that it has no rider: take the as out, read the error, and add the rider 'Salim'.
priceOf(menu, name) gives the price of one item on a menu of prices: priceOf({ 'masala chai': 40, 'cutting chai': 25 }, 'cutting chai') is 25, and an item that is not on the menu, like 'bun maska', is 0. Type menu as an object from names to prices instead of any, so a menu of objects like { 'masala chai': { price: 40 } } is a type error. The any hid that a menu holds numbers, not objects with a price. Dev will ask why as and any are worse than an error.
What you will learn
- States as a union (chapter 9): one shape per status.
- Generic wrappers (chapter 16): one envelope around any data.
- Checking outside data (chapters 10, 11, 28): unknown, then a type guard.
- Types from types, and no casts (chapters 17, 24): Extract, and why as and any are risky.
More in The interview
Build The interview 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.