type-fest 2.3.3 → 2.5.1
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/index.d.ts +2 -0
- package/package.json +2 -2
- package/readme.md +32 -27
- package/source/except.d.ts +1 -1
- package/source/get.d.ts +1 -1
- package/source/internal.d.ts +26 -0
- package/source/multidimensional-array.d.ts +43 -0
- package/source/multidimensional-readonly-array.d.ts +47 -0
- package/source/mutable.d.ts +2 -2
- package/source/observable-like.d.ts +34 -1
- package/source/partial-deep.d.ts +6 -2
- package/source/promisable.d.ts +2 -2
package/index.d.ts
CHANGED
|
@@ -28,6 +28,8 @@ export {ConditionalPick} from './source/conditional-pick';
|
|
|
28
28
|
export {UnionToIntersection} from './source/union-to-intersection';
|
|
29
29
|
export {Stringified} from './source/stringified';
|
|
30
30
|
export {FixedLengthArray} from './source/fixed-length-array';
|
|
31
|
+
export {MultidimensionalArray} from './source/multidimensional-array';
|
|
32
|
+
export {MultidimensionalReadonlyArray} from './source/multidimensional-readonly-array';
|
|
31
33
|
export {IterableElement} from './source/iterable-element';
|
|
32
34
|
export {Entry} from './source/entry';
|
|
33
35
|
export {Entries} from './source/entries';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-fest",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "A collection of essential TypeScript types",
|
|
5
5
|
"license": "(MIT OR CC0-1.0)",
|
|
6
6
|
"repository": "sindresorhus/type-fest",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"node": ">=12.20"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
|
-
"test": "xo && tsd && tsc"
|
|
17
|
+
"test": "xo && tsd && tsc && node script/test/source-files-extension.js"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
20
|
"index.d.ts",
|
package/readme.md
CHANGED
|
@@ -48,12 +48,14 @@
|
|
|
48
48
|
[](https://www.npmjs.com/package/type-fest)
|
|
49
49
|
[](https://paka.dev/npm/type-fest)
|
|
50
50
|
|
|
51
|
-
Many of the types here should have been built-in. You can help by suggesting some of them to the [TypeScript project](https://github.com/Microsoft/TypeScript/blob/
|
|
51
|
+
Many of the types here should have been built-in. You can help by suggesting some of them to the [TypeScript project](https://github.com/Microsoft/TypeScript/blob/main/CONTRIBUTING.md).
|
|
52
52
|
|
|
53
53
|
Either add this package as a dependency or copy-paste the needed types. No credit required. 👌
|
|
54
54
|
|
|
55
55
|
PR welcome for additional commonly needed types and docs improvements. Read the [contributing guidelines](.github/contributing.md) first.
|
|
56
56
|
|
|
57
|
+
**Help wanted with reviewing [proposals](https://github.com/sindresorhus/type-fest/issues) and [pull requests](https://github.com/sindresorhus/type-fest/pulls).**
|
|
58
|
+
|
|
57
59
|
## Install
|
|
58
60
|
|
|
59
61
|
```
|
|
@@ -94,15 +96,15 @@ Click the type names for complete docs.
|
|
|
94
96
|
|
|
95
97
|
### Utilities
|
|
96
98
|
|
|
97
|
-
- [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/
|
|
99
|
+
- [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys).
|
|
98
100
|
- [`Mutable`](source/mutable.d.ts) - Create a type that strips `readonly` from all or some of an object's keys. The inverse of `Readonly<T>`.
|
|
99
101
|
- [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
100
102
|
- [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive keys.
|
|
101
103
|
- [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
|
|
102
104
|
- [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
|
|
103
105
|
- [`RequireAllOrNone`](source/require-all-or-none.d.ts) - Create a type that requires all of the given keys or none of the given keys.
|
|
104
|
-
- [`PartialDeep`](source/partial-deep.d.ts) - Create a deeply optional version of another type. Use [`Partial<T>`](https://
|
|
105
|
-
- [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of an `object`/`Map`/`Set`/`Array` type. Use [`Readonly<T>`](https://
|
|
106
|
+
- [`PartialDeep`](source/partial-deep.d.ts) - Create a deeply optional version of another type. Use [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) if you only need one level deep.
|
|
107
|
+
- [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of an `object`/`Map`/`Set`/`Array` type. Use [`Readonly<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) if you only need one level deep.
|
|
106
108
|
- [`LiteralUnion`](source/literal-union.d.ts) - Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729).
|
|
107
109
|
- [`Promisable`](source/promisable.d.ts) - Create a type that represents either the value or the value wrapped in `PromiseLike`.
|
|
108
110
|
- [`Opaque`](source/opaque.d.ts) - Create an [opaque type](https://codemix.com/opaque-types-in-javascript/).
|
|
@@ -117,6 +119,8 @@ Click the type names for complete docs.
|
|
|
117
119
|
- [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
|
|
118
120
|
- [`Stringified`](source/stringified.d.ts) - Create a type with the keys of the given type changed to `string` type.
|
|
119
121
|
- [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
|
|
122
|
+
- [`MultidimensionalArray`](source/multidimensional-array.d.ts) - Create a type that represents a multidimensional array of the given type and dimensions.
|
|
123
|
+
- [`MultidimensionalReadonlyArray`](source/multidimensional-readonly-array.d.ts) - Create a type that represents a multidimensional readonly array of the given type and dimensions.
|
|
120
124
|
- [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
|
|
121
125
|
- [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
|
|
122
126
|
- [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
|
|
@@ -129,8 +133,8 @@ Click the type names for complete docs.
|
|
|
129
133
|
### Template literal types
|
|
130
134
|
|
|
131
135
|
- [`CamelCase`](source/camel-case.d.ts) – Convert a string literal to camel-case (`fooBar`).
|
|
132
|
-
- [`CamelCasedProperties`](source/camel-cased-properties.d.ts) – Convert object properties to camel-case
|
|
133
|
-
- [`CamelCasedPropertiesDeep`](source/camel-cased-properties-deep.d.ts) – Convert object properties to camel-case recursively
|
|
136
|
+
- [`CamelCasedProperties`](source/camel-cased-properties.d.ts) – Convert object properties to camel-case (`fooBar`).
|
|
137
|
+
- [`CamelCasedPropertiesDeep`](source/camel-cased-properties-deep.d.ts) – Convert object properties to camel-case recursively (`fooBar`).
|
|
134
138
|
- [`KebabCase`](source/kebab-case.d.ts) – Convert a string literal to kebab-case (`foo-bar`).
|
|
135
139
|
- [`KebabCasedProperties`](source/kebab-cased-properties.d.ts) – Convert a object properties to kebab-case recursively (`foo-bar`).
|
|
136
140
|
- [`KebabCasedPropertiesDeep`](source/kebab-cased-properties-deep.d.ts) – Convert object properties to kebab-case (`foo-bar`).
|
|
@@ -160,8 +164,9 @@ Click the type names for complete docs.
|
|
|
160
164
|
*If we decline a type addition, we will make sure to document the better solution here.*
|
|
161
165
|
|
|
162
166
|
- [`Diff` and `Spread`](https://github.com/sindresorhus/type-fest/pull/7) - The PR author didn't provide any real-world use-cases and the PR went stale. If you think this type is useful, provide some real-world use-cases and we might reconsider.
|
|
163
|
-
- [`Dictionary`](https://github.com/sindresorhus/type-fest/issues/33) - You only save a few characters (`Dictionary<number>` vs `Record<string, number>`) from [`Record`](https://
|
|
167
|
+
- [`Dictionary`](https://github.com/sindresorhus/type-fest/issues/33) - You only save a few characters (`Dictionary<number>` vs `Record<string, number>`) from [`Record`](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type), which is more flexible and well-known. Also, you shouldn't use an object as a dictionary. We have `Map` in JavaScript now.
|
|
164
168
|
- [`ExtractProperties` and `ExtractMethods`](https://github.com/sindresorhus/type-fest/pull/4) - The types violate the single responsibility principle. Instead, refine your types into more granular type hierarchies.
|
|
169
|
+
- [`Url2Json`](https://github.com/sindresorhus/type-fest/pull/262) - Inferring search parameters from a URL string is a cute idea, but not very useful in practice, since search parameters are usually dynamic and defined separately.
|
|
165
170
|
|
|
166
171
|
## Tips
|
|
167
172
|
|
|
@@ -173,7 +178,7 @@ Click the type names for complete docs.
|
|
|
173
178
|
|
|
174
179
|
There are many advanced types most users don't know about.
|
|
175
180
|
|
|
176
|
-
- [`Partial<T>`](https://
|
|
181
|
+
- [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) - Make all properties in `T` optional.
|
|
177
182
|
<details>
|
|
178
183
|
<summary>
|
|
179
184
|
Example
|
|
@@ -220,7 +225,7 @@ There are many advanced types most users don't know about.
|
|
|
220
225
|
```
|
|
221
226
|
</details>
|
|
222
227
|
|
|
223
|
-
- [`Required<T>`](https://
|
|
228
|
+
- [`Required<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype) - Make all properties in `T` required.
|
|
224
229
|
<details>
|
|
225
230
|
<summary>
|
|
226
231
|
Example
|
|
@@ -250,7 +255,7 @@ There are many advanced types most users don't know about.
|
|
|
250
255
|
```
|
|
251
256
|
</details>
|
|
252
257
|
|
|
253
|
-
- [`Readonly<T>`](https://
|
|
258
|
+
- [`Readonly<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) - Make all properties in `T` readonly.
|
|
254
259
|
<details>
|
|
255
260
|
<summary>
|
|
256
261
|
Example
|
|
@@ -295,7 +300,7 @@ There are many advanced types most users don't know about.
|
|
|
295
300
|
```
|
|
296
301
|
</details>
|
|
297
302
|
|
|
298
|
-
- [`Pick<T, K>`](https://
|
|
303
|
+
- [`Pick<T, K>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys) - From `T`, pick a set of properties whose keys are in the union `K`.
|
|
299
304
|
<details>
|
|
300
305
|
<summary>
|
|
301
306
|
Example
|
|
@@ -335,7 +340,7 @@ There are many advanced types most users don't know about.
|
|
|
335
340
|
```
|
|
336
341
|
</details>
|
|
337
342
|
|
|
338
|
-
- [`Record<K, T>`](https://
|
|
343
|
+
- [`Record<K, T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type) - Construct a type with a set of properties `K` of type `T`.
|
|
339
344
|
<details>
|
|
340
345
|
<summary>
|
|
341
346
|
Example
|
|
@@ -378,7 +383,7 @@ There are many advanced types most users don't know about.
|
|
|
378
383
|
```
|
|
379
384
|
</details>
|
|
380
385
|
|
|
381
|
-
- [`Exclude<T, U>`](https://
|
|
386
|
+
- [`Exclude<T, U>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#excludetype-excludedunion) - Exclude from `T` those types that are assignable to `U`.
|
|
382
387
|
<details>
|
|
383
388
|
<summary>
|
|
384
389
|
Example
|
|
@@ -412,7 +417,7 @@ There are many advanced types most users don't know about.
|
|
|
412
417
|
```
|
|
413
418
|
</details>
|
|
414
419
|
|
|
415
|
-
- [`Extract<T, U>`](https://
|
|
420
|
+
- [`Extract<T, U>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#extracttype-union) - Extract from `T` those types that are assignable to `U`.
|
|
416
421
|
<details>
|
|
417
422
|
<summary>
|
|
418
423
|
Example
|
|
@@ -455,12 +460,12 @@ There are many advanced types most users don't know about.
|
|
|
455
460
|
```
|
|
456
461
|
</details>
|
|
457
462
|
|
|
458
|
-
- [`NonNullable<T>`](https://
|
|
463
|
+
- [`NonNullable<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype) - Exclude `null` and `undefined` from `T`.
|
|
459
464
|
<details>
|
|
460
465
|
<summary>
|
|
461
466
|
Example
|
|
462
467
|
</summary>
|
|
463
|
-
Works with <code>strictNullChecks</code> set to <code>true</code>.
|
|
468
|
+
Works with <a href="https://www.typescriptlang.org/tsconfig#strictNullChecks"><code>strictNullChecks</code></a> set to <code>true</code>.
|
|
464
469
|
|
|
465
470
|
[Playground](https://typescript-play.js.org/?target=6#code/C4TwDgpgBACg9gJ2AOQK4FsBGEFQLxQDOwCAlgHYDmUAPlORtrnQwDasDcAUFwPQBU-WAEMkUOADMowqAGNWwwoSgATCBIqlgpOOSjAAFsOBRSy1IQgr9cKJlSlW1mZYQA3HFH68u8xcoBlHA8EACEHJ08Aby4oKDBUTFZSWXjEFEYcAEIALihkXTR2YSSIAB54JDQsHAA+blj4xOTUsHSACkMzPKD3HHDHNQQAGjSkPMqMmoQASh7g-oihqBi4uNIpdraxPAI2VhmVxrX9AzMAOm2ppnwoAA4ABifuE4BfKAhWSyOTuK7CS7pao3AhXF5rV48E4ICDAVAIPT-cGQyG+XTEIgLMJLTx7CAAdygvRCA0iCHaMwarhJOIQjUBSHaACJHk8mYdeLwxtdcVAAOSsh58+lXdr7Dlcq7A3n3J4PEUdADMcspUE53OluAIUGVTx46oAKuAIAFZGQwCYAKIIBCILjUxaDHAMnla+iodjcIA)
|
|
466
471
|
|
|
@@ -494,7 +499,7 @@ There are many advanced types most users don't know about.
|
|
|
494
499
|
```
|
|
495
500
|
</details>
|
|
496
501
|
|
|
497
|
-
- [`Parameters<T>`](https://
|
|
502
|
+
- [`Parameters<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype) - Obtain the parameters of a function type in a tuple.
|
|
498
503
|
<details>
|
|
499
504
|
<summary>
|
|
500
505
|
Example
|
|
@@ -522,7 +527,7 @@ There are many advanced types most users don't know about.
|
|
|
522
527
|
```
|
|
523
528
|
</details>
|
|
524
529
|
|
|
525
|
-
- [`ConstructorParameters<T>`](https://
|
|
530
|
+
- [`ConstructorParameters<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#constructorparameterstype) - Obtain the parameters of a constructor function type in a tuple.
|
|
526
531
|
<details>
|
|
527
532
|
<summary>
|
|
528
533
|
Example
|
|
@@ -570,7 +575,7 @@ There are many advanced types most users don't know about.
|
|
|
570
575
|
```
|
|
571
576
|
</details>
|
|
572
577
|
|
|
573
|
-
- [`ReturnType<T>`](https://
|
|
578
|
+
- [`ReturnType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype) – Obtain the return type of a function type.
|
|
574
579
|
<details>
|
|
575
580
|
<summary>
|
|
576
581
|
Example
|
|
@@ -605,7 +610,7 @@ There are many advanced types most users don't know about.
|
|
|
605
610
|
```
|
|
606
611
|
</details>
|
|
607
612
|
|
|
608
|
-
- [`InstanceType<T>`](https://
|
|
613
|
+
- [`InstanceType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype) – Obtain the instance type of a constructor function type.
|
|
609
614
|
<details>
|
|
610
615
|
<summary>
|
|
611
616
|
Example
|
|
@@ -658,7 +663,7 @@ There are many advanced types most users don't know about.
|
|
|
658
663
|
```
|
|
659
664
|
</details>
|
|
660
665
|
|
|
661
|
-
- [`Omit<T, K>`](https://
|
|
666
|
+
- [`Omit<T, K>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) – Constructs a type by picking all properties from T and then removing K.
|
|
662
667
|
<details>
|
|
663
668
|
<summary>
|
|
664
669
|
Example
|
|
@@ -680,14 +685,14 @@ There are many advanced types most users don't know about.
|
|
|
680
685
|
type AnimalShortInfo = Omit<Animal, 'images' | 'paragraphs'>;
|
|
681
686
|
|
|
682
687
|
function renderAnimalHoverInfo (animals: AnimalShortInfo[]): HTMLElement {
|
|
683
|
-
const container =
|
|
688
|
+
const container = document.createElement('div');
|
|
684
689
|
// Internal implementation.
|
|
685
690
|
return container;
|
|
686
691
|
}
|
|
687
692
|
```
|
|
688
693
|
</details>
|
|
689
694
|
|
|
690
|
-
- [`Uppercase<S extends string>`](https://www.typescriptlang.org/docs/handbook/
|
|
695
|
+
- [`Uppercase<S extends string>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#uppercasestringtype) - Transforms every character in a string into uppercase.
|
|
691
696
|
<details>
|
|
692
697
|
<summary>
|
|
693
698
|
Example
|
|
@@ -708,7 +713,7 @@ There are many advanced types most users don't know about.
|
|
|
708
713
|
```
|
|
709
714
|
</details>
|
|
710
715
|
|
|
711
|
-
- [`Lowercase<S extends string>`](https://www.typescriptlang.org/docs/handbook/
|
|
716
|
+
- [`Lowercase<S extends string>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#lowercasestringtype) - Transforms every character in a string into lowercase.
|
|
712
717
|
<details>
|
|
713
718
|
<summary>
|
|
714
719
|
Example
|
|
@@ -729,7 +734,7 @@ There are many advanced types most users don't know about.
|
|
|
729
734
|
```
|
|
730
735
|
</details>
|
|
731
736
|
|
|
732
|
-
- [`Capitalize<S extends string>`](https://www.typescriptlang.org/docs/handbook/
|
|
737
|
+
- [`Capitalize<S extends string>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#capitalizestringtype) - Transforms the first character in a string into uppercase.
|
|
733
738
|
<details>
|
|
734
739
|
<summary>
|
|
735
740
|
Example
|
|
@@ -750,7 +755,7 @@ There are many advanced types most users don't know about.
|
|
|
750
755
|
```
|
|
751
756
|
</details>
|
|
752
757
|
|
|
753
|
-
- [`Uncapitalize<S extends string>`](https://www.typescriptlang.org/docs/handbook/
|
|
758
|
+
- [`Uncapitalize<S extends string>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#uncapitalizestringtype) - Transforms the first character in a string into lowercase.
|
|
754
759
|
<details>
|
|
755
760
|
<summary>
|
|
756
761
|
Example
|
|
@@ -771,7 +776,7 @@ There are many advanced types most users don't know about.
|
|
|
771
776
|
```
|
|
772
777
|
</details>
|
|
773
778
|
|
|
774
|
-
You can find some examples in the [TypeScript docs](https://www.typescriptlang.org/docs/handbook/
|
|
779
|
+
You can find some examples in the [TypeScript docs](https://www.typescriptlang.org/docs/handbook/utility-types.html).
|
|
775
780
|
|
|
776
781
|
## Maintainers
|
|
777
782
|
|
package/source/except.d.ts
CHANGED
|
@@ -52,6 +52,6 @@ type FooWithoutA = Except<Foo, 'a' | 'c'>;
|
|
|
52
52
|
|
|
53
53
|
@category Utilities
|
|
54
54
|
*/
|
|
55
|
-
export type Except<ObjectType, KeysType> = {
|
|
55
|
+
export type Except<ObjectType, KeysType extends keyof ObjectType> = {
|
|
56
56
|
[KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
|
|
57
57
|
};
|
package/source/get.d.ts
CHANGED
|
@@ -49,7 +49,7 @@ type ConsistsOnlyOf<LongString extends string, Substring extends string> =
|
|
|
49
49
|
? true
|
|
50
50
|
: LongString extends `${Substring}${infer Tail}`
|
|
51
51
|
? ConsistsOnlyOf<Tail, Substring>
|
|
52
|
-
|
|
52
|
+
: false;
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
Convert a type which may have number keys to one with string keys, making it possible to index using strings retrieved from template types.
|
package/source/internal.d.ts
CHANGED
|
@@ -9,3 +9,29 @@ export type IsEqual<T, U> =
|
|
|
9
9
|
(<G>() => G extends U ? 1 : 2)
|
|
10
10
|
? true
|
|
11
11
|
: false;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
Infer the length of the given array `<T>`.
|
|
15
|
+
|
|
16
|
+
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
17
|
+
*/
|
|
18
|
+
type TupleLength<T extends readonly unknown[]> = T extends {readonly length: infer L} ? L : never;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
Create a tuple type of the given length `<L>`.
|
|
22
|
+
|
|
23
|
+
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
24
|
+
*/
|
|
25
|
+
type BuildTuple<L extends number, T extends readonly unknown[] = []> = T extends {readonly length: L}
|
|
26
|
+
? T
|
|
27
|
+
: BuildTuple<L, [...T, unknown]>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
Create a tuple of length `A` and a tuple composed of two other tuples,
|
|
31
|
+
the inferred tuple `U` and a tuple of length `B`, then extracts the length of tuple `U`.
|
|
32
|
+
|
|
33
|
+
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
34
|
+
*/
|
|
35
|
+
export type Subtract<A extends number, B extends number> = BuildTuple<A> extends [...(infer U), ...BuildTuple<B>]
|
|
36
|
+
? TupleLength<U>
|
|
37
|
+
: never;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {IsEqual, Subtract} from './internal';
|
|
2
|
+
|
|
3
|
+
type Recursive<T> = Array<Recursive<T>>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Creates a type that represents a multidimensional array of the given type and dimension.
|
|
7
|
+
|
|
8
|
+
Use-cases:
|
|
9
|
+
- Return a n-dimensional array from functions.
|
|
10
|
+
- Declare a n-dimensional array by defining its dimensions rather than declaring `[]` repetitively.
|
|
11
|
+
- Infer the dimensions of a n-dimensional array automatically from function arguments.
|
|
12
|
+
- Avoid the need to know in advance the dimensions of a n-dimensional array allowing them to be dynamic.
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import {MultidimensionalArray} from 'type-fest';
|
|
17
|
+
|
|
18
|
+
function emptyMatrix<T extends number>(dimensions: T): MultidimensionalArray<unknown, T> {
|
|
19
|
+
const matrix: unknown[] = [];
|
|
20
|
+
|
|
21
|
+
let subMatrix = matrix;
|
|
22
|
+
for (let dimension = 1; dimension < dimensions; ++dimension) {
|
|
23
|
+
console.log(`Initializing dimension #${dimension}`);
|
|
24
|
+
|
|
25
|
+
subMatrix[0] = [];
|
|
26
|
+
subMatrix = subMatrix[0] as unknown[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return matrix as MultidimensionalArray<unknown, T>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const matrix = emptyMatrix(3);
|
|
33
|
+
|
|
34
|
+
matrix[0][0][0] = 42;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
@category Utilities
|
|
38
|
+
*/
|
|
39
|
+
export type MultidimensionalArray<Element, Dimensions extends number> = number extends Dimensions
|
|
40
|
+
? Recursive<Element>
|
|
41
|
+
: IsEqual<Dimensions, 0> extends true
|
|
42
|
+
? Element
|
|
43
|
+
: Array<MultidimensionalArray<Element, Subtract<Dimensions, 1>>>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {IsEqual, Subtract} from './internal';
|
|
2
|
+
|
|
3
|
+
type Recursive<T> = ReadonlyArray<Recursive<T>>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Creates a type that represents a multidimensional readonly array that of the given type and dimension.
|
|
7
|
+
|
|
8
|
+
Use-cases:
|
|
9
|
+
- Return a n-dimensional array from functions.
|
|
10
|
+
- Declare a n-dimensional array by defining its dimensions rather than declaring `[]` repetitively.
|
|
11
|
+
- Infer the dimensions of a n-dimensional array automatically from function arguments.
|
|
12
|
+
- Avoid the need to know in advance the dimensions of a n-dimensional array allowing them to be dynamic.
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import {MultidimensionalReadonlyArray} from 'type-fest';
|
|
17
|
+
|
|
18
|
+
function emptyMatrix<T extends number>(dimensions: T): MultidimensionalReadonlyArray<unknown, T> {
|
|
19
|
+
const matrix: unknown[] = [];
|
|
20
|
+
|
|
21
|
+
let subMatrix = matrix;
|
|
22
|
+
for (let dimension = 1; dimension < dimensions; ++dimension) {
|
|
23
|
+
console.log(`Initializing dimension #${dimension}`);
|
|
24
|
+
|
|
25
|
+
subMatrix[0] = [];
|
|
26
|
+
if (dimension < dimensions - 1) {
|
|
27
|
+
subMatrix = subMatrix[0] as unknown[];
|
|
28
|
+
} else {
|
|
29
|
+
subMatrix[0] = 42;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return matrix as MultidimensionalReadonlyArray<unknown, T>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const matrix = emptyMatrix(3);
|
|
37
|
+
|
|
38
|
+
const answer = matrix[0][0][0]; // 42
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
@category Utilities
|
|
42
|
+
*/
|
|
43
|
+
export type MultidimensionalReadonlyArray<Element, Dimensions extends number> = number extends Dimensions
|
|
44
|
+
? Recursive<Element>
|
|
45
|
+
: IsEqual<Dimensions, 0> extends true
|
|
46
|
+
? Element
|
|
47
|
+
: ReadonlyArray<MultidimensionalReadonlyArray<Element, Subtract<Dimensions, 1>>>;
|
package/source/mutable.d.ts
CHANGED
|
@@ -35,6 +35,6 @@ export type Mutable<BaseType, Keys extends keyof BaseType = keyof BaseType> =
|
|
|
35
35
|
Simplify<
|
|
36
36
|
// Pick just the keys that are not mutable from the base type.
|
|
37
37
|
Except<BaseType, Keys> &
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
// Pick the keys that should be mutable from the base type and make them mutable by removing the `readonly` modifier from the key.
|
|
39
|
+
{-readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick<BaseType, Keys>[KeyType]}
|
|
40
40
|
>;
|
|
@@ -4,12 +4,45 @@ declare global {
|
|
|
4
4
|
}
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
@remarks
|
|
9
|
+
The TC39 observable proposal defines a `closed` property, but some implementations (such as xstream) do not as of 10/08/2021.
|
|
10
|
+
As well, some guideance on making an `Observable` do not include `closed` propery.
|
|
11
|
+
@see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L129-L130
|
|
12
|
+
@see https://github.com/staltz/xstream/blob/6c22580c1d84d69773ee4b0905df44ad464955b3/src/index.ts#L79-L85
|
|
13
|
+
@see https://github.com/benlesh/symbol-observable#making-an-object-observable
|
|
14
|
+
*/
|
|
15
|
+
export type Unsubscribable = {
|
|
16
|
+
unsubscribe(): void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type OnNext<ValueType> = (value: ValueType) => void;
|
|
20
|
+
type OnError = (error: unknown) => void;
|
|
21
|
+
type OnComplete = () => void;
|
|
22
|
+
|
|
23
|
+
export type Observer<ValueType> = {
|
|
24
|
+
next: OnNext<ValueType>;
|
|
25
|
+
error: OnError;
|
|
26
|
+
complete: OnComplete;
|
|
27
|
+
};
|
|
28
|
+
|
|
7
29
|
/**
|
|
8
30
|
Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
|
|
9
31
|
|
|
32
|
+
@remarks
|
|
33
|
+
The TC39 Observable proposal defines 2 forms of `subscribe()`:
|
|
34
|
+
1. Three callback arguments: `subscribe(observer: OnNext<ValueType>, onError?: OnError, onComplete?: OnComplete): Unsubscribable;`
|
|
35
|
+
2. A single `observer` argument: (as defined below)
|
|
36
|
+
|
|
37
|
+
But `Observable` implementations have evolved to preferring case 2 and some implementations choose not to implement case 1. Therefore, an `ObservableLike` cannot be trusted to implement the first case. (xstream and hand built observerables often do not implement case 1)
|
|
38
|
+
|
|
39
|
+
@see https://github.com/tc39/proposal-observable#observable
|
|
40
|
+
@see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L246-L259
|
|
41
|
+
@see https://benlesh.com/posts/learning-observable-by-building-observable/
|
|
42
|
+
|
|
10
43
|
@category Basic
|
|
11
44
|
*/
|
|
12
45
|
export interface ObservableLike<ValueType = unknown> {
|
|
13
|
-
subscribe(observer
|
|
46
|
+
subscribe(observer?: Partial<Observer<ValueType>>): Unsubscribable;
|
|
14
47
|
[Symbol.observable](): ObservableLike<ValueType>;
|
|
15
48
|
}
|
package/source/partial-deep.d.ts
CHANGED
|
@@ -43,11 +43,15 @@ export type PartialDeep<T> = T extends Primitive
|
|
|
43
43
|
: T extends ((...arguments: any[]) => unknown)
|
|
44
44
|
? T | undefined
|
|
45
45
|
: T extends object
|
|
46
|
-
?
|
|
46
|
+
? T extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
|
47
|
+
? ItemType[] extends T // Test for arrays (non-tuples) specifically
|
|
48
|
+
? Array<PartialDeep<ItemType | undefined>> // Recreate relevant array type to prevent eager evaluation of circular reference
|
|
49
|
+
: PartialObjectDeep<T> // Tuples behave properly
|
|
50
|
+
: PartialObjectDeep<T>
|
|
47
51
|
: unknown;
|
|
48
52
|
|
|
49
53
|
/**
|
|
50
|
-
Same as `PartialDeep`, but accepts only `Map`s and
|
|
54
|
+
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
|
|
51
55
|
*/
|
|
52
56
|
interface PartialMapDeep<KeyType, ValueType> extends Map<PartialDeep<KeyType>, PartialDeep<ValueType>> {}
|
|
53
57
|
|
package/source/promisable.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31394)
|
|
|
12
12
|
import {Promisable} from 'type-fest';
|
|
13
13
|
|
|
14
14
|
async function logger(getLogEntry: () => Promisable<string>): Promise<void> {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
const entry = await getLogEntry();
|
|
16
|
+
console.log(entry);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
logger(() => 'foo');
|