type-fest 2.4.0 → 2.5.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/index.d.ts +2 -0
- package/package.json +2 -2
- package/readme.md +11 -3
- package/source/delimiter-case.d.ts +5 -3
- package/source/internal.d.ts +26 -0
- package/source/join.d.ts +1 -1
- package/source/multidimensional-array.d.ts +43 -0
- package/source/multidimensional-readonly-array.d.ts +47 -0
- package/source/partial-deep.d.ts +5 -1
- package/source/simplify.d.ts +35 -1
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.3",
|
|
4
4
|
"description": "A collection of essential TypeScript types",
|
|
5
5
|
"license": "(MIT OR CC0-1.0)",
|
|
6
6
|
"repository": "sindresorhus/type-fest",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@sindresorhus/tsconfig": "~0.7.0",
|
|
36
36
|
"expect-type": "^0.12.0",
|
|
37
37
|
"tsd": "^0.17.0",
|
|
38
|
-
"typescript": "
|
|
38
|
+
"typescript": ">=4.2",
|
|
39
39
|
"xo": "^0.43.0"
|
|
40
40
|
},
|
|
41
41
|
"types": "./index.d.ts",
|
package/readme.md
CHANGED
|
@@ -58,8 +58,8 @@ PR welcome for additional commonly needed types and docs improvements. Read the
|
|
|
58
58
|
|
|
59
59
|
## Install
|
|
60
60
|
|
|
61
|
-
```
|
|
62
|
-
|
|
61
|
+
```sh
|
|
62
|
+
npm install type-fest
|
|
63
63
|
```
|
|
64
64
|
|
|
65
65
|
*Requires TypeScript >=4.2*
|
|
@@ -119,13 +119,15 @@ Click the type names for complete docs.
|
|
|
119
119
|
- [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
|
|
120
120
|
- [`Stringified`](source/stringified.d.ts) - Create a type with the keys of the given type changed to `string` type.
|
|
121
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.
|
|
122
124
|
- [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
|
|
123
125
|
- [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
|
|
124
126
|
- [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
|
|
125
127
|
- [`SetReturnType`](source/set-return-type.d.ts) - Create a function type with a return type of your choice and the same parameters as the given function type.
|
|
126
128
|
- [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
|
|
127
129
|
- [`Includes`](source/includes.d.ts) - Returns a boolean for whether the given array includes the given item.
|
|
128
|
-
- [`Simplify`](source/simplify.d.ts) -
|
|
130
|
+
- [`Simplify`](source/simplify.d.ts) - 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.
|
|
129
131
|
- [`Jsonify`](source/jsonify.d.ts) - Transform a type to one that is assignable to the `JsonValue` type.
|
|
130
132
|
|
|
131
133
|
### Template literal types
|
|
@@ -166,6 +168,12 @@ Click the type names for complete docs.
|
|
|
166
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.
|
|
167
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.
|
|
168
170
|
|
|
171
|
+
## Alternative type names
|
|
172
|
+
|
|
173
|
+
*If you know one of our types by a different name, add it here for discovery.*
|
|
174
|
+
|
|
175
|
+
- `PartialBy` - See [`SetOptional`](https://github.com/sindresorhus/type-fest/blob/main/source/set-optional.d.ts)
|
|
176
|
+
|
|
169
177
|
## Tips
|
|
170
178
|
|
|
171
179
|
### Related
|
|
@@ -24,8 +24,9 @@ Format a specific part of the splitted string literal that `StringArrayToDelimit
|
|
|
24
24
|
|
|
25
25
|
@see StringArrayToDelimiterCase
|
|
26
26
|
*/
|
|
27
|
-
type StringPartToDelimiterCase<StringPart extends string, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
|
|
27
|
+
type StringPartToDelimiterCase<StringPart extends string, Start extends boolean, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
|
|
28
28
|
StringPart extends UsedWordSeparators ? Delimiter :
|
|
29
|
+
Start extends true ? Lowercase<StringPart> :
|
|
29
30
|
StringPart extends UsedUpperCaseCharacters ? `${Delimiter}${Lowercase<StringPart>}` :
|
|
30
31
|
StringPart;
|
|
31
32
|
|
|
@@ -36,9 +37,9 @@ It receives `UsedWordSeparators` and `UsedUpperCaseCharacters` as input to ensur
|
|
|
36
37
|
|
|
37
38
|
@see SplitIncludingDelimiters
|
|
38
39
|
*/
|
|
39
|
-
type StringArrayToDelimiterCase<Parts extends readonly any[], UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
|
|
40
|
+
type StringArrayToDelimiterCase<Parts extends readonly any[], Start extends boolean, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
|
|
40
41
|
Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
|
|
41
|
-
? `${StringPartToDelimiterCase<FirstPart, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}${StringArrayToDelimiterCase<RemainingParts, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}`
|
|
42
|
+
? `${StringPartToDelimiterCase<FirstPart, Start, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}${StringArrayToDelimiterCase<RemainingParts, false, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}`
|
|
42
43
|
: '';
|
|
43
44
|
|
|
44
45
|
/**
|
|
@@ -81,6 +82,7 @@ const rawCliOptions: OddlyCasedProperties<SomeOptions> = {
|
|
|
81
82
|
export type DelimiterCase<Value, Delimiter extends string> = Value extends string
|
|
82
83
|
? StringArrayToDelimiterCase<
|
|
83
84
|
SplitIncludingDelimiters<Value, WordSeparators | UpperCaseCharacters>,
|
|
85
|
+
true,
|
|
84
86
|
WordSeparators,
|
|
85
87
|
UpperCaseCharacters,
|
|
86
88
|
Delimiter
|
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;
|
package/source/join.d.ts
CHANGED
|
@@ -26,4 +26,4 @@ export type Join<
|
|
|
26
26
|
Strings extends [string | number] ? `${Strings[0]}` :
|
|
27
27
|
// @ts-expect-error `Rest` is inferred as `unknown` here: https://github.com/microsoft/TypeScript/issues/45281
|
|
28
28
|
Strings extends [string | number, ...infer Rest] ? `${Strings[0]}${Delimiter}${Join<Rest, Delimiter>}` :
|
|
29
|
-
string
|
|
29
|
+
string;
|
|
@@ -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/partial-deep.d.ts
CHANGED
|
@@ -43,7 +43,11 @@ 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
|
/**
|
package/source/simplify.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
2
|
+
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.
|
|
3
3
|
|
|
4
4
|
@example
|
|
5
5
|
```
|
|
@@ -19,6 +19,40 @@ type SizeProps = {
|
|
|
19
19
|
type Props = Simplify<PositionProps & SizeProps>;
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
+
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.
|
|
23
|
+
|
|
24
|
+
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`.
|
|
25
|
+
|
|
26
|
+
@example
|
|
27
|
+
```
|
|
28
|
+
import {Simplify} from 'type-fest';
|
|
29
|
+
|
|
30
|
+
interface SomeInterface {
|
|
31
|
+
foo: number;
|
|
32
|
+
bar?: string;
|
|
33
|
+
baz: number | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type SomeType = {
|
|
37
|
+
foo: number;
|
|
38
|
+
bar?: string;
|
|
39
|
+
baz: number | undefined;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
43
|
+
const someType: SomeType = literal;
|
|
44
|
+
const someInterface: SomeInterface = literal;
|
|
45
|
+
|
|
46
|
+
function fn(object: Record<string, unknown>): void {}
|
|
47
|
+
|
|
48
|
+
fn(literal); // Good: literal object type is sealed
|
|
49
|
+
fn(someType); // Good: type is sealed
|
|
50
|
+
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
51
|
+
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
55
|
+
|
|
22
56
|
@category Utilities
|
|
23
57
|
*/
|
|
24
58
|
export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]};
|