Skip to content

JSON type

Daisho Komiyama edited this page Apr 19, 2023 · 1 revision
type Primitive = string | number | boolean | null

type JSONObject = { [k: string]: JSONValue }
type JSONArray = JSONValue[]
type JSONValue = Primitive | JSONArray | JSONObject

function isJSON(arg: JSONValue) {}

isJSON("hello")
isJSON([4, 8, 15, "yo"])
isJSON({ greeting: "hello", age: 93 })
isJSON(false)
isJSON(true)
isJSON(null)
isJSON({ a: { b: [2, 3, "foo" ] } })

// NEGATIVE test cases (must fail)
// @ts-expect-error
isJSON(() => "")
// @ts-expect-error
isJSON(undefined)
// @ts-expect-error
isJSON(new BigInt(143))
Clone this wiki locally