TypeScript for real code · Chapter 3 of 36

Lists and pairs

By the end of this chapter you can type a list, a list that nobody may change, and a pair or triple whose positions each have their own type. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Why types
  • 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

Ravi saw six orders on the counter screen and seven on the kitchen screen. Bunty's addToQueue pushed each new order into the list it was given, and that was the same list the counter screen was still drawing from. Ravi also wants his delivery log typed: an order id, a rider, and the minutes late, left out when the rider was on time.

So far you can write string[], a list of any length where every element has the same type. That says nothing about who may change the list, and a pair like "each pays, left over" comes out as number[], which could be empty or ten long. Bunty typed the rest as any[], which checks nothing at all.

What you build: 4 tasks

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

  1. Task 1

    menuPrices: a list of menu items in, a list of numbers out

    menuPrices gives the prices from a list of menu items: menuPrices(menu) is [40, 25, 30] and menuPrices([]) is [].

    Replace both any[] types: a list of MenuItem (the type is exported in the file) goes in, and a list of numbers comes out, so a list of names, like menuPrices(['Masala chai']), is a type error. No any left: any checks nothing.

  2. Task 2

    Interview: addToQueue, leave the queue alone

    addToQueue adds an order id to the kitchen queue and returns the queue with it: addToQueue(['T-1'], 'T-2') is ['T-1', 'T-2'], and the list it was given is still ['T-1'] afterwards. Bunty's version pushes into that list, the same one the counter screen draws from.

    Make queue a readonly list of strings, so push is a type error and a read-only list can be passed in, and return a new list instead. It still returns a list of strings, and an order id is text: addToQueue(queue, 42) is a type error. Dev will ask why the two screens disagreed and how readonly stops it.

  3. Task 3

    splitBill: a pair, not a list of any length

    splitBill splits a bill between friends as [each pays, left over]: splitBill(100, 3) is [33, 1] and splitBill(120, 4) is [30, 0].

    number[] means numbers, any how many. Make the return type a tuple of exactly two numbers, so the result has length 2, each part is a number, and reading a third part, like bill[2], is a type error. The code inside already returns a pair: only the type changes.

  4. Task 4

    Delivery: a tuple with the minutes late optional

    Ravi logs a delivery as [order id, rider, minutes late], and leaves out the minutes when the rider was on time, so ['T-7', 'Arjun'] and ['T-8', 'Arjun', 10] are both deliveries. lateMessage(['T-8', 'Arjun', 10]) is 'T-8 with Arjun, 10 min late' and lateMessage(['T-7', 'Arjun']) is 'T-7 with Arjun, on time'.

    Write Delivery as a tuple instead of any[]: a string, a string, and an optional number. So a delivery with no rider, ['T-9'], minutes in words, ['T-9', 'Arjun', 'ten'], and a fourth part, ['T-9', 'Arjun', 10, 'rain'], are all type errors. lateMessage stays as it is.

What you will learn

  • List types: number[] and lists of your own types, kept by array methods like map.
  • Read-only lists: readonly string[] has no push, pop or sort, so a function cannot change the caller's list.
  • Tuples: a list with a fixed length and a type for each position, with labels and optional positions.

More in Why types

Build "Lists and pairs" 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.