Skip to content

@snailicide/build-config / utilities / Jsonifiable

Jsonifiable

ts
type Jsonifiable = JsonPrimitive | JsonifiableObject | JsonifiableArray;

Defined in: node_modules/.pnpm/type-fest@4.41.0/node_modules/type-fest/source/jsonifiable.d.ts:37

Matches a value that can be losslessly converted to JSON.

Can be used to type values that you expect to pass to JSON.stringify.

undefined is allowed in object fields (for example, {a?: number}) as a special case even though JSON.stringify({a: undefined}) is {} because it makes this class more widely useful and checking for undefined-but-present values is likely an anti-pattern.

Example

import type {Jsonifiable} from 'type-fest';

// @ts-expect-error
const error: Jsonifiable = {
    map: new Map([['a', 1]]),
};

JSON.stringify(error);
//=> {"map": {}}

const good: Jsonifiable = {
    number: 3,
    date: new Date(),
    missing: undefined,
}

JSON.stringify(good);
//=> {"number": 3, "date": "2022-10-17T22:22:35.920Z"}