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