forked from woutervh-/typescript-is
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
295 lines (260 loc) · 8.03 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* Checks if the given argument is assignable to the given type-argument.
*
* @param object object whose type needs to be checked.
* @returns `true` if `object` is assignable to `T`, false otherwise.
* @example
```
is<number>(42); // -> true
is<number>('foo'); // -> false
```
*/
export function is<T>(object: any): object is T;
/**
* Creates a function similar to `is<T>` that can be invoked at a later point.
*
* This is useful, for example, if you want to re-use the function multiple times.
*
* @example
```
const checkNumber = createIs<number>();
checkNumber(42); // -> true
checkNumber('foo'); // -> false
```
*/
export function createIs<T>(): (object: any) => object is T;
/**
* Checks if the given argument is assignable to the given type-argument and vice versa.
* Superfluous properties will cause the validation to fail.
*
* @param object object whose type needs to be checked.
* @returns `true` if `object` is assignable to `T` and if `T` is "assignable" to `object`, false otherwise.
* @example
```
is<{ foo: string }>({}); // -> false
is<{ foo: string }>({ foo: 'bar' }); // -> true
is<{ foo: string }>({ foo: 'bar', baz: 'qux' }); // -> false
```
*/
export function equals<T>(object: any): object is T;
/**
* Creates a function similar to `equals<T>` that can be invoked at a later point.
*
* This is useful, for example, if you want to re-use the function multiple times.
*
* @example
```
const checkObject = createEquals<{ foo: string }>();
checkObject({}); // -> false
checkObject({ foo: 'bar' }); // -> true
checkObject({ foo: 'bar', baz: 'qux' }); // -> false
```
*/
export function createEquals<T>(): (object: any) => object is T;
/**
* Asserts the given argument to be assignable to the given type-argument.
* If the given argument is not assignable to the given type-argument, an error will be thrown.
*
* @param object object whose type will be asserted.
* @returns the given `object`, or an error is thrown if validation failed.
* @example
```
const safeNumber = assertType<number>(42); // safeNumber === 42, code continues
assertType<number>('foo'); // throws an error
```
*/
export function assertType<T>(object: any): T;
/**
* Creates a function similar to `assertType<T>` that can be invoked at a later point.
*
* This is useful, for example, if you want to re-use the function multiple times.
*
* @example
```
const assertNumber = createAssertType<number>();
const safeNumber = assertNumber(42); // safeNumber === 42, code continues
assertNumber('foo'); // throws an error
```
*/
export function createAssertType<T>(): (object: any) => T;
/**
* Asserts the given argument to be assignable to the given type-argument and vice versa.
* If the given argument is not assignable to the given type-argument, an error will be thrown.
* If the given type-argument is not assignable to the given argument, an error will be thrown.
* Superfluous properties will cause the validation to fail.
*
* @param object object whose type will be asserted.
* @returns the given `object`, or an error is thrown if validation failed.
* @example
```
const safeObject = assertEquals<{ foo: string }>({ foo: 'bar' }); // safeObject === { foo: 'bar' }, code continues
assertEquals<{ foo: string }>({ foo: 'bar', baz: 'qux' }); // throws an error
```
*/
export function assertEquals<T>(object: any): T;
/**
* Creates a function similar to `assertEquals<T>` that can be invoked at a later point.
*
* This is useful, for example, if you want to re-use the function multiple times.
*
* @example
```
const assertObject = createAssertEquals<{ foo: string }>();
const safeObject = assertObject({ foo: 'bar' }); // safeObject === { foo: 'bar' }, code continues
assertObject({ foo: 'bar', baz: 'qux' }); // throws an error
```
*/
export function createAssertEquals<T>(): (object: any) => T;
/**
* Creates a type assertion and saves it in the reflection metadata of the method's class.
* Then, when the class is decorated with `ValidateClass`, the method's arguments will be validated.
*
* @example
* ```
@ValidateClass()
class A { method(@AssertType() value: number) { value can safely be used a number } }
new A().method(0); // nothing happens
new A().method('0' as any); // will throw an error
```
*/
export function AssertType(options?: { async: boolean }): (target: object, propertyKey: string | symbol, parameterIndex: number) => void;
/**
* Overrides methods in the target class with a proxy that will first validate the argument types.
*
* @param errorConstructor a constructor of an `Error` class.
* This will be used to create an error when validation fails.
* @example
* ```
@ValidateClass()
class A { method(@AssertType() value: number) { value can safely be used a number } }
new A().method(0); // nothing happens
new A().method('0' as any); // will throw an error
```
*/
export function ValidateClass(errorConstructor?: { new(): Error }): <TFunction extends Function>(target: TFunction) => void;
/**
* Class which helps catch errors specifically from this library.
* When `assertType` or `createAssertType` throw an error, it uses this class to create an instance.
* By default, a class decorated with `@ValidateClass` will also throw errors of this class, unless it's overriden using the options.
*
* @example
* ```
// Somewhere in the code:
{
assertType<MyType>(obj);
}
// Somewhere higher up the call stack:
try {
...
} catch (error) {
if (error instanceof TypeGuardError) {
// An error from this library occurred.
}
}
```
*/
export class TypeGuardError extends Error {
public constructor(errorObject: { message: string, path: string[], reason: Reason }, inputObject: unknown);
public readonly path: string[];
public readonly reason: Reason;
public readonly input: unknown;
}
interface ExpectedFunction {
type: 'function';
}
interface ExpectedString {
type: 'string';
}
interface ExpectedNumber {
type: 'number';
}
interface ExpectedBigInt {
type: 'big-int';
}
interface ExpectedBoolean {
type: 'boolean';
}
interface ExpectedStringLiteral {
type: 'string-literal';
value: string;
}
interface ExpectedNumberLiteral {
type: 'number-literal';
value: number;
}
interface ExpectedBooleanLiteral {
type: 'boolean-literal';
value: boolean;
}
interface ExpectedObject {
type: 'object';
}
interface ExpectedDate {
type: 'date';
}
interface ExpectedNonPrimitive {
type: 'non-primitive';
}
interface MissingObjectProperty {
type: 'missing-property';
property: string;
}
interface SuperfluousObjectProperty {
type: 'superfluous-property';
}
interface ExpectedObjectKeyof {
type: 'object-keyof';
properties: string[];
}
interface ExpectedArray {
type: 'array';
}
interface NeverType {
type: 'never';
}
interface ExpectedTuple {
type: 'tuple';
minLength: number;
maxLength: number;
}
interface NoValidUnionAlternatives {
type: 'union';
}
interface ExpectedUndefined {
type: 'undefined';
}
interface ExpectedNull {
type: 'null';
}
type TemplateLiteralPair = [string, 'string' | 'number' | 'bigint' | 'any' | 'undefined' | 'null' | undefined];
interface ExpectedTemplateLiteral {
type: 'template-literal'
value: TemplateLiteralPair[]
}
type Reason = ExpectedFunction
| ExpectedString
| ExpectedNumber
| ExpectedBigInt
| ExpectedBoolean
| ExpectedObject
| ExpectedDate
| ExpectedNonPrimitive
| MissingObjectProperty
| SuperfluousObjectProperty
| ExpectedObjectKeyof
| ExpectedArray
| ExpectedTuple
| NeverType
| NoValidUnionAlternatives
| ExpectedUndefined
| ExpectedNull
| ExpectedStringLiteral
| ExpectedNumberLiteral
| ExpectedBooleanLiteral
| ExpectedTemplateLiteral;
/**
* Set default getErrorObject function used for transpiled source.
*
* @param getErrorObject
*/
export function setDefaultGetErrorObject(getErrorObject?: () => { message: string, path: string[], reason: Reason } | null): void;