-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
schema.d.ts
114 lines (103 loc) · 2.93 KB
/
schema.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
Create a deep version of another object type where property values are recursively replaced into a given value type.
Use-cases:
- Form validation: Define how each field should be validated.
- Form settings: Define configuration for input fields.
- Parsing: Define types that specify special behavior for specific fields.
@example
```
import type {Schema} from 'type-fest';
interface User {
id: string;
name: {
firstname: string;
lastname: string;
};
created: Date;
active: boolean;
passwordHash: string;
attributes: ['Foo', 'Bar']
}
type UserMask = Schema<User, 'mask' | 'hide' | 'show'>;
const userMaskSettings: UserMask = {
id: 'show',
name: {
firstname: 'show',
lastname: 'mask',
},
created: 'show',
active: 'show',
passwordHash: 'hide',
attributes: ['mask', 'show']
}
```
@category Object
*/
export type Schema<ObjectType, ValueType, Options extends SchemaOptions = {}> = ObjectType extends string
? ValueType
: ObjectType extends Map<unknown, unknown>
? ValueType
: ObjectType extends Set<unknown>
? ValueType
: ObjectType extends ReadonlyMap<unknown, unknown>
? ValueType
: ObjectType extends ReadonlySet<unknown>
? ValueType
: ObjectType extends Array<infer U>
? Options['recurseIntoArrays'] extends false | undefined
? ValueType
: Array<Schema<U, ValueType>>
: ObjectType extends (...arguments_: unknown[]) => unknown
? ValueType
: ObjectType extends Date
? ValueType
: ObjectType extends Function
? ValueType
: ObjectType extends RegExp
? ValueType
: ObjectType extends object
? SchemaObject<ObjectType, ValueType, Options>
: ValueType;
/**
Same as `Schema`, but accepts only `object`s as inputs. Internal helper for `Schema`.
*/
type SchemaObject<
ObjectType extends object,
K,
Options extends SchemaOptions,
> = {
[KeyType in keyof ObjectType]: ObjectType[KeyType] extends
| readonly unknown[]
| unknown[]
? Options['recurseIntoArrays'] extends false | undefined
? K
: Schema<ObjectType[KeyType], K, Options>
: Schema<ObjectType[KeyType], K, Options> | K;
};
/**
@see Schema
*/
export type SchemaOptions = {
/**
By default, this affects elements in array and tuple types. You can change this by passing `{recurseIntoArrays: false}` as the third type argument:
- If `recurseIntoArrays` is set to `true` (default), array elements will be recursively processed as well.
- If `recurseIntoArrays` is set to `false`, arrays will not be recursively processed, and the entire array will be replaced with the given value type.
@example
```
type UserMask = Schema<User, 'mask' | 'hide' | 'show', {recurseIntoArrays: false}>;
const userMaskSettings: UserMask = {
id: 'show',
name: {
firstname: 'show',
lastname: 'mask',
},
created: 'show',
active: 'show',
passwordHash: 'hide',
attributes: 'hide'
}
```
@default true
*/
readonly recurseIntoArrays?: boolean | undefined;
};