JavaScript traps · Chapter 8 of 16

JSON losing types

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

  • Part: Everyday traps
  • 3 tasks
  • JavaScript
  • In the all labs plan

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

Why you need this

Saving worked. Loading worked. The string even prints like a Date. The crash comes much later, in other code, when something calls a Date method on a string. And structuredClone is no way out here: it keeps Dates and BigInts, but it gives back an object, not text, so it cannot go into a column, a file or a request.

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.js: lose a Date on the way through history

    Write reproduce() in repro.js. Build an order whose placedAt is a Date (give it an id, items and a note too, but no BigInt points, so today's saveOrder does not throw). Save it with saveOrder from history.js, load the text back with loadOrder, and resolve to typeof the loaded order's placedAt.

    The check runs your reproduce() twice: beside the original history.js it must resolve to 'string' (the Date came back as text), and beside a fixed history.js it must resolve to 'object' (a Date). So it must really save and load an order with a Date. Nothing to wait for: reproduce() must finish within 2 seconds.

  2. Task 2

    history.js: an order comes back as it was saved

    Fix saveOrder(order) and loadOrder(text) in history.js so that loadOrder(saveOrder(order)) gives the order back: every Date, nested ones too, comes back a Date at the same time; every BigInt comes back the same BigInt, however large; null comes back null; strings, numbers, arrays and plain objects come back as they were.

    saveOrder must still give plain JSON text that JSON.parse can read on its own: write a Date as { "$date": "<its toISOString()>" } and a BigInt as { "$bigint": "<its digits>" }, and have loadOrder turn exactly those two shapes back. A string that only looks like a date, such as a customer's note '2026-10-24', must come back a string. Keep daysSince as it is. The check uses its own order, with a Date nested inside another object and a BigInt far past what a number can hold.

  3. Task 3

    history.test.js: a test that would have caught it

    Write a vitest test file, history.test.js, that would have caught this bug before it shipped. The check runs your file three times: beside the original history.js, where at least one test must fail; beside the fixed history.js, where every test must pass; and beside a wrong fix that turns every string that looks like a date into a Date, where at least one test must fail.

    So test a round trip, saveOrder then loadOrder, of an order with a Date, a BigInt and a note that looks like a date: the Date comes back a Date at the same time, the BigInt the same BigInt, and the note '2026-10-24' a string. Each test must finish within 3 seconds.

What you will learn

  • repro.js: lose a Date on the way through history
  • history.js: an order comes back as it was saved
  • history.test.js: a test that would have caught it

More in Everyday traps

Build "JSON losing types" yourself

Tapri runs on JavaScript, 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 JavaScript traps are free. This one comes with the all labs plan.