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);