UNPKG

5.56 kBTypeScriptView Raw
1import { Infer, Struct } from '../struct.js';
2import { ObjectSchema, ObjectType, AnyStruct, InferStructTuple, UnionToIntersection } from '../utils.js';
3/**
4 * Ensure that any value passes validation.
5 */
6export declare function any(): Struct<any, null>;
7/**
8 * Ensure that a value is an array and that its elements are of a specific type.
9 *
10 * Note: If you omit the element struct, the arrays elements will not be
11 * iterated at all. This can be helpful for cases where performance is critical,
12 * and it is preferred to using `array(any())`.
13 */
14export declare function array<T extends Struct<any>>(Element: T): Struct<Infer<T>[], T>;
15export declare function array(): Struct<unknown[], undefined>;
16/**
17 * Ensure that a value is a bigint.
18 */
19export declare function bigint(): Struct<bigint, null>;
20/**
21 * Ensure that a value is a boolean.
22 */
23export declare function boolean(): Struct<boolean, null>;
24/**
25 * Ensure that a value is a valid `Date`.
26 *
27 * Note: this also ensures that the value is *not* an invalid `Date` object,
28 * which can occur when parsing a date fails but still returns a `Date`.
29 */
30export declare function date(): Struct<Date, null>;
31/**
32 * Ensure that a value is one of a set of potential values.
33 *
34 * Note: after creating the struct, you can access the definition of the
35 * potential values as `struct.schema`.
36 */
37export declare function enums<U extends number, T extends readonly U[]>(values: T): Struct<T[number], {
38 [K in T[number]]: K;
39}>;
40export declare function enums<U extends string, T extends readonly U[]>(values: T): Struct<T[number], {
41 [K in T[number]]: K;
42}>;
43/**
44 * Ensure that a value is a function.
45 */
46export declare function func(): Struct<Function, null>;
47/**
48 * Ensure that a value is an instance of a specific class.
49 */
50export declare function instance<T extends {
51 new (...args: any): any;
52}>(Class: T): Struct<InstanceType<T>, null>;
53/**
54 * Ensure that a value is an integer.
55 */
56export declare function integer(): Struct<number, null>;
57/**
58 * Ensure that a value matches all of a set of types.
59 */
60export declare function intersection<A extends AnyStruct, B extends AnyStruct[]>(Structs: [A, ...B]): Struct<Infer<A> & UnionToIntersection<InferStructTuple<B>[number]>, null>;
61/**
62 * Ensure that a value is an exact value, using `===` for comparison.
63 */
64export declare function literal<T extends boolean>(constant: T): Struct<T, T>;
65export declare function literal<T extends number>(constant: T): Struct<T, T>;
66export declare function literal<T extends string>(constant: T): Struct<T, T>;
67export declare function literal<T>(constant: T): Struct<T, null>;
68/**
69 * Ensure that a value is a `Map` object, and that its keys and values are of
70 * specific types.
71 */
72export declare function map(): Struct<Map<unknown, unknown>, null>;
73export declare function map<K, V>(Key: Struct<K>, Value: Struct<V>): Struct<Map<K, V>, null>;
74/**
75 * Ensure that no value ever passes validation.
76 */
77export declare function never(): Struct<never, null>;
78/**
79 * Augment an existing struct to allow `null` values.
80 */
81export declare function nullable<T, S>(struct: Struct<T, S>): Struct<T | null, S>;
82/**
83 * Ensure that a value is a number.
84 */
85export declare function number(): Struct<number, null>;
86/**
87 * Ensure that a value is an object, that is has a known set of properties,
88 * and that its properties are of specific types.
89 *
90 * Note: Unrecognized properties will fail validation.
91 */
92export declare function object(): Struct<Record<string, unknown>, null>;
93export declare function object<S extends ObjectSchema>(schema: S): Struct<ObjectType<S>, S>;
94/**
95 * Augment a struct to allow `undefined` values.
96 */
97export declare function optional<T, S>(struct: Struct<T, S>): Struct<T | undefined, S>;
98/**
99 * Ensure that a value is an object with keys and values of specific types, but
100 * without ensuring any specific shape of properties.
101 *
102 * Like TypeScript's `Record` utility.
103 */
104export declare function record<K extends string, V>(Key: Struct<K>, Value: Struct<V>): Struct<Record<K, V>, null>;
105/**
106 * Ensure that a value is a `RegExp`.
107 *
108 * Note: this does not test the value against the regular expression! For that
109 * you need to use the `pattern()` refinement.
110 */
111export declare function regexp(): Struct<RegExp, null>;
112/**
113 * Ensure that a value is a `Set` object, and that its elements are of a
114 * specific type.
115 */
116export declare function set(): Struct<Set<unknown>, null>;
117export declare function set<T>(Element: Struct<T>): Struct<Set<T>, null>;
118/**
119 * Ensure that a value is a string.
120 */
121export declare function string(): Struct<string, null>;
122/**
123 * Ensure that a value is a tuple of a specific length, and that each of its
124 * elements is of a specific type.
125 */
126export declare function tuple<A extends AnyStruct, B extends AnyStruct[]>(Structs: [A, ...B]): Struct<[Infer<A>, ...InferStructTuple<B>], null>;
127/**
128 * Ensure that a value has a set of known properties of specific types.
129 *
130 * Note: Unrecognized properties are allowed and untouched. This is similar to
131 * how TypeScript's structural typing works.
132 */
133export declare function type<S extends ObjectSchema>(schema: S): Struct<ObjectType<S>, S>;
134/**
135 * Ensure that a value matches one of a set of types.
136 */
137export declare function union<A extends AnyStruct, B extends AnyStruct[]>(Structs: [A, ...B]): Struct<Infer<A> | InferStructTuple<B>[number], null>;
138/**
139 * Ensure that any value passes validation, without widening its type to `any`.
140 */
141export declare function unknown(): Struct<unknown, null>;
142//# sourceMappingURL=types.d.ts.map
\No newline at end of file