0Pricing
TypeScript Academy · Lesson

JSON Value Types

Define a complete, type-safe JSON value type.

What Is a JSON Value?

JSON allows a fixed set of value shapes: strings, numbers, booleans, null, arrays, and objects. A recursive type can describe exactly that set.

type Json =
  | string
  | number
  | boolean
  | null
  | Json[]
  | { [k: string]: Json };
// arrays and objects nest Json again.

The Recursive JSON Type

The key insight: arrays contain Json and object values are Json, so the type references itself for nested data.

type Json =
  | string | number | boolean | null
  | Json[]
  | { [k: string]: Json };
const x: Json = { name: "Ada", tags: ["a", "b"], active: true };
console.log(x);

All lessons in this course

  1. Recursive Type Definitions
  2. Typing Tree Structures
  3. JSON Value Types
  4. Recursion Depth and Limits
← Back to TypeScript Academy