TIL

Typescript type for the JSON spec

Some JavaScript types like dates are not representable directly with JSON and require extra care to parse back out to a JavaScript object after they’re serialized. If you want to make sure that you are only stringify-ing JSON-safe types, you can use this type to represent objects compatible with the JSON spec:

type Json =
    | { [key: string]: Json }
    | Json[]
    | string
    | number
    | true
    | false
    | null;

function safeStringify(obj: Json) {
    return JSON.stringify(obj);
}

If you have some type that is not JSON-compatible that you want to serialize, this type will help you write a serialize() function where the Typescript compiler confirms that the function is producing a safe-to-stringify value:

type MyType = {
    date: Date,
    title: string,	
}

function serialize(obj: MyType): Json {
    return {title: obj.title, date: obj.date.toISOString()};
}

Last Updated in Apr 2023