TypeScript for real code · Chapter 12 of 36

Hands off

By the end of this chapter you can mark data that must not change, so TypeScript refuses every change, and freeze it, so JavaScript refuses them too. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Modelling Tapri
  • 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

Anjali set masala chai to ₹45 on the new price board, only to preview it, and every screen and every bill in the shop said ₹45. Bunty's withPrice changed the item it was given instead of making a new one, and all the screens share that one object. The same day his A to Z sort quietly reordered the real menu. Ravi wants the opening hours fixed in the code, so nobody moves closing time by accident.

With the types you know so far, a function that takes a MenuItem or a string[] may do anything to it, and nothing in its type says so. const does not help: it only stops you giving the name a new value. The object behind it can still change:

const RUSH = { from: '16:00', to: '18:00' };
RUSH = { from: '15:00', to: '18:00' };
$ typecheck demo.ts
demo.ts:2:1 - error TS2588: Cannot assign to 'RUSH' because it is a constant.

RUSH.to = 'whenever' on the next line compiles without a word.

What you build: 5 tasks

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

  1. Task 1

    MenuItem: an id and a name that never change

    Once an item is on the menu, its id and name never change, but Anjali still updates the price. Make id and name readonly in the MenuItem type, so item.id = 'm9' and item.name = 'kadak chai' are type errors.

    Leave price writable: item.price = 45 must still compile. Keep all three properties and their types (id and name are strings, price is a number).

  2. Task 2

    sortedNames: A to Z without touching the list

    sortedNames returns the names in A to Z order: sortedNames(['masala chai', 'bun maska', 'cutting chai']) is ['bun maska', 'cutting chai', 'masala chai']. The list passed in must keep its own order afterwards, and a frozen list, like Object.freeze(['parle-g', 'bun maska']), must work too (it gives ['bun maska', 'parle-g']).

    Type names as readonly string[], so a list made with as const is accepted, and return a new string[] that the caller may change.

  3. Task 3

    withPrice: a new item with a new price

    withPrice(chai, 45), where chai is { id: 'm1', name: 'masala chai', price: 40 }, returns { id: 'm1', name: 'masala chai', price: 45 }. It must be a new object: chai keeps its price of 40.

    Give item the type Readonly<MenuItem>, exactly, and keep the return type MenuItem.

  4. Task 4

    OPENING: opening hours nobody can move

    Tapri's opening hours must never change in the code. Add as const to OPENING, so OPENING.close = '01:00' and OPENING.days.push('holiday') are type errors, and the type of OPENING.close is exactly '23:00', not string.

    Keep the values as they are: close is '23:00' and there are seven days.

  5. Task 5 · interview question

    Interview: lockMenu, locked in the types and at runtime

    lockMenu(menu) returns a locked copy of the menu, with the same items: lockMenu([{ id: 'm1', name: 'masala chai', price: 40 }]) equals [{ id: 'm1', name: 'masala chai', price: 40 }].

    At runtime the returned list is frozen with Object.freeze, and so is every item in it. The menu you were given, the list and its items, must not be frozen. In the types, the result is readonly Readonly<MenuItem>[], so pushing to it and setting locked[0].price are type errors. It still takes a plain MenuItem[]. Dev will ask what the difference between readonly and Object.freeze is.

What you will learn

  • readonly: a property or a list that can be read but never changed, and Readonly<T> for every property of a type at once.
  • Copies instead of changes: methods like toReversed that return a new list, and a spread that returns a new object.
  • as const: a value fixed all the way down, with its exact text kept.
  • Object.freeze: the runtime lock, because types are gone when the file runs.

More in Modelling Tapri

Build "Hands off" 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.