TypeScript for real code · Chapter 11 of 36

Data from outside

By the end of this chapter you can take data your code did not create, like JSON from another app, and check it before you trust it, with unknown instead of any. 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 changed the price of masala chai in the admin panel, and the app's bill now says ₹405. The admin panel sent the price as the text "40", and "40" + 5 is "405". Bunty had typed every function that reads the menu as any, and it all compiled.

Everything you have typed so far was data your own code made, so the types were true. JSON from the admin panel or the rider app is whatever was sent, and JSON.parse returns any, which switches checking off. Writing as MenuItem after it only tells the compiler what you hope. Nothing checks the "40".

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    readPrice: take unknown and check before reading

    readPrice reads the price from some data: readPrice({ name: 'masala chai', price: 40 }) is 40.

    Give data the type unknown instead of any, and the return type number. If data is not an object with a number price, throw new Error('No price in this data'): that covers null, text like 'chai', and { price: '40' } with the price written as text.

  2. Task 2

    isMenuItem: a type predicate for unknown values

    isMenuItem is true when value is an object with a name that is a string and a price that is a number. isMenuItem({ name: 'masala chai', price: 40 }) is true, and so is isMenuItem({ name: 'cutting chai', price: 0 }). isMenuItem({ name: 'masala chai', price: '40' }), isMenuItem({ price: 25 }), isMenuItem(null) and isMenuItem('masala chai') are all false.

    Take value: unknown instead of any, and give it the return type value is MenuItem, so inside if (isMenuItem(value)) value is a MenuItem.

  3. Task 3

    parseMenu: checked menu items, not any

    parseMenu turns the menu's JSON text into a MenuItem[]: parseMenu('[{"name":"masala chai","price":40},{"name":"bun maska","price":30}]') gives those two items, and parseMenu('[]') is [].

    Its return type must be exactly MenuItem[], not any. Check that the parsed data is an array and that every item passes isMenuItem; otherwise throw new Error('Bad menu'). So a price written as text, '[{"name":"masala chai","price":"40"}]', and one item that is not in a list, '{"name":"masala chai","price":40}', both throw 'Bad menu'.

  4. Task 4 · interview question

    Interview: safeJson returns unknown instead of any

    safeJson is a JSON.parse that never throws: safeJson('{"price":40}') is { price: 40 }, safeJson('[1,2]') is [1, 2], and safeJson('not json') is undefined.

    Give it the return type unknown, so every caller has to check what came back: safeJson('{"price":40}').price is a type error. Dev will ask what the difference is between any and unknown.

What you will learn

  • How any spreads: one any makes everything it touches any, far from where it was written.
  • unknown: accepts every value like any, but lets you use none until you check.
  • Narrowing unknown: typeof, a null check and in, one step at a time, wrapped in a type predicate when you need it again.

More in Modelling Tapri

Build "Data from outside" 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.