TypeScript for real code · Chapter 32 of 36

Turning on strict

By the end of this chapter you can switch a JavaScript port to strict mode and fix what it finds with types and real checks, not with any, as or !. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Real-world TypeScript
  • 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

Bunty ported Tapri's rider and menu code from JavaScript in one afternoon, and set "strict": false "to make the red go away". That evening the rider screen crashed on an order no rider had picked up yet. Kabir has switched strict back on: every red line is a bug report that arrives before a customer finds the bug.

Without strict, TypeScript stays quiet in exactly the places where a JavaScript port goes wrong. A parameter with no type silently becomes any. A find that finds nothing is treated as if it found something. A this inside a callback can be anything. And Bunty's quick fixes (any, !) make an error disappear without checking a thing.

What you build: 5 tasks

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

  1. Task 1

    orderTotal: a type for the parameter strict found

    orderTotal(lines) adds up price times qty for every line of an order. orderTotal([{ name: 'masala chai', price: 40, qty: 2 }, { name: 'bun maska', price: 30, qty: 1 }]) is 110, and orderTotal([]) is 0.

    Give lines a real type: a list of objects with a name (string), a price (number) and a qty (number). Then a price written as text, a line with no qty, or one name instead of a list is a type error. orderTotal returns a number. Not any: that only hides the error, and the bad calls still compile.

  2. Task 2

    riderFor: a real check instead of !

    riderFor(riders, orderId) gives the name of the rider carrying that order, or 'not assigned' when no rider has it. With Salim carrying A7 and Imran carrying nothing: riderFor(riders, 'A7') is 'Salim', riderFor(riders, 'B2') is 'not assigned', and riderFor([], 'A7') is 'not assigned'.

    Keep the signature: it returns a string, never undefined. Bunty's ! silenced the "possibly undefined" error, and riderFor(riders, 'B2') still crashes. Take the ! out and handle the missing rider with a real check.

  3. Task 3

    TokenBoard: keep this in the callback

    serveAll(tokens) adds every token to the board's served list, in order: after serveAll([14, 15]) and then serveAll([16]), served is [14, 15, 16]. tokens stays a list of numbers, so serveAll(['1']) is a type error.

    Inside Bunty's function callback, this is not the board (forEach calls it with no this, so it is undefined), the code crashes, and strict reports it. Make this in the callback be the board, without typing this as any.

  4. Task 4 · interview question

    Interview: cheapest, honest about an empty menu

    cheapest(menu) gives the item with the lowest price: for masala chai at 40, cutting chai at 25 and bun maska at 30 it is { name: 'cutting chai', price: 25 }. An empty menu has no cheapest item, so cheapest([]) is undefined. Today it throws, because reduce with no starting value throws on an empty list.

    Its return type must be exactly MenuItem | undefined, so reading price from cheapest([]) without a check is a type error. Dev will ask what the old signature promised for an empty menu, and why strict did not catch it.

  5. Task 5

    exercise.ts: no type errors under strict

    When tasks 1 to 4 are done, typecheck prints No type errors for the whole file, with the lab's strict settings.

    Fix every error with a type or a real check: no any, no as, no ! and no this typed as any. A task can be green while its function still has a type error, so this task is the one that makes sure none is left.

What you will learn

  • What strict turns on: noImplicitAny, strictNullChecks and noImplicitThis, and the bug each one catches.
  • A check instead of !: why a non-null assertion is only a promise, and what to write instead.
  • this in callbacks: why a function callback loses the class's this, and an arrow function keeps it.
  • The holes strict leaves: an empty list, read with [0] or reduce.

More in Real-world TypeScript

Build "Turning on strict" 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.