tiny-decoders
Advanced tools
Comparing version
@@ -19,5 +19,8 @@ export type Decoder<T, U = unknown> = (value: U) => T; | ||
export declare function string(value: unknown): string; | ||
export declare function stringUnion<T extends [string, ...Array<string>]>(variants: readonly [...T]): Decoder<T[number]>; | ||
export declare function stringUnion<const T extends readonly [string, ...Array<string>]>(variants: T): Decoder<T[number]>; | ||
export declare function array<T>(decoder: Decoder<T>): Decoder<Array<T>>; | ||
export declare function record<T>(decoder: Decoder<T>): Decoder<Record<string, T>>; | ||
/** | ||
* @deprecated Use `fieldsAuto` instead. | ||
*/ | ||
export declare function fields<T>(callback: (field: <U>(key: string, decoder: Decoder<U>) => U, object: Record<string, unknown>) => T, { exact, allow, }?: { | ||
@@ -27,7 +30,24 @@ exact?: "allow extra" | "throw"; | ||
}): Decoder<WithUndefinedAsOptional<T>>; | ||
export declare function fieldsAuto<T extends Record<string, unknown>>(mapping: { | ||
[P in keyof T]: P extends "__proto__" ? never : Decoder<T[P]>; | ||
}, { exact }?: { | ||
type Field<Decoded, Meta extends FieldMeta> = Meta & { | ||
decoder: Decoder<Decoded>; | ||
}; | ||
type FieldMeta = { | ||
renameFrom?: string | undefined; | ||
optional?: boolean | undefined; | ||
}; | ||
type FieldsMapping = Record<string, Decoder<any> | Field<any, FieldMeta>>; | ||
type InferField<T extends Decoder<any> | Field<any, FieldMeta>> = T extends Field<any, FieldMeta> ? ReturnType<T["decoder"]> : T extends Decoder<any> ? ReturnType<T> : never; | ||
type InferFields<Mapping extends FieldsMapping> = Expand<{ | ||
[Key in keyof Mapping as Mapping[Key] extends { | ||
optional: true; | ||
} ? never : Key]: InferField<Mapping[Key]>; | ||
} & { | ||
[Key in keyof Mapping as Mapping[Key] extends { | ||
optional: true; | ||
} ? Key : never]?: InferField<Mapping[Key]>; | ||
}>; | ||
export declare function fieldsAuto<Mapping extends FieldsMapping>(mapping: Mapping, { exact }?: { | ||
exact?: "allow extra" | "throw"; | ||
}): Decoder<WithUndefinedAsOptional<T>>; | ||
}): Decoder<InferFields<Mapping>>; | ||
export declare function field<Decoded, const Meta extends FieldMeta>(decoder: Decoder<Decoded>, meta: Meta): Field<Decoded, Meta>; | ||
type Values<T> = T[keyof T]; | ||
@@ -39,5 +59,5 @@ export declare function fieldsUnion<T extends Record<string, Decoder<unknown>>>(key: string, mapping: keyof T extends string ? keyof T extends never ? "fieldsUnion must have at least one member" : T : { | ||
}>>>; | ||
export declare function tuple<T extends Array<unknown>>(mapping: readonly [...{ | ||
export declare function tuple<T extends Array<unknown>>(mapping: [...{ | ||
[P in keyof T]: Decoder<T[P]>; | ||
}]): Decoder<[...T]>; | ||
}]): Decoder<T>; | ||
type Multi<Types> = Types extends any ? Types extends "undefined" ? { | ||
@@ -66,5 +86,6 @@ type: "undefined"; | ||
type MultiTypeName = "array" | "boolean" | "null" | "number" | "object" | "string" | "undefined"; | ||
export declare function multi<Types extends [MultiTypeName, ...Array<MultiTypeName>]>(types: readonly [...Types]): Decoder<Multi<Types[number]>>; | ||
export declare function optional<T>(decoder: Decoder<T>): Decoder<T | undefined>; | ||
export declare function optional<T, U>(decoder: Decoder<T>, defaultValue: U): Decoder<T | U>; | ||
export declare function multi<Types extends readonly [MultiTypeName, ...Array<MultiTypeName>]>(types: Types): Decoder<Multi<Types[number]>>; | ||
export declare function recursive<T>(callback: () => Decoder<T>): Decoder<T>; | ||
export declare function undefinedOr<T>(decoder: Decoder<T>): Decoder<T | undefined>; | ||
export declare function undefinedOr<T, U>(decoder: Decoder<T>, defaultValue: U): Decoder<T | U>; | ||
export declare function nullable<T>(decoder: Decoder<T>): Decoder<T | null>; | ||
@@ -82,2 +103,6 @@ export declare function nullable<T, U>(decoder: Decoder<T>, defaultValue: U): Decoder<T | U>; | ||
} | { | ||
tag: "missing field"; | ||
field: string; | ||
got: Record<string, unknown>; | ||
} | { | ||
tag: "tuple size"; | ||
@@ -123,5 +148,5 @@ expected: number; | ||
value: unknown; | ||
key?: Key; | ||
key?: Key | undefined; | ||
} | (DecoderErrorVariant & { | ||
key?: Key; | ||
key?: Key | undefined; | ||
})); | ||
@@ -133,10 +158,10 @@ static MISSING_VALUE: symbol; | ||
export type ReprOptions = { | ||
depth?: number; | ||
indent?: string; | ||
maxArrayChildren?: number; | ||
maxObjectChildren?: number; | ||
maxLength?: number; | ||
sensitive?: boolean; | ||
depth?: number | undefined; | ||
indent?: string | undefined; | ||
maxArrayChildren?: number | undefined; | ||
maxObjectChildren?: number | undefined; | ||
maxLength?: number | undefined; | ||
sensitive?: boolean | undefined; | ||
}; | ||
export declare function repr(value: unknown, { depth, indent, maxArrayChildren, maxObjectChildren, maxLength, sensitive, }?: ReprOptions): string; | ||
export {}; |
{ | ||
"name": "tiny-decoders", | ||
"version": "10.0.0", | ||
"version": "11.0.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Simon Lydell", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43679
10.78%1170
10.69%