@fleet-sdk/blockchain-providers 0.8.1 → 0.8.3

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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { BoxId, HexString, Base58String, TokenId, TransactionId, Box, DataInput, BlockHeader, SignedTransaction, UnsignedTransaction, ProverResult } from '@fleet-sdk/common';
1
+ import { BoxId, HexString, Base58String, TokenId, TransactionId, Box, ProverResult, DataInput, BlockHeader, SignedTransaction, UnsignedTransaction } from '@fleet-sdk/common';
2
2
  import { ErgoAddress } from '@fleet-sdk/core';
3
3
 
4
4
  declare global {
@@ -8,6 +8,175 @@ declare global {
8
8
  }
9
9
  }
10
10
 
11
+ /**
12
+ Extract all optional keys from the given type.
13
+
14
+ This is useful when you want to create a new type that contains different type values for the optional keys only.
15
+
16
+ @example
17
+ ```
18
+ import type {OptionalKeysOf, Except} from 'type-fest';
19
+
20
+ interface User {
21
+ name: string;
22
+ surname: string;
23
+
24
+ luckyNumber?: number;
25
+ }
26
+
27
+ const REMOVE_FIELD = Symbol('remove field symbol');
28
+ type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
29
+ [Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
30
+ };
31
+
32
+ const update1: UpdateOperation<User> = {
33
+ name: 'Alice'
34
+ };
35
+
36
+ const update2: UpdateOperation<User> = {
37
+ name: 'Bob',
38
+ luckyNumber: REMOVE_FIELD
39
+ };
40
+ ```
41
+
42
+ @category Utilities
43
+ */
44
+ type OptionalKeysOf<BaseType extends object> =
45
+ BaseType extends unknown // For distributing `BaseType`
46
+ ? (keyof {
47
+ [Key in keyof BaseType as BaseType extends Record<Key, BaseType[Key]> ? never : Key]: never
48
+ }) & (keyof BaseType) // Intersect with `keyof BaseType` to ensure result of `OptionalKeysOf<BaseType>` is always assignable to `keyof BaseType`
49
+ : never; // Should never happen
50
+
51
+ /**
52
+ Extract all required keys from the given type.
53
+
54
+ This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
55
+
56
+ @example
57
+ ```
58
+ import type {RequiredKeysOf} from 'type-fest';
59
+
60
+ declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
61
+
62
+ interface User {
63
+ name: string;
64
+ surname: string;
65
+
66
+ luckyNumber?: number;
67
+ }
68
+
69
+ const validator1 = createValidation<User>('name', value => value.length < 25);
70
+ const validator2 = createValidation<User>('surname', value => value.length < 25);
71
+ ```
72
+
73
+ @category Utilities
74
+ */
75
+ type RequiredKeysOf<BaseType extends object> =
76
+ BaseType extends unknown // For distributing `BaseType`
77
+ ? Exclude<keyof BaseType, OptionalKeysOf<BaseType>>
78
+ : never; // Should never happen
79
+
80
+ /**
81
+ Returns a boolean for whether the given type is `never`.
82
+
83
+ @link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
84
+ @link https://stackoverflow.com/a/53984913/10292952
85
+ @link https://www.zhenghao.io/posts/ts-never
86
+
87
+ Useful in type utilities, such as checking if something does not occur.
88
+
89
+ @example
90
+ ```
91
+ import type {IsNever, And} from 'type-fest';
92
+
93
+ // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
94
+ type AreStringsEqual<A extends string, B extends string> =
95
+ And<
96
+ IsNever<Exclude<A, B>> extends true ? true : false,
97
+ IsNever<Exclude<B, A>> extends true ? true : false
98
+ >;
99
+
100
+ type EndIfEqual<I extends string, O extends string> =
101
+ AreStringsEqual<I, O> extends true
102
+ ? never
103
+ : void;
104
+
105
+ function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
106
+ if (input === output) {
107
+ process.exit(0);
108
+ }
109
+ }
110
+
111
+ endIfEqual('abc', 'abc');
112
+ //=> never
113
+
114
+ endIfEqual('abc', '123');
115
+ //=> void
116
+ ```
117
+
118
+ @category Type Guard
119
+ @category Utilities
120
+ */
121
+ type IsNever<T> = [T] extends [never] ? true : false;
122
+
123
+ /**
124
+ An if-else-like type that resolves depending on whether the given type is `never`.
125
+
126
+ @see {@link IsNever}
127
+
128
+ @example
129
+ ```
130
+ import type {IfNever} from 'type-fest';
131
+
132
+ type ShouldBeTrue = IfNever<never>;
133
+ //=> true
134
+
135
+ type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
136
+ //=> 'bar'
137
+ ```
138
+
139
+ @category Type Guard
140
+ @category Utilities
141
+ */
142
+ type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
143
+ IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
144
+ );
145
+
146
+ // Can eventually be replaced with the built-in once this library supports
147
+ // TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
148
+ type NoInfer<T> = T extends infer U ? U : never;
149
+
150
+ /**
151
+ Returns a boolean for whether the given type is `any`.
152
+
153
+ @link https://stackoverflow.com/a/49928360/1490091
154
+
155
+ Useful in type utilities, such as disallowing `any`s to be passed to a function.
156
+
157
+ @example
158
+ ```
159
+ import type {IsAny} from 'type-fest';
160
+
161
+ const typedObject = {a: 1, b: 2} as const;
162
+ const anyObject: any = {a: 1, b: 2};
163
+
164
+ function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
165
+ return obj[key];
166
+ }
167
+
168
+ const typedA = get(typedObject, 'a');
169
+ //=> 1
170
+
171
+ const anyA = get(anyObject, 'a');
172
+ //=> any
173
+ ```
174
+
175
+ @category Type Guard
176
+ @category Utilities
177
+ */
178
+ type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
179
+
11
180
  /**
12
181
  Returns a boolean for whether the two given types are equal.
13
182
 
@@ -35,11 +204,355 @@ type Includes<Value extends readonly any[], Item> =
35
204
  @category Utilities
36
205
  */
37
206
  type IsEqual<A, B> =
38
- (<G>() => G extends A ? 1 : 2) extends
39
- (<G>() => G extends B ? 1 : 2)
207
+ (<G>() => G extends A & G | G ? 1 : 2) extends
208
+ (<G>() => G extends B & G | G ? 1 : 2)
40
209
  ? true
41
210
  : false;
42
211
 
212
+ /**
213
+ 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.
214
+
215
+ @example
216
+ ```
217
+ import type {Simplify} from 'type-fest';
218
+
219
+ type PositionProps = {
220
+ top: number;
221
+ left: number;
222
+ };
223
+
224
+ type SizeProps = {
225
+ width: number;
226
+ height: number;
227
+ };
228
+
229
+ // In your editor, hovering over `Props` will show a flattened object with all the properties.
230
+ type Props = Simplify<PositionProps & SizeProps>;
231
+ ```
232
+
233
+ Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
234
+
235
+ If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
236
+
237
+ @example
238
+ ```
239
+ import type {Simplify} from 'type-fest';
240
+
241
+ interface SomeInterface {
242
+ foo: number;
243
+ bar?: string;
244
+ baz: number | undefined;
245
+ }
246
+
247
+ type SomeType = {
248
+ foo: number;
249
+ bar?: string;
250
+ baz: number | undefined;
251
+ };
252
+
253
+ const literal = {foo: 123, bar: 'hello', baz: 456};
254
+ const someType: SomeType = literal;
255
+ const someInterface: SomeInterface = literal;
256
+
257
+ function fn(object: Record<string, unknown>): void {}
258
+
259
+ fn(literal); // Good: literal object type is sealed
260
+ fn(someType); // Good: type is sealed
261
+ fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
262
+ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
263
+ ```
264
+
265
+ @link https://github.com/microsoft/TypeScript/issues/15300
266
+ @see SimplifyDeep
267
+ @category Object
268
+ */
269
+ type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
270
+
271
+ /**
272
+ Omit any index signatures from the given object type, leaving only explicitly defined properties.
273
+
274
+ This is the counterpart of `PickIndexSignature`.
275
+
276
+ Use-cases:
277
+ - Remove overly permissive signatures from third-party types.
278
+
279
+ This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
280
+
281
+ It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
282
+
283
+ (The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
284
+
285
+ ```
286
+ const indexed: Record<string, unknown> = {}; // Allowed
287
+
288
+ const keyed: Record<'foo', unknown> = {}; // Error
289
+ // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
290
+ ```
291
+
292
+ 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:
293
+
294
+ ```
295
+ type Indexed = {} extends Record<string, unknown>
296
+ ? '✅ `{}` is assignable to `Record<string, unknown>`'
297
+ : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
298
+ // => '✅ `{}` is assignable to `Record<string, unknown>`'
299
+
300
+ type Keyed = {} extends Record<'foo' | 'bar', unknown>
301
+ ? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
302
+ : "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
303
+ // => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
304
+ ```
305
+
306
+ 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`...
307
+
308
+ ```
309
+ import type {OmitIndexSignature} from 'type-fest';
310
+
311
+ type OmitIndexSignature<ObjectType> = {
312
+ [KeyType in keyof ObjectType // Map each key of `ObjectType`...
313
+ ]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
314
+ };
315
+ ```
316
+
317
+ ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
318
+
319
+ ```
320
+ import type {OmitIndexSignature} from 'type-fest';
321
+
322
+ type OmitIndexSignature<ObjectType> = {
323
+ [KeyType in keyof ObjectType
324
+ // Is `{}` assignable to `Record<KeyType, unknown>`?
325
+ as {} extends Record<KeyType, unknown>
326
+ ? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
327
+ : ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
328
+ ]: ObjectType[KeyType];
329
+ };
330
+ ```
331
+
332
+ If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
333
+
334
+ @example
335
+ ```
336
+ import type {OmitIndexSignature} from 'type-fest';
337
+
338
+ interface Example {
339
+ // These index signatures will be removed.
340
+ [x: string]: any
341
+ [x: number]: any
342
+ [x: symbol]: any
343
+ [x: `head-${string}`]: string
344
+ [x: `${string}-tail`]: string
345
+ [x: `head-${string}-tail`]: string
346
+ [x: `${bigint}`]: string
347
+ [x: `embedded-${number}`]: string
348
+
349
+ // These explicitly defined keys will remain.
350
+ foo: 'bar';
351
+ qux?: 'baz';
352
+ }
353
+
354
+ type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
355
+ // => { foo: 'bar'; qux?: 'baz' | undefined; }
356
+ ```
357
+
358
+ @see PickIndexSignature
359
+ @category Object
360
+ */
361
+ type OmitIndexSignature<ObjectType> = {
362
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
363
+ ? never
364
+ : KeyType]: ObjectType[KeyType];
365
+ };
366
+
367
+ /**
368
+ Pick only index signatures from the given object type, leaving out all explicitly defined properties.
369
+
370
+ This is the counterpart of `OmitIndexSignature`.
371
+
372
+ @example
373
+ ```
374
+ import type {PickIndexSignature} from 'type-fest';
375
+
376
+ declare const symbolKey: unique symbol;
377
+
378
+ type Example = {
379
+ // These index signatures will remain.
380
+ [x: string]: unknown;
381
+ [x: number]: unknown;
382
+ [x: symbol]: unknown;
383
+ [x: `head-${string}`]: string;
384
+ [x: `${string}-tail`]: string;
385
+ [x: `head-${string}-tail`]: string;
386
+ [x: `${bigint}`]: string;
387
+ [x: `embedded-${number}`]: string;
388
+
389
+ // These explicitly defined keys will be removed.
390
+ ['kebab-case-key']: string;
391
+ [symbolKey]: string;
392
+ foo: 'bar';
393
+ qux?: 'baz';
394
+ };
395
+
396
+ type ExampleIndexSignature = PickIndexSignature<Example>;
397
+ // {
398
+ // [x: string]: unknown;
399
+ // [x: number]: unknown;
400
+ // [x: symbol]: unknown;
401
+ // [x: `head-${string}`]: string;
402
+ // [x: `${string}-tail`]: string;
403
+ // [x: `head-${string}-tail`]: string;
404
+ // [x: `${bigint}`]: string;
405
+ // [x: `embedded-${number}`]: string;
406
+ // }
407
+ ```
408
+
409
+ @see OmitIndexSignature
410
+ @category Object
411
+ */
412
+ type PickIndexSignature<ObjectType> = {
413
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
414
+ ? KeyType
415
+ : never]: ObjectType[KeyType];
416
+ };
417
+
418
+ // Merges two objects without worrying about index signatures.
419
+ type SimpleMerge<Destination, Source> = {
420
+ [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
421
+ } & Source;
422
+
423
+ /**
424
+ Merge two types into a new type. Keys of the second type overrides keys of the first type.
425
+
426
+ @example
427
+ ```
428
+ import type {Merge} from 'type-fest';
429
+
430
+ interface Foo {
431
+ [x: string]: unknown;
432
+ [x: number]: unknown;
433
+ foo: string;
434
+ bar: symbol;
435
+ }
436
+
437
+ type Bar = {
438
+ [x: number]: number;
439
+ [x: symbol]: unknown;
440
+ bar: Date;
441
+ baz: boolean;
442
+ };
443
+
444
+ export type FooBar = Merge<Foo, Bar>;
445
+ // => {
446
+ // [x: string]: unknown;
447
+ // [x: number]: number;
448
+ // [x: symbol]: unknown;
449
+ // foo: string;
450
+ // bar: Date;
451
+ // baz: boolean;
452
+ // }
453
+ ```
454
+
455
+ @category Object
456
+ */
457
+ type Merge<Destination, Source> =
458
+ Simplify<
459
+ SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
460
+ & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
461
+ >;
462
+
463
+ /**
464
+ An if-else-like type that resolves depending on whether the given type is `any`.
465
+
466
+ @see {@link IsAny}
467
+
468
+ @example
469
+ ```
470
+ import type {IfAny} from 'type-fest';
471
+
472
+ type ShouldBeTrue = IfAny<any>;
473
+ //=> true
474
+
475
+ type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
476
+ //=> 'bar'
477
+ ```
478
+
479
+ @category Type Guard
480
+ @category Utilities
481
+ */
482
+ type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
483
+ IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
484
+ );
485
+
486
+ /**
487
+ Merges user specified options with default options.
488
+
489
+ @example
490
+ ```
491
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
492
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
493
+ type SpecifiedOptions = {leavesOnly: true};
494
+
495
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
496
+ //=> {maxRecursionDepth: 10; leavesOnly: true}
497
+ ```
498
+
499
+ @example
500
+ ```
501
+ // Complains if default values are not provided for optional options
502
+
503
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
504
+ type DefaultPathsOptions = {maxRecursionDepth: 10};
505
+ type SpecifiedOptions = {};
506
+
507
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
508
+ // ~~~~~~~~~~~~~~~~~~~
509
+ // Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
510
+ ```
511
+
512
+ @example
513
+ ```
514
+ // Complains if an option's default type does not conform to the expected type
515
+
516
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
517
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
518
+ type SpecifiedOptions = {};
519
+
520
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
521
+ // ~~~~~~~~~~~~~~~~~~~
522
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
523
+ ```
524
+
525
+ @example
526
+ ```
527
+ // Complains if an option's specified type does not conform to the expected type
528
+
529
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
530
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
531
+ type SpecifiedOptions = {leavesOnly: 'yes'};
532
+
533
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
534
+ // ~~~~~~~~~~~~~~~~
535
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
536
+ ```
537
+ */
538
+ type ApplyDefaultOptions<
539
+ Options extends object,
540
+ Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
541
+ SpecifiedOptions extends Options,
542
+ > =
543
+ IfAny<SpecifiedOptions, Defaults,
544
+ IfNever<SpecifiedOptions, Defaults,
545
+ Simplify<Merge<Defaults, {
546
+ [Key in keyof SpecifiedOptions
547
+ as Key extends OptionalKeysOf<Options>
548
+ ? Extract<SpecifiedOptions[Key], undefined> extends never
549
+ ? Key
550
+ : never
551
+ : Key
552
+ ]: SpecifiedOptions[Key]
553
+ }> & Required<Options>> // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
554
+ >>;
555
+
43
556
  /**
44
557
  Filter out keys from an object.
45
558
 
@@ -80,6 +593,10 @@ type ExceptOptions = {
80
593
  requireExactProps?: boolean;
81
594
  };
82
595
 
596
+ type DefaultExceptOptions = {
597
+ requireExactProps: false;
598
+ };
599
+
83
600
  /**
84
601
  Create a type from an object type without certain keys.
85
602
 
@@ -109,11 +626,34 @@ type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
109
626
 
110
627
  const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
111
628
  //=> errors at 'b': Type 'string' is not assignable to type 'undefined'.
629
+
630
+ // The `Omit` utility type doesn't work when omitting specific keys from objects containing index signatures.
631
+
632
+ // Consider the following example:
633
+
634
+ type UserData = {
635
+ [metadata: string]: string;
636
+ email: string;
637
+ name: string;
638
+ role: 'admin' | 'user';
639
+ };
640
+
641
+ // `Omit` clearly doesn't behave as expected in this case:
642
+ type PostPayload = Omit<UserData, 'email'>;
643
+ //=> type PostPayload = { [x: string]: string; [x: number]: string; }
644
+
645
+ // In situations like this, `Except` works better.
646
+ // It simply removes the `email` key while preserving all the other keys.
647
+ type PostPayload = Except<UserData, 'email'>;
648
+ //=> type PostPayload = { [x: string]: string; name: string; role: 'admin' | 'user'; }
112
649
  ```
113
650
 
114
651
  @category Object
115
652
  */
116
- type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {requireExactProps: false}> = {
653
+ type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {}> =
654
+ _Except<ObjectType, KeysType, ApplyDefaultOptions<ExceptOptions, DefaultExceptOptions, Options>>;
655
+
656
+ type _Except<ObjectType, KeysType extends keyof ObjectType, Options extends Required<ExceptOptions>> = {
117
657
  [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
118
658
  } & (Options['requireExactProps'] extends true
119
659
  ? Partial<Record<KeysType, never>>