UNPKG

5.78 kBTypeScriptView Raw
1import { Struct, Infer, Result, Context, Describe } from './struct';
2import { Failure } from './error';
3/**
4 * Check if a value is a plain object.
5 */
6export declare function isObject(x: unknown): x is object;
7/**
8 * Check if a value is a plain object.
9 */
10export declare function isPlainObject(x: unknown): x is {
11 [key: string]: any;
12};
13/**
14 * Return a value as a printable string.
15 */
16export declare function print(value: any): string;
17/**
18 * Shifts (removes and returns) the first value from the `input` iterator.
19 * Like `Array.prototype.shift()` but for an `Iterator`.
20 */
21export declare function shiftIterator<T>(input: Iterator<T>): T | undefined;
22/**
23 * Convert a single validation result to a failure.
24 */
25export declare function toFailure<T, S>(result: string | boolean | Partial<Failure>, context: Context, struct: Struct<T, S>, value: any): Failure | undefined;
26/**
27 * Convert a validation result to an iterable of failures.
28 */
29export declare function toFailures<T, S>(result: Result, context: Context, struct: Struct<T, S>, value: any): IterableIterator<Failure>;
30/**
31 * Check a value against a struct, traversing deeply into nested values, and
32 * returning an iterator of failures or success.
33 */
34export declare function run<T, S>(value: unknown, struct: Struct<T, S>, options?: {
35 path?: any[];
36 branch?: any[];
37 coerce?: boolean;
38 mask?: boolean;
39}): IterableIterator<[Failure, undefined] | [undefined, T]>;
40/**
41 * Convert a union of type to an intersection.
42 */
43export declare type UnionToIntersection<U> = (U extends any ? (arg: U) => any : never) extends (arg: infer I) => void ? I : never;
44/**
45 * Assign properties from one type to another, overwriting existing.
46 */
47export declare type Assign<T, U> = Simplify<U & Omit<T, keyof U>>;
48/**
49 * A schema for enum structs.
50 */
51export declare type EnumSchema<T extends string | number | undefined> = {
52 [K in NonNullable<T>]: K;
53};
54/**
55 * Check if a type is a match for another whilst treating overlapping
56 * unions as a match.
57 */
58export declare type IsMatch<T, G> = T extends G ? (G extends T ? T : never) : never;
59/**
60 * Check if a type is an exact match.
61 */
62export declare type IsExactMatch<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? T : never;
63/**
64 * Check if a type is a record type.
65 */
66export declare type IsRecord<T> = T extends object ? string extends keyof T ? T : never : never;
67/**
68 * Check if a type is a tuple.
69 */
70export declare type IsTuple<T> = T extends [any] ? T : T extends [any, any] ? T : T extends [any, any, any] ? T : T extends [any, any, any, any] ? T : T extends [any, any, any, any, any] ? T : never;
71/**
72 * Check if a type is a union.
73 */
74export declare type IsUnion<T, U extends T = T> = (T extends any ? (U extends T ? false : true) : false) extends false ? never : T;
75/**
76 * A schema for object structs.
77 */
78export declare type ObjectSchema = Record<string, Struct<any, any>>;
79/**
80 * Infer a type from an object struct schema.
81 */
82export declare type ObjectType<S extends ObjectSchema> = Simplify<Optionalize<{
83 [K in keyof S]: Infer<S[K]>;
84}>>;
85/**
86 * Omit properties from a type that extend from a specific type.
87 */
88export declare type OmitBy<T, V> = Omit<T, {
89 [K in keyof T]: V extends Extract<T[K], V> ? K : never;
90}[keyof T]>;
91/**
92 * Normalize properties of a type that allow `undefined` to make them optional.
93 */
94export declare type Optionalize<S extends object> = OmitBy<S, undefined> & Partial<PickBy<S, undefined>>;
95/**
96 * Transform an object schema type to represent a partial.
97 */
98export declare type PartialObjectSchema<S extends ObjectSchema> = {
99 [K in keyof S]: Struct<Infer<S[K]> | undefined>;
100};
101/**
102 * Pick properties from a type that extend from a specific type.
103 */
104export declare type PickBy<T, V> = Pick<T, {
105 [K in keyof T]: V extends Extract<T[K], V> ? K : never;
106}[keyof T]>;
107/**
108 * Simplifies a type definition to its most basic representation.
109 */
110export declare type Simplify<T> = T extends any[] | Date ? T : {
111 [K in keyof T]: T[K];
112} & {};
113export declare type If<B extends Boolean, Then, Else> = B extends true ? Then : Else;
114/**
115 * A schema for any type of struct.
116 */
117export declare type StructSchema<T> = [T] extends [string | undefined] ? [T] extends [IsMatch<T, string | undefined>] ? null : [T] extends [IsUnion<T>] ? EnumSchema<T> : T : [T] extends [number | undefined] ? [T] extends [IsMatch<T, number | undefined>] ? null : [T] extends [IsUnion<T>] ? EnumSchema<T> : T : [T] extends [boolean] ? [T] extends [IsExactMatch<T, boolean>] ? null : T : T extends bigint | symbol | undefined | null | Function | Date | Error | RegExp | Map<any, any> | WeakMap<any, any> | Set<any> | WeakSet<any> | Promise<any> ? null : T extends Array<infer E> ? T extends IsTuple<T> ? null : Struct<E> : T extends object ? T extends IsRecord<T> ? null : {
118 [K in keyof T]: Describe<T[K]>;
119} : null;
120/**
121 * A schema for tuple structs.
122 */
123export declare type TupleSchema<T> = {
124 [K in keyof T]: Struct<T[K]>;
125};
126/**
127 * Shorthand type for matching any `Struct`.
128 */
129export declare type AnyStruct = Struct<any, any>;
130/**
131 * Infer a tuple of types from a tuple of `Struct`s.
132 *
133 * This is used to recursively retrieve the type from `union` `intersection` and
134 * `tuple` structs.
135 */
136export declare type InferStructTuple<Tuple extends AnyStruct[], Length extends number = Tuple['length']> = Length extends Length ? number extends Length ? Tuple : _InferTuple<Tuple, Length, []> : never;
137declare type _InferTuple<Tuple extends AnyStruct[], Length extends number, Accumulated extends unknown[], Index extends number = Accumulated['length']> = Index extends Length ? Accumulated : _InferTuple<Tuple, Length, [...Accumulated, Infer<Tuple[Index]>]>;
138export {};
139//# sourceMappingURL=utils.d.ts.map
\No newline at end of file