type-fest 0.11.0 → 0.12.0
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 +1 -0
- package/license +1 -1
- package/package.json +8 -13
- package/readme.md +3 -0
- package/source/basic.d.ts +1 -1
- package/source/require-exactly-one.d.ts +0 -1
- package/source/set-optional.d.ts +3 -1
- package/source/set-required.d.ts +3 -1
- package/source/union-to-intersection.d.ts +58 -0
package/index.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export {AsyncReturnType} from './source/async-return-type';
|
|
|
20
20
|
export {ConditionalExcept} from './source/conditional-except';
|
|
21
21
|
export {ConditionalKeys} from './source/conditional-keys';
|
|
22
22
|
export {ConditionalPick} from './source/conditional-pick';
|
|
23
|
+
export {UnionToIntersection} from './source/union-to-intersection';
|
|
23
24
|
|
|
24
25
|
// Miscellaneous
|
|
25
26
|
export {PackageJson} from './source/package-json';
|
package/license
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https:/sindresorhus.com)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-fest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "A collection of essential TypeScript types",
|
|
5
5
|
"license": "(MIT OR CC0-1.0)",
|
|
6
6
|
"repository": "sindresorhus/type-fest",
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
"author": {
|
|
9
9
|
"name": "Sindre Sorhus",
|
|
10
10
|
"email": "sindresorhus@gmail.com",
|
|
11
|
-
"url": "sindresorhus.com"
|
|
11
|
+
"url": "https://sindresorhus.com"
|
|
12
12
|
},
|
|
13
13
|
"engines": {
|
|
14
|
-
"node": ">=
|
|
14
|
+
"node": ">=10"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"test": "xo && tsd"
|
|
@@ -33,21 +33,16 @@
|
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@sindresorhus/tsconfig": "^0.7.0",
|
|
36
|
-
"@typescript-eslint/eslint-plugin": "2.
|
|
37
|
-
"@typescript-eslint/parser": "2.
|
|
38
|
-
"eslint-config-xo-typescript": "^0.
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^2.22.0",
|
|
37
|
+
"@typescript-eslint/parser": "^2.22.0",
|
|
38
|
+
"eslint-config-xo-typescript": "^0.26.0",
|
|
39
39
|
"tsd": "^0.7.3",
|
|
40
|
-
"typescript": "3.
|
|
41
|
-
"xo": "^0.
|
|
40
|
+
"typescript": "^3.8.3",
|
|
41
|
+
"xo": "^0.27.2"
|
|
42
42
|
},
|
|
43
43
|
"types": "index.d.ts",
|
|
44
44
|
"xo": {
|
|
45
|
-
"extends": "xo-typescript",
|
|
46
|
-
"extensions": [
|
|
47
|
-
"ts"
|
|
48
|
-
],
|
|
49
45
|
"rules": {
|
|
50
|
-
"import/no-unresolved": "off",
|
|
51
46
|
"@typescript-eslint/indent": "off"
|
|
52
47
|
}
|
|
53
48
|
}
|
package/readme.md
CHANGED
|
@@ -79,6 +79,7 @@ Click the type names for complete docs.
|
|
|
79
79
|
- [`ConditionalKeys`](source/conditional-keys.d.ts) - Extract keys from a shape where values extend the given `Condition` type.
|
|
80
80
|
- [`ConditionalPick`](source/conditional-pick.d.ts) - Like `Pick` except it selects properties from a shape where the values extend the given `Condition` type.
|
|
81
81
|
- [`ConditionalExcept`](source/conditional-except.d.ts) - Like `Omit` except it removes properties from a shape where the values extend the given `Condition` type.
|
|
82
|
+
- [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
|
|
82
83
|
|
|
83
84
|
### Miscellaneous
|
|
84
85
|
|
|
@@ -91,6 +92,8 @@ Click the type names for complete docs.
|
|
|
91
92
|
|
|
92
93
|
- [`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.
|
|
93
94
|
- [`Dictionary`](https://github.com/sindresorhus/type-fest/issues/33) - You only save a few characters (`Dictionary<number>` vs `Record<string, number>`) from [`Record`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1429-L1434), which is more flexible and well-known. Also, you shouldn't use an object as a dictionary. We have `Map` in JavaScript now.
|
|
95
|
+
- [`SubType`](https://github.com/sindresorhus/type-fest/issues/22) - The type is powerful, but lacks good use-cases and is prone to misuse.
|
|
96
|
+
- [`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.
|
|
94
97
|
|
|
95
98
|
## Tips
|
|
96
99
|
|
package/source/basic.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type Primitive =
|
|
|
17
17
|
/**
|
|
18
18
|
Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
|
|
19
19
|
*/
|
|
20
|
-
export type Class<T = unknown, Arguments extends any[] = any[]> = new(...arguments_: Arguments) => T;
|
|
20
|
+
export type Class<T = unknown, Arguments extends any[] = any[]> = new(...arguments_: Arguments) => T; // eslint-disable-line @typescript-eslint/type-annotation-spacing
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
|
package/source/set-optional.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {Except} from './except';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.
|
|
3
5
|
|
|
@@ -23,7 +25,7 @@ type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
|
|
|
23
25
|
*/
|
|
24
26
|
export type SetOptional<BaseType, Keys extends keyof BaseType = keyof BaseType> =
|
|
25
27
|
// Pick just the keys that are not optional from the base type.
|
|
26
|
-
|
|
28
|
+
Except<BaseType, Keys> &
|
|
27
29
|
// Pick the keys that should be optional from the base type and make them optional.
|
|
28
30
|
Partial<Pick<BaseType, Keys>> extends
|
|
29
31
|
// If `InferredType` extends the previous, then for each key, use the inferred type key.
|
package/source/set-required.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {Except} from './except';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` type.
|
|
3
5
|
|
|
@@ -23,7 +25,7 @@ type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
|
|
|
23
25
|
*/
|
|
24
26
|
export type SetRequired<BaseType, Keys extends keyof BaseType = keyof BaseType> =
|
|
25
27
|
// Pick just the keys that are not required from the base type.
|
|
26
|
-
|
|
28
|
+
Except<BaseType, Keys> &
|
|
27
29
|
// Pick the keys that should be required from the base type and make them required.
|
|
28
30
|
Required<Pick<BaseType, Keys>> extends
|
|
29
31
|
// If `InferredType` extends the previous, then for each key, use the inferred type key.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
3
|
+
|
|
4
|
+
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import {UnionToIntersection} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
11
|
+
|
|
12
|
+
type Intersection = UnionToIntersection<Union>;
|
|
13
|
+
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
A more applicable example which could make its way into your library code follows.
|
|
17
|
+
|
|
18
|
+
@example
|
|
19
|
+
```
|
|
20
|
+
import {UnionToIntersection} from 'type-fest';
|
|
21
|
+
|
|
22
|
+
class CommandOne {
|
|
23
|
+
commands: {
|
|
24
|
+
a1: () => undefined,
|
|
25
|
+
b1: () => undefined,
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class CommandTwo {
|
|
30
|
+
commands: {
|
|
31
|
+
a2: (argA: string) => undefined,
|
|
32
|
+
b2: (argB: string) => undefined,
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
|
|
37
|
+
type Union = typeof union;
|
|
38
|
+
//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
|
|
39
|
+
|
|
40
|
+
type Intersection = UnionToIntersection<Union>;
|
|
41
|
+
//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
|
|
42
|
+
```
|
|
43
|
+
*/
|
|
44
|
+
export type UnionToIntersection<Union> = (
|
|
45
|
+
// `extends any` is always going to be the case and is used to convert the
|
|
46
|
+
// `Union` into a [distributive conditional
|
|
47
|
+
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
48
|
+
Union extends unknown
|
|
49
|
+
// The union type is used as the only argument to a function since the union
|
|
50
|
+
// of function arguments is an intersection.
|
|
51
|
+
? (distributedUnion: Union) => void
|
|
52
|
+
// This won't happen.
|
|
53
|
+
: never
|
|
54
|
+
// Infer the `Intersection` type since TypeScript represents the positional
|
|
55
|
+
// arguments of unions of functions as an intersection of the union.
|
|
56
|
+
) extends ((mergedIntersection: infer Intersection) => void)
|
|
57
|
+
? Intersection
|
|
58
|
+
: never;
|