TypeScript traps · Chapter 3 of 16

any leaking from a library

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Data from outside
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of TypeScript traps are free. This one comes with the all labs plan.

What you build: 3 tasks

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

  1. Task 1

    repro.ts: add up the cups as text on purpose

    Write reproduce() in repro.ts. Build the day's sales CSV from the ticket: the header item,cups,price and three rows under it, Masala chai,124,40 then Cutting chai,53,25 then Elaichi chai,38,35, one per line. Resolve to cupsSold of that CSV, from sales-report.ts.

    The check runs your reproduce() twice: beside the original sales-report.ts it must resolve to '01245338' (the cups from the ticket, added up as text), and beside a fixed sales-report.ts it must resolve to the number 215. So it must really send those three rows. No real waiting: the CSV is a string you write in, and reproduce() must finish within 2 seconds.

  2. Task 2

    sales-report.ts: give the library's rows real types

    Write and export readSales(csv: string): Sale[] in sales-report.ts. It reads the rows with parseCsv and checks each one before it becomes a Sale: item must be text that is not blank, and cups and price must be whole numbers written in digits only, 0 or more ('007' is 7). It gives { item, cups, price } for each row, in order, with cups and price as numbers. A blank cell, letters, a decimal, a minus sign or a missing cell in cups or price, or a blank item, makes it throw an Error saying which row is wrong.

    Then make cupsSold and revenue use readSales, so both give numbers, and keep Sale and reportText as they are. Do not change csv-kit.js or csv-kit.d.ts: they belong to the library. The check uses its own CSVs, broken ones too, and also type-checks sales-report.ts: readSales must return Sale[], with no any in it, and cupsSold and revenue must return number. Calling Number() on every cell is not a fix: a blank cell would count as 0 cups.

  3. Task 3

    sales-report.test.ts: a test that would have caught it

    Write a vitest test file, sales-report.test.ts, that would have caught this bug before the report went out. The check runs your file three times: beside the original sales-report.ts, where at least one test must fail; beside the fixed sales-report.ts, where every test must pass; and beside a wrong fix that calls Number() on every cell, where at least one test must fail.

    So test both sides through cupsSold, which every version has: a good CSV gives the number of cups (a number, not text), and a CSV with a blank cups cell is refused. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.ts: add up the cups as text on purpose
  • sales-report.ts: give the library's rows real types
  • sales-report.test.ts: a test that would have caught it

More in Data from outside

Build "any leaking from a library" yourself

Tapri moved to TypeScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. 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 traps are free. This one comes with the all labs plan.