-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.d.ts
142 lines (138 loc) · 3.81 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
declare function isString(x: unknown): x is string
declare function isNumber(x: unknown): x is number
declare function isBoolean(x: unknown): x is boolean
declare function isUndefined(x: unknown): x is undefined
declare function isNull(x: unknown): x is null
declare function isFunction(x: unknown): x is Function
declare function isObject(x: unknown): x is Record<string, unknown>
declare function isRegExp(x: unknown): x is RegExp
declare function isDate(x: unknown): x is Date
declare function isSymbol(x: unknown): x is Symbol
declare function isBigInt(x: unknown): x is BigInt
declare function isWindow(x: unknown): x is Window
declare function isPromise(x: unknown): x is Promise<unknown>
declare function isIterator(obj: unknown): obj is Iterator<unknown>
declare function isMap(obj: unknown): obj is Map<unknown, unknown>
declare function isSet(obj: unknown): obj is Set<unknown>
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never
type Guard<T> = ((x: unknown) => x is T) & {
__isOptional?: boolean
__isCircularRef?: boolean
}
type GuardFor<T> = T extends Guard<infer R> ? R : never
type GuardConfig = Record<PropertyKey, Guard<unknown>>
type MakeGuardFromConfig<T extends Record<PropertyKey, Guard<unknown>>> = (
value: unknown
) => value is MakeTypeFromConfig<T>
type MakeTypeFromConfig<T extends Record<PropertyKey, Guard<unknown>>> = handleMetadataIsSelf<
UnionToIntersection<handleMetadataIsOptional<T, keyof T>>
>
type handleMetadataIsOptional<T extends GuardConfig, K extends keyof T> = K extends K
? T[K] extends {
__isOptional: true
}
? {
[Key in keyof T as Key extends K ? Key : never]?: T[K]
}
: {
[Key in keyof T as Key extends K ? Key : never]: T[K]
}
: never
type handleMetadataIsSelf<T> = {
[K in keyof T]: T[K] extends {
__isCircularRef: true
}
? handleMetadataIsSelf<T>
: GuardFor<T[K]>
}
declare function toString(x: unknown): string
declare function objectOf<R extends Record<PropertyKey, Guard<unknown>>>(guardByProperty: R): MakeGuardFromConfig<R>
declare function arrayOf<T extends unknown[]>(
...guards: {
[K in keyof T]: Guard<T[K]>
}
): Guard<T>
declare function union<T extends any[]>(
...guards: {
[K in keyof T]: Guard<T[K]>
}
): (x: unknown) => x is T[number]
declare function intersection<T extends any[]>(
...guards: {
[K in keyof T]: Guard<T[K]>
}
): (x: unknown) => x is UnionToIntersection<T[number]>
declare function optional<T>(guard: Guard<T>): Guard<T> & {
__isOptional: true
}
declare const or: typeof union
declare const and: typeof intersection
declare const make: typeof objectOf
declare const SELF: Guard<any>
declare function hasUnknownProperty<K extends string>(
x: unknown,
name: K
): x is {
[Key in K]: unknown
}
declare function hasProperty<K extends string, V>(
x: unknown,
name: K,
guard: Guard<V>
): x is {
[Key in K]: V
}
declare function hasProperties<R extends GuardConfig>(x: unknown, guardByProperty: R): x is MakeTypeFromConfig<R>
declare function _hasProperty<K extends string, V, R extends GuardConfig>(
x: unknown,
name: K,
guard: Guard<V>,
memo?: Set<unknown>,
guardByProperty?: R
): x is {
[Key in K]: V
}
declare function _hasProperties<R extends GuardConfig>(
x: unknown,
guardByProperty: R,
memo?: Set<unknown>
): x is MakeTypeFromConfig<R>
export {
Guard,
GuardConfig,
GuardFor,
MakeGuardFromConfig,
MakeTypeFromConfig,
SELF,
UnionToIntersection,
_hasProperties,
_hasProperty,
and,
arrayOf,
hasProperties,
hasProperty,
hasUnknownProperty,
intersection,
isBigInt,
isBoolean,
isDate,
isFunction,
isIterator,
isMap,
isNull,
isNumber,
isObject,
isPromise,
isRegExp,
isSet,
isString,
isSymbol,
isUndefined,
isWindow,
make,
objectOf,
optional,
or,
toString,
union,
}