@tesouro/embedded-components-loader 0.11.248 → 0.11.250
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/react/experimental/BalanceHistoryWidget.d.ts +394 -181
- package/dist/react/experimental/index.d.ts +394 -181
- package/dist/react/index.d.ts +394 -181
- package/dist/react/lib/AcceptDisclosuresWidget.d.ts +394 -181
- package/dist/react/lib/BankAccountOnboardingWidget.d.ts +394 -181
- package/dist/react/lib/CardDetailsWidget.d.ts +394 -181
- package/dist/react/lib/CounterpartsWidget.d.ts +394 -181
- package/dist/react/lib/CounterpartsWidgetPublic.d.ts +394 -181
- package/dist/react/lib/CreditCardsWidget.d.ts +394 -181
- package/dist/react/lib/DebitCardsWidget.d.ts +394 -181
- package/dist/react/lib/OfferWidget.d.ts +394 -181
- package/dist/react/lib/ReceivablesWidget.d.ts +394 -181
- package/dist/react/lib/WidgetSuite.d.ts +394 -181
- package/package.json +1 -1
|
@@ -20,13 +20,84 @@ type Primitive =
|
|
|
20
20
|
| symbol
|
|
21
21
|
| bigint;
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
/**
|
|
24
|
+
Returns a boolean for whether the given type is `any`.
|
|
25
|
+
|
|
26
|
+
@link https://stackoverflow.com/a/49928360/1490091
|
|
27
|
+
|
|
28
|
+
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
29
|
+
|
|
30
|
+
@example
|
|
31
|
+
```
|
|
32
|
+
import type {IsAny} from 'type-fest';
|
|
33
|
+
|
|
34
|
+
const typedObject = {a: 1, b: 2} as const;
|
|
35
|
+
const anyObject: any = {a: 1, b: 2};
|
|
36
|
+
|
|
37
|
+
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(object: O, key: K) {
|
|
38
|
+
return object[key];
|
|
28
39
|
}
|
|
29
40
|
|
|
41
|
+
const typedA = get(typedObject, 'a');
|
|
42
|
+
//=> 1
|
|
43
|
+
|
|
44
|
+
const anyA = get(anyObject, 'a');
|
|
45
|
+
//=> any
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
@category Type Guard
|
|
49
|
+
@category Utilities
|
|
50
|
+
*/
|
|
51
|
+
type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
Returns a boolean for whether the given key is an optional key of type.
|
|
55
|
+
|
|
56
|
+
This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
|
|
57
|
+
|
|
58
|
+
@example
|
|
59
|
+
```
|
|
60
|
+
import type {IsOptionalKeyOf} from 'type-fest';
|
|
61
|
+
|
|
62
|
+
type User = {
|
|
63
|
+
name: string;
|
|
64
|
+
surname: string;
|
|
65
|
+
|
|
66
|
+
luckyNumber?: number;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
type Admin = {
|
|
70
|
+
name: string;
|
|
71
|
+
surname?: string;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
|
|
75
|
+
//=> true
|
|
76
|
+
|
|
77
|
+
type T2 = IsOptionalKeyOf<User, 'name'>;
|
|
78
|
+
//=> false
|
|
79
|
+
|
|
80
|
+
type T3 = IsOptionalKeyOf<User, 'name' | 'luckyNumber'>;
|
|
81
|
+
//=> boolean
|
|
82
|
+
|
|
83
|
+
type T4 = IsOptionalKeyOf<User | Admin, 'name'>;
|
|
84
|
+
//=> false
|
|
85
|
+
|
|
86
|
+
type T5 = IsOptionalKeyOf<User | Admin, 'surname'>;
|
|
87
|
+
//=> boolean
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
@category Type Guard
|
|
91
|
+
@category Utilities
|
|
92
|
+
*/
|
|
93
|
+
type IsOptionalKeyOf<Type extends object, Key extends keyof Type> =
|
|
94
|
+
IsAny<Type | Key> extends true ? never
|
|
95
|
+
: Key extends keyof Type
|
|
96
|
+
? Type extends Record<Key, Type[Key]>
|
|
97
|
+
? false
|
|
98
|
+
: true
|
|
99
|
+
: false;
|
|
100
|
+
|
|
30
101
|
/**
|
|
31
102
|
Extract all optional keys from the given type.
|
|
32
103
|
|
|
@@ -36,12 +107,12 @@ This is useful when you want to create a new type that contains different type v
|
|
|
36
107
|
```
|
|
37
108
|
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
38
109
|
|
|
39
|
-
|
|
110
|
+
type User = {
|
|
40
111
|
name: string;
|
|
41
112
|
surname: string;
|
|
42
113
|
|
|
43
114
|
luckyNumber?: number;
|
|
44
|
-
}
|
|
115
|
+
};
|
|
45
116
|
|
|
46
117
|
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
47
118
|
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
@@ -49,22 +120,25 @@ type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKe
|
|
|
49
120
|
};
|
|
50
121
|
|
|
51
122
|
const update1: UpdateOperation<User> = {
|
|
52
|
-
name: 'Alice'
|
|
123
|
+
name: 'Alice',
|
|
53
124
|
};
|
|
54
125
|
|
|
55
126
|
const update2: UpdateOperation<User> = {
|
|
56
127
|
name: 'Bob',
|
|
57
|
-
luckyNumber: REMOVE_FIELD
|
|
128
|
+
luckyNumber: REMOVE_FIELD,
|
|
58
129
|
};
|
|
59
130
|
```
|
|
60
131
|
|
|
61
132
|
@category Utilities
|
|
62
133
|
*/
|
|
63
|
-
type OptionalKeysOf<
|
|
64
|
-
|
|
65
|
-
? (keyof {
|
|
66
|
-
|
|
67
|
-
|
|
134
|
+
type OptionalKeysOf<Type extends object> =
|
|
135
|
+
Type extends unknown // For distributing `Type`
|
|
136
|
+
? (keyof {[Key in keyof Type as
|
|
137
|
+
IsOptionalKeyOf<Type, Key> extends false
|
|
138
|
+
? never
|
|
139
|
+
: Key
|
|
140
|
+
]: never
|
|
141
|
+
}) & keyof Type // Intersect with `keyof Type` to ensure result of `OptionalKeysOf<Type>` is always assignable to `keyof Type`
|
|
68
142
|
: never; // Should never happen
|
|
69
143
|
|
|
70
144
|
/**
|
|
@@ -76,24 +150,30 @@ This is useful when you want to create a new type that contains different type v
|
|
|
76
150
|
```
|
|
77
151
|
import type {RequiredKeysOf} from 'type-fest';
|
|
78
152
|
|
|
79
|
-
declare function createValidation<
|
|
153
|
+
declare function createValidation<
|
|
154
|
+
Entity extends object,
|
|
155
|
+
Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>,
|
|
156
|
+
>(field: Key, validator: (value: Entity[Key]) => boolean): (entity: Entity) => boolean;
|
|
80
157
|
|
|
81
|
-
|
|
158
|
+
type User = {
|
|
82
159
|
name: string;
|
|
83
160
|
surname: string;
|
|
84
|
-
|
|
85
161
|
luckyNumber?: number;
|
|
86
|
-
}
|
|
162
|
+
};
|
|
87
163
|
|
|
88
164
|
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
89
165
|
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
166
|
+
|
|
167
|
+
// @ts-expect-error
|
|
168
|
+
const validator3 = createValidation<User>('luckyNumber', value => value > 0);
|
|
169
|
+
// Error: Argument of type '"luckyNumber"' is not assignable to parameter of type '"name" | "surname"'.
|
|
90
170
|
```
|
|
91
171
|
|
|
92
172
|
@category Utilities
|
|
93
173
|
*/
|
|
94
|
-
type RequiredKeysOf<
|
|
95
|
-
|
|
96
|
-
? Exclude<keyof
|
|
174
|
+
type RequiredKeysOf<Type extends object> =
|
|
175
|
+
Type extends unknown // For distributing `Type`
|
|
176
|
+
? Exclude<keyof Type, OptionalKeysOf<Type>>
|
|
97
177
|
: never; // Should never happen
|
|
98
178
|
|
|
99
179
|
/**
|
|
@@ -109,29 +189,41 @@ Useful in type utilities, such as checking if something does not occur.
|
|
|
109
189
|
```
|
|
110
190
|
import type {IsNever, And} from 'type-fest';
|
|
111
191
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
And<
|
|
115
|
-
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
116
|
-
IsNever<Exclude<B, A>> extends true ? true : false
|
|
117
|
-
>;
|
|
192
|
+
type A = IsNever<never>;
|
|
193
|
+
//=> true
|
|
118
194
|
|
|
119
|
-
type
|
|
120
|
-
|
|
121
|
-
? never
|
|
122
|
-
: void;
|
|
195
|
+
type B = IsNever<any>;
|
|
196
|
+
//=> false
|
|
123
197
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
198
|
+
type C = IsNever<unknown>;
|
|
199
|
+
//=> false
|
|
200
|
+
|
|
201
|
+
type D = IsNever<never[]>;
|
|
202
|
+
//=> false
|
|
203
|
+
|
|
204
|
+
type E = IsNever<object>;
|
|
205
|
+
//=> false
|
|
206
|
+
|
|
207
|
+
type F = IsNever<string>;
|
|
208
|
+
//=> false
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
@example
|
|
212
|
+
```
|
|
213
|
+
import type {IsNever} from 'type-fest';
|
|
214
|
+
|
|
215
|
+
type IsTrue<T> = T extends true ? true : false;
|
|
129
216
|
|
|
130
|
-
|
|
217
|
+
// When a distributive conditional is instantiated with `never`, the entire conditional results in `never`.
|
|
218
|
+
type A = IsTrue<never>;
|
|
131
219
|
//=> never
|
|
132
220
|
|
|
133
|
-
|
|
134
|
-
|
|
221
|
+
// If you don't want that behaviour, you can explicitly add an `IsNever` check before the distributive conditional.
|
|
222
|
+
type IsTrueFixed<T> =
|
|
223
|
+
IsNever<T> extends true ? false : T extends true ? true : false;
|
|
224
|
+
|
|
225
|
+
type B = IsTrueFixed<never>;
|
|
226
|
+
//=> false
|
|
135
227
|
```
|
|
136
228
|
|
|
137
229
|
@category Type Guard
|
|
@@ -140,61 +232,125 @@ endIfEqual('abc', '123');
|
|
|
140
232
|
type IsNever<T> = [T] extends [never] ? true : false;
|
|
141
233
|
|
|
142
234
|
/**
|
|
143
|
-
An if-else-like type that resolves depending on whether the given type is `
|
|
235
|
+
An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
|
|
236
|
+
|
|
237
|
+
Use-cases:
|
|
238
|
+
- You can use this in combination with `Is*` types to create an if-else-like experience. For example, `If<IsAny<any>, 'is any', 'not any'>`.
|
|
144
239
|
|
|
145
|
-
|
|
240
|
+
Note:
|
|
241
|
+
- Returns a union of if branch and else branch if the given type is `boolean` or `any`. For example, `If<boolean, 'Y', 'N'>` will return `'Y' | 'N'`.
|
|
242
|
+
- Returns the else branch if the given type is `never`. For example, `If<never, 'Y', 'N'>` will return `'N'`.
|
|
146
243
|
|
|
147
244
|
@example
|
|
148
245
|
```
|
|
149
|
-
import type {
|
|
246
|
+
import type {If} from 'type-fest';
|
|
150
247
|
|
|
151
|
-
type
|
|
152
|
-
//=>
|
|
248
|
+
type A = If<true, 'yes', 'no'>;
|
|
249
|
+
//=> 'yes'
|
|
153
250
|
|
|
154
|
-
type
|
|
155
|
-
//=> '
|
|
156
|
-
```
|
|
251
|
+
type B = If<false, 'yes', 'no'>;
|
|
252
|
+
//=> 'no'
|
|
157
253
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
*/
|
|
161
|
-
type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
|
|
162
|
-
IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
|
|
163
|
-
);
|
|
254
|
+
type C = If<boolean, 'yes', 'no'>;
|
|
255
|
+
//=> 'yes' | 'no'
|
|
164
256
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
type NoInfer<T> = T extends infer U ? U : never;
|
|
257
|
+
type D = If<any, 'yes', 'no'>;
|
|
258
|
+
//=> 'yes' | 'no'
|
|
168
259
|
|
|
169
|
-
|
|
170
|
-
|
|
260
|
+
type E = If<never, 'yes', 'no'>;
|
|
261
|
+
//=> 'no'
|
|
262
|
+
```
|
|
171
263
|
|
|
172
|
-
@
|
|
264
|
+
@example
|
|
265
|
+
```
|
|
266
|
+
import type {If, IsAny, IsNever} from 'type-fest';
|
|
173
267
|
|
|
174
|
-
|
|
268
|
+
type A = If<IsAny<unknown>, 'is any', 'not any'>;
|
|
269
|
+
//=> 'not any'
|
|
270
|
+
|
|
271
|
+
type B = If<IsNever<never>, 'is never', 'not never'>;
|
|
272
|
+
//=> 'is never'
|
|
273
|
+
```
|
|
175
274
|
|
|
176
275
|
@example
|
|
177
276
|
```
|
|
178
|
-
import type {
|
|
277
|
+
import type {If, IsEqual} from 'type-fest';
|
|
179
278
|
|
|
180
|
-
|
|
181
|
-
const anyObject: any = {a: 1, b: 2};
|
|
279
|
+
type IfEqual<T, U, IfBranch, ElseBranch> = If<IsEqual<T, U>, IfBranch, ElseBranch>;
|
|
182
280
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
281
|
+
type A = IfEqual<string, string, 'equal', 'not equal'>;
|
|
282
|
+
//=> 'equal'
|
|
186
283
|
|
|
187
|
-
|
|
188
|
-
//=>
|
|
284
|
+
type B = IfEqual<string, number, 'equal', 'not equal'>;
|
|
285
|
+
//=> 'not equal'
|
|
286
|
+
```
|
|
189
287
|
|
|
190
|
-
|
|
191
|
-
|
|
288
|
+
Note: Sometimes using the `If` type can make an implementation non–tail-recursive, which can impact performance. In such cases, it’s better to use a conditional directly. Refer to the following example:
|
|
289
|
+
|
|
290
|
+
@example
|
|
291
|
+
```
|
|
292
|
+
import type {If, IsEqual, StringRepeat} from 'type-fest';
|
|
293
|
+
|
|
294
|
+
type HundredZeroes = StringRepeat<'0', 100>;
|
|
295
|
+
|
|
296
|
+
// The following implementation is not tail recursive
|
|
297
|
+
type Includes<S extends string, Char extends string> =
|
|
298
|
+
S extends `${infer First}${infer Rest}`
|
|
299
|
+
? If<IsEqual<First, Char>,
|
|
300
|
+
'found',
|
|
301
|
+
Includes<Rest, Char>>
|
|
302
|
+
: 'not found';
|
|
303
|
+
|
|
304
|
+
// Hence, instantiations with long strings will fail
|
|
305
|
+
// @ts-expect-error
|
|
306
|
+
type Fails = Includes<HundredZeroes, '1'>;
|
|
307
|
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
308
|
+
// Error: Type instantiation is excessively deep and possibly infinite.
|
|
309
|
+
|
|
310
|
+
// However, if we use a simple conditional instead of `If`, the implementation becomes tail-recursive
|
|
311
|
+
type IncludesWithoutIf<S extends string, Char extends string> =
|
|
312
|
+
S extends `${infer First}${infer Rest}`
|
|
313
|
+
? IsEqual<First, Char> extends true
|
|
314
|
+
? 'found'
|
|
315
|
+
: IncludesWithoutIf<Rest, Char>
|
|
316
|
+
: 'not found';
|
|
317
|
+
|
|
318
|
+
// Now, instantiations with long strings will work
|
|
319
|
+
type Works = IncludesWithoutIf<HundredZeroes, '1'>;
|
|
320
|
+
//=> 'not found'
|
|
192
321
|
```
|
|
193
322
|
|
|
194
323
|
@category Type Guard
|
|
195
324
|
@category Utilities
|
|
196
325
|
*/
|
|
197
|
-
type
|
|
326
|
+
type If<Type extends boolean, IfBranch, ElseBranch> =
|
|
327
|
+
IsNever<Type> extends true
|
|
328
|
+
? ElseBranch
|
|
329
|
+
: Type extends true
|
|
330
|
+
? IfBranch
|
|
331
|
+
: ElseBranch;
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
335
|
+
*/
|
|
336
|
+
type BuiltIns = Primitive | void | Date | RegExp;
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
Test if the given function has multiple call signatures.
|
|
340
|
+
|
|
341
|
+
Needed to handle the case of a single call signature with properties.
|
|
342
|
+
|
|
343
|
+
Multiple call signatures cannot currently be supported due to a TypeScript limitation.
|
|
344
|
+
@see https://github.com/microsoft/TypeScript/issues/29732
|
|
345
|
+
*/
|
|
346
|
+
type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
|
|
347
|
+
T extends {(...arguments_: infer A): unknown; (...arguments_: infer B): unknown}
|
|
348
|
+
? B extends A
|
|
349
|
+
? A extends B
|
|
350
|
+
? false
|
|
351
|
+
: true
|
|
352
|
+
: true
|
|
353
|
+
: false;
|
|
198
354
|
|
|
199
355
|
/**
|
|
200
356
|
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
@@ -241,20 +397,61 @@ const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
|
241
397
|
const someType: SomeType = literal;
|
|
242
398
|
const someInterface: SomeInterface = literal;
|
|
243
399
|
|
|
244
|
-
function fn(object: Record<string, unknown>): void
|
|
400
|
+
declare function fn(object: Record<string, unknown>): void;
|
|
245
401
|
|
|
246
402
|
fn(literal); // Good: literal object type is sealed
|
|
247
403
|
fn(someType); // Good: type is sealed
|
|
404
|
+
// @ts-expect-error
|
|
248
405
|
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
249
406
|
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
250
407
|
```
|
|
251
408
|
|
|
252
409
|
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
253
|
-
@see SimplifyDeep
|
|
410
|
+
@see {@link SimplifyDeep}
|
|
254
411
|
@category Object
|
|
255
412
|
*/
|
|
256
413
|
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
257
414
|
|
|
415
|
+
/**
|
|
416
|
+
Returns a boolean for whether the two given types are equal.
|
|
417
|
+
|
|
418
|
+
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
419
|
+
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
420
|
+
|
|
421
|
+
Use-cases:
|
|
422
|
+
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
423
|
+
|
|
424
|
+
@example
|
|
425
|
+
```
|
|
426
|
+
import type {IsEqual} from 'type-fest';
|
|
427
|
+
|
|
428
|
+
// This type returns a boolean for whether the given array includes the given item.
|
|
429
|
+
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
430
|
+
type Includes<Value extends readonly any[], Item> =
|
|
431
|
+
Value extends readonly [Value[0], ...infer rest]
|
|
432
|
+
? IsEqual<Value[0], Item> extends true
|
|
433
|
+
? true
|
|
434
|
+
: Includes<rest, Item>
|
|
435
|
+
: false;
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
@category Type Guard
|
|
439
|
+
@category Utilities
|
|
440
|
+
*/
|
|
441
|
+
type IsEqual<A, B> =
|
|
442
|
+
[A] extends [B]
|
|
443
|
+
? [B] extends [A]
|
|
444
|
+
? _IsEqual<A, B>
|
|
445
|
+
: false
|
|
446
|
+
: false;
|
|
447
|
+
|
|
448
|
+
// This version fails the `equalWrappedTupleIntersectionToBeNeverAndNeverExpanded` test in `test-d/is-equal.ts`.
|
|
449
|
+
type _IsEqual<A, B> =
|
|
450
|
+
(<G>() => G extends A & G | G ? 1 : 2) extends
|
|
451
|
+
(<G>() => G extends B & G | G ? 1 : 2)
|
|
452
|
+
? true
|
|
453
|
+
: false;
|
|
454
|
+
|
|
258
455
|
/**
|
|
259
456
|
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
260
457
|
|
|
@@ -272,8 +469,9 @@ It relies on the fact that an empty object (`{}`) is assignable to an object wit
|
|
|
272
469
|
```
|
|
273
470
|
const indexed: Record<string, unknown> = {}; // Allowed
|
|
274
471
|
|
|
472
|
+
// @ts-expect-error
|
|
275
473
|
const keyed: Record<'foo', unknown> = {}; // Error
|
|
276
|
-
//
|
|
474
|
+
// TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
277
475
|
```
|
|
278
476
|
|
|
279
477
|
Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
|
|
@@ -282,19 +480,21 @@ Instead of causing a type error like the above, you can also use a [conditional
|
|
|
282
480
|
type Indexed = {} extends Record<string, unknown>
|
|
283
481
|
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
284
482
|
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
285
|
-
|
|
483
|
+
|
|
484
|
+
type IndexedResult = Indexed;
|
|
485
|
+
//=> '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
286
486
|
|
|
287
487
|
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
288
|
-
?
|
|
289
|
-
:
|
|
290
|
-
|
|
488
|
+
? '✅ `{}` is assignable to `Record<\'foo\' | \'bar\', unknown>`'
|
|
489
|
+
: '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`';
|
|
490
|
+
|
|
491
|
+
type KeyedResult = Keyed;
|
|
492
|
+
//=> '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`'
|
|
291
493
|
```
|
|
292
494
|
|
|
293
495
|
Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
|
|
294
496
|
|
|
295
497
|
```
|
|
296
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
297
|
-
|
|
298
498
|
type OmitIndexSignature<ObjectType> = {
|
|
299
499
|
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
300
500
|
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
@@ -304,14 +504,12 @@ type OmitIndexSignature<ObjectType> = {
|
|
|
304
504
|
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
305
505
|
|
|
306
506
|
```
|
|
307
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
308
|
-
|
|
309
507
|
type OmitIndexSignature<ObjectType> = {
|
|
310
508
|
[KeyType in keyof ObjectType
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
509
|
+
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
510
|
+
as {} extends Record<KeyType, unknown>
|
|
511
|
+
? never // ✅ `{}` is assignable to `Record<KeyType, unknown>`
|
|
512
|
+
: KeyType // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
|
|
315
513
|
]: ObjectType[KeyType];
|
|
316
514
|
};
|
|
317
515
|
```
|
|
@@ -322,27 +520,27 @@ If `{}` is assignable, it means that `KeyType` is an index signature and we want
|
|
|
322
520
|
```
|
|
323
521
|
import type {OmitIndexSignature} from 'type-fest';
|
|
324
522
|
|
|
325
|
-
|
|
523
|
+
type Example = {
|
|
326
524
|
// These index signatures will be removed.
|
|
327
|
-
[x: string]: any
|
|
328
|
-
[x: number]: any
|
|
329
|
-
[x: symbol]: any
|
|
330
|
-
[x: `head-${string}`]: string
|
|
331
|
-
[x: `${string}-tail`]: string
|
|
332
|
-
[x: `head-${string}-tail`]: string
|
|
333
|
-
[x: `${bigint}`]: string
|
|
334
|
-
[x: `embedded-${number}`]: string
|
|
525
|
+
[x: string]: any;
|
|
526
|
+
[x: number]: any;
|
|
527
|
+
[x: symbol]: any;
|
|
528
|
+
[x: `head-${string}`]: string;
|
|
529
|
+
[x: `${string}-tail`]: string;
|
|
530
|
+
[x: `head-${string}-tail`]: string;
|
|
531
|
+
[x: `${bigint}`]: string;
|
|
532
|
+
[x: `embedded-${number}`]: string;
|
|
335
533
|
|
|
336
534
|
// These explicitly defined keys will remain.
|
|
337
535
|
foo: 'bar';
|
|
338
536
|
qux?: 'baz';
|
|
339
|
-
}
|
|
537
|
+
};
|
|
340
538
|
|
|
341
539
|
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
342
|
-
|
|
540
|
+
//=> {foo: 'bar'; qux?: 'baz'}
|
|
343
541
|
```
|
|
344
542
|
|
|
345
|
-
@see PickIndexSignature
|
|
543
|
+
@see {@link PickIndexSignature}
|
|
346
544
|
@category Object
|
|
347
545
|
*/
|
|
348
546
|
type OmitIndexSignature<ObjectType> = {
|
|
@@ -393,7 +591,7 @@ type ExampleIndexSignature = PickIndexSignature<Example>;
|
|
|
393
591
|
// }
|
|
394
592
|
```
|
|
395
593
|
|
|
396
|
-
@see OmitIndexSignature
|
|
594
|
+
@see {@link OmitIndexSignature}
|
|
397
595
|
@category Object
|
|
398
596
|
*/
|
|
399
597
|
type PickIndexSignature<ObjectType> = {
|
|
@@ -403,23 +601,48 @@ type PickIndexSignature<ObjectType> = {
|
|
|
403
601
|
};
|
|
404
602
|
|
|
405
603
|
// Merges two objects without worrying about index signatures.
|
|
406
|
-
type SimpleMerge<Destination, Source> = {
|
|
604
|
+
type SimpleMerge<Destination, Source> = Simplify<{
|
|
407
605
|
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
|
408
|
-
} & Source
|
|
606
|
+
} & Source>;
|
|
409
607
|
|
|
410
608
|
/**
|
|
411
609
|
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
412
610
|
|
|
611
|
+
This is different from the TypeScript `&` (intersection) operator. With `&`, conflicting property types are intersected, which often results in `never`. For example, `{a: string} & {a: number}` makes `a` become `string & number`, which resolves to `never`. With `Merge`, the second type's keys cleanly override the first, so `Merge<{a: string}, {a: number}>` gives `{a: number}` as expected. `Merge` also produces a flattened type (via `Simplify`), making it more readable in IDE tooltips compared to `A & B`.
|
|
612
|
+
|
|
413
613
|
@example
|
|
414
614
|
```
|
|
415
615
|
import type {Merge} from 'type-fest';
|
|
416
616
|
|
|
417
|
-
|
|
617
|
+
type Foo = {
|
|
618
|
+
a: string;
|
|
619
|
+
b: number;
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
type Bar = {
|
|
623
|
+
a: number; // Conflicts with Foo['a']
|
|
624
|
+
c: boolean;
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
// With `&`, `a` becomes `string & number` which is `never`. Not what you want.
|
|
628
|
+
type WithIntersection = (Foo & Bar)['a'];
|
|
629
|
+
//=> never
|
|
630
|
+
|
|
631
|
+
// With `Merge`, `a` is cleanly overridden to `number`.
|
|
632
|
+
type WithMerge = Merge<Foo, Bar>['a'];
|
|
633
|
+
//=> number
|
|
634
|
+
```
|
|
635
|
+
|
|
636
|
+
@example
|
|
637
|
+
```
|
|
638
|
+
import type {Merge} from 'type-fest';
|
|
639
|
+
|
|
640
|
+
type Foo = {
|
|
418
641
|
[x: string]: unknown;
|
|
419
642
|
[x: number]: unknown;
|
|
420
643
|
foo: string;
|
|
421
644
|
bar: symbol;
|
|
422
|
-
}
|
|
645
|
+
};
|
|
423
646
|
|
|
424
647
|
type Bar = {
|
|
425
648
|
[x: number]: number;
|
|
@@ -429,7 +652,7 @@ type Bar = {
|
|
|
429
652
|
};
|
|
430
653
|
|
|
431
654
|
export type FooBar = Merge<Foo, Bar>;
|
|
432
|
-
|
|
655
|
+
//=> {
|
|
433
656
|
// [x: string]: unknown;
|
|
434
657
|
// [x: number]: number;
|
|
435
658
|
// [x: symbol]: unknown;
|
|
@@ -439,41 +662,23 @@ export type FooBar = Merge<Foo, Bar>;
|
|
|
439
662
|
// }
|
|
440
663
|
```
|
|
441
664
|
|
|
665
|
+
Note: If you want a merge type that more accurately reflects the runtime behavior of object spread or `Object.assign`, refer to the {@link ObjectMerge} type.
|
|
666
|
+
|
|
667
|
+
@see {@link ObjectMerge}
|
|
442
668
|
@category Object
|
|
443
669
|
*/
|
|
444
670
|
type Merge<Destination, Source> =
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
An if-else-like type that resolves depending on whether the given type is `any`.
|
|
452
|
-
|
|
453
|
-
@see {@link IsAny}
|
|
454
|
-
|
|
455
|
-
@example
|
|
456
|
-
```
|
|
457
|
-
import type {IfAny} from 'type-fest';
|
|
458
|
-
|
|
459
|
-
type ShouldBeTrue = IfAny<any>;
|
|
460
|
-
//=> true
|
|
461
|
-
|
|
462
|
-
type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
|
|
463
|
-
//=> 'bar'
|
|
464
|
-
```
|
|
465
|
-
|
|
466
|
-
@category Type Guard
|
|
467
|
-
@category Utilities
|
|
468
|
-
*/
|
|
469
|
-
type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
|
|
470
|
-
IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
|
|
471
|
-
);
|
|
671
|
+
Destination extends unknown // For distributing `Destination`
|
|
672
|
+
? Source extends unknown // For distributing `Source`
|
|
673
|
+
? If<IsEqual<Destination, Source>, Destination, _Merge<Destination, Source>>
|
|
674
|
+
: never // Should never happen
|
|
675
|
+
: never; // Should never happen
|
|
472
676
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
677
|
+
type _Merge<Destination, Source> =
|
|
678
|
+
Simplify<
|
|
679
|
+
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
680
|
+
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
681
|
+
>;
|
|
477
682
|
|
|
478
683
|
/**
|
|
479
684
|
Merges user specified options with default options.
|
|
@@ -532,18 +737,22 @@ type ApplyDefaultOptions<
|
|
|
532
737
|
Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
|
|
533
738
|
SpecifiedOptions extends Options,
|
|
534
739
|
> =
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
740
|
+
_ApplyDefaultOptions<Options, Defaults, SpecifiedOptions> extends infer Result extends Required<Options> // `extends Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
|
|
741
|
+
? Result
|
|
742
|
+
: never;
|
|
743
|
+
|
|
744
|
+
type _ApplyDefaultOptions<
|
|
745
|
+
Options,
|
|
746
|
+
Defaults,
|
|
747
|
+
SpecifiedOptions,
|
|
748
|
+
> =
|
|
749
|
+
If<IsAny<SpecifiedOptions>, Defaults,
|
|
750
|
+
If<IsNever<SpecifiedOptions>, Defaults,
|
|
751
|
+
Merge<Defaults, {
|
|
752
|
+
[Key in keyof SpecifiedOptions
|
|
753
|
+
as undefined extends Required<Options>[Key & keyof Options] ? Key : undefined extends SpecifiedOptions[Key] ? never : Key
|
|
754
|
+
]: SpecifiedOptions[Key]
|
|
755
|
+
}>>>;
|
|
547
756
|
|
|
548
757
|
/**
|
|
549
758
|
@see {@link PartialDeep}
|
|
@@ -562,10 +771,10 @@ type PartialDeepOptions = {
|
|
|
562
771
|
- When set to `true`, elements of non-tuple arrays can be `undefined`.
|
|
563
772
|
- When set to `false`, only explicitly defined elements are allowed in non-tuple arrays, ensuring stricter type checking.
|
|
564
773
|
|
|
565
|
-
@default
|
|
774
|
+
@default false
|
|
566
775
|
|
|
567
776
|
@example
|
|
568
|
-
You can
|
|
777
|
+
You can allow `undefined` values in non-tuple arrays by passing `{recurseIntoArrays: true; allowUndefinedInNonTupleArrays: true}` as the second type argument:
|
|
569
778
|
|
|
570
779
|
```
|
|
571
780
|
import type {PartialDeep} from 'type-fest';
|
|
@@ -574,10 +783,9 @@ type PartialDeepOptions = {
|
|
|
574
783
|
languages: string[];
|
|
575
784
|
};
|
|
576
785
|
|
|
577
|
-
declare const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true; allowUndefinedInNonTupleArrays:
|
|
786
|
+
declare const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true; allowUndefinedInNonTupleArrays: true}>;
|
|
578
787
|
|
|
579
|
-
partialSettings.languages = [undefined]; //
|
|
580
|
-
partialSettings.languages = []; // Ok
|
|
788
|
+
partialSettings.languages = [undefined]; // OK
|
|
581
789
|
```
|
|
582
790
|
*/
|
|
583
791
|
readonly allowUndefinedInNonTupleArrays?: boolean;
|
|
@@ -585,33 +793,35 @@ type PartialDeepOptions = {
|
|
|
585
793
|
|
|
586
794
|
type DefaultPartialDeepOptions = {
|
|
587
795
|
recurseIntoArrays: false;
|
|
588
|
-
allowUndefinedInNonTupleArrays:
|
|
796
|
+
allowUndefinedInNonTupleArrays: false;
|
|
589
797
|
};
|
|
590
798
|
|
|
591
799
|
/**
|
|
592
|
-
Create a
|
|
800
|
+
Create a deeply optional version of another type.
|
|
593
801
|
|
|
594
802
|
Use-cases:
|
|
595
803
|
- Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
|
|
596
804
|
- Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
|
|
597
805
|
|
|
806
|
+
Use [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) if you only need one level deep.
|
|
807
|
+
|
|
598
808
|
@example
|
|
599
809
|
```
|
|
600
810
|
import type {PartialDeep} from 'type-fest';
|
|
601
811
|
|
|
602
|
-
|
|
812
|
+
let settings = {
|
|
603
813
|
textEditor: {
|
|
604
814
|
fontSize: 14,
|
|
605
815
|
fontColor: '#000000',
|
|
606
|
-
fontWeight: 400
|
|
816
|
+
fontWeight: 400,
|
|
607
817
|
},
|
|
608
818
|
autocomplete: false,
|
|
609
|
-
autosave: true
|
|
819
|
+
autosave: true,
|
|
610
820
|
};
|
|
611
821
|
|
|
612
|
-
const applySavedSettings = (savedSettings: PartialDeep<
|
|
613
|
-
|
|
614
|
-
|
|
822
|
+
const applySavedSettings = (savedSettings: PartialDeep<typeof settings>) => (
|
|
823
|
+
{...settings, ...savedSettings, textEditor: {...settings.textEditor, ...savedSettings.textEditor}}
|
|
824
|
+
);
|
|
615
825
|
|
|
616
826
|
settings = applySavedSettings({textEditor: {fontWeight: 500}});
|
|
617
827
|
```
|
|
@@ -621,13 +831,15 @@ By default, this does not affect elements in array and tuple types. You can chan
|
|
|
621
831
|
```
|
|
622
832
|
import type {PartialDeep} from 'type-fest';
|
|
623
833
|
|
|
624
|
-
type
|
|
625
|
-
|
|
626
|
-
}
|
|
834
|
+
type Shape = {
|
|
835
|
+
dimensions: [number, number];
|
|
836
|
+
};
|
|
627
837
|
|
|
628
|
-
const
|
|
629
|
-
|
|
838
|
+
const partialShape: PartialDeep<Shape, {recurseIntoArrays: true}> = {
|
|
839
|
+
dimensions: [], // OK
|
|
630
840
|
};
|
|
841
|
+
|
|
842
|
+
partialShape.dimensions = [15]; // OK
|
|
631
843
|
```
|
|
632
844
|
|
|
633
845
|
@see {@link PartialDeepOptions}
|
|
@@ -642,16 +854,20 @@ type PartialDeep<T, Options extends PartialDeepOptions = {}> =
|
|
|
642
854
|
|
|
643
855
|
type _PartialDeep<T, Options extends Required<PartialDeepOptions>> = T extends BuiltIns | ((new (...arguments_: any[]) => unknown))
|
|
644
856
|
? T
|
|
645
|
-
:
|
|
646
|
-
?
|
|
647
|
-
: T extends
|
|
648
|
-
?
|
|
649
|
-
: T extends
|
|
650
|
-
?
|
|
651
|
-
: T extends
|
|
652
|
-
?
|
|
653
|
-
: T extends
|
|
654
|
-
?
|
|
857
|
+
: T extends Map<infer KeyType, infer ValueType>
|
|
858
|
+
? PartialMapDeep<KeyType, ValueType, Options>
|
|
859
|
+
: T extends Set<infer ItemType>
|
|
860
|
+
? PartialSetDeep<ItemType, Options>
|
|
861
|
+
: T extends ReadonlyMap<infer KeyType, infer ValueType>
|
|
862
|
+
? PartialReadonlyMapDeep<KeyType, ValueType, Options>
|
|
863
|
+
: T extends ReadonlySet<infer ItemType>
|
|
864
|
+
? PartialReadonlySetDeep<ItemType, Options>
|
|
865
|
+
: T extends (...arguments_: any[]) => unknown
|
|
866
|
+
? IsNever<keyof T> extends true
|
|
867
|
+
? T // For functions with no properties
|
|
868
|
+
: HasMultipleCallSignatures<T> extends true
|
|
869
|
+
? T
|
|
870
|
+
: ((...arguments_: Parameters<T>) => ReturnType<T>) & PartialObjectDeep<T, Options>
|
|
655
871
|
: T extends object
|
|
656
872
|
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
|
657
873
|
? Options['recurseIntoArrays'] extends true
|
|
@@ -687,12 +903,9 @@ type PartialReadonlySetDeep<T, Options extends Required<PartialDeepOptions>> = {
|
|
|
687
903
|
/**
|
|
688
904
|
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
|
|
689
905
|
*/
|
|
690
|
-
type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> =
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
: {}) & ({
|
|
694
|
-
[KeyType in keyof ObjectType]?: _PartialDeep<ObjectType[KeyType], Options>
|
|
695
|
-
});
|
|
906
|
+
type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> = {
|
|
907
|
+
[KeyType in keyof ObjectType]?: _PartialDeep<ObjectType[KeyType], Options>
|
|
908
|
+
};
|
|
696
909
|
|
|
697
910
|
type AuthToken = string | undefined;
|
|
698
911
|
interface Auth {
|