UNPKG

json-decoder

Version:

Lightweight, lightning-fast, type safe JSON decoder for TypeScript

48 lines (47 loc) 2.28 kB
export declare const OK = "OK"; export declare const ERR = "ERR"; export type Ok<T> = { type: typeof OK; value: T; map: <T2>(func: (t: T) => T2) => Result<T2>; }; export type Err<T> = { type: typeof ERR; message: string; map: <T2>(func: (t: T) => T2) => Result<T2>; }; export type Result<T> = Ok<T> | Err<T>; export declare const ok: <T>(value: T) => Result<T>; export declare const err: <T>(message: string) => Result<T>; export type Decoder<T> = { decode: (a: unknown) => Result<T>; decodeAsync: (a: unknown) => Promise<T>; map: <T2>(func: (t: T) => T2) => Decoder<T2>; bind: <T2>(func: (t: T) => Decoder<T2>) => Decoder<T2>; then: <T2>(nextDecoder: Decoder<T2>) => Decoder<T2>; validate: (func: (t: T) => boolean, errMessage?: string | ((t: T) => string)) => Decoder<T>; }; export type DecoderType<D> = D extends Decoder<infer T> ? T : never; export type ArrayType<A> = A extends Array<infer T> ? T : never; export type DecoderArrayType<DD> = DecoderType<ArrayType<DD>>; export declare const decoder: <T>(decode: (a: unknown) => Result<T>) => Decoder<T>; type ArrayDecoder<T> = Decoder<T[]>; type DecoderMap<T> = { [K in keyof T]: Decoder<T[K]>; }; export declare const stringDecoder: Decoder<string>; export declare const numberDecoder: Decoder<number>; export declare const boolDecoder: Decoder<boolean>; export declare const nullDecoder: Decoder<null>; export declare const undefinedDecoder: Decoder<undefined>; export declare const arrayDecoder: <T>(itemDecoder: Decoder<T>) => ArrayDecoder<T>; export declare const oneOfDecoders: <T>(...decoders: Decoder<T>[]) => Decoder<T>; type LastElem<T extends number> = [-1, 0, 1, 2, 3, 4, 5][T]; type LastElemType<T extends unknown[]> = T[LastElem<T["length"]>]; type LastDecoder<T extends Decoder<unknown>[]> = LastElemType<T> extends Decoder<infer R> ? R : T[0]; export declare const allOfDecoders: <TDecoders extends Decoder<unknown>[], R = LastDecoder<TDecoders>>(...decoders: TDecoders) => Decoder<R>; export declare const exactDecoder: <T>(value: T) => Decoder<T>; export declare const objectDecoder: <T>(decoderMap: DecoderMap<T>) => Decoder<T>; export declare const anyDecoder: Decoder<unknown>; export declare const valueDecoder: <T>(value: T) => Decoder<T>; export {};