type-fest 0.17.0 → 0.18.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 +2 -0
- package/package.json +2 -2
- package/readme.md +2 -0
- package/source/asyncify.d.ts +31 -0
- package/source/set-return-type.d.ts +29 -0
package/index.d.ts
CHANGED
|
@@ -27,6 +27,8 @@ export {FixedLengthArray} from './source/fixed-length-array';
|
|
|
27
27
|
export {IterableElement} from './source/iterable-element';
|
|
28
28
|
export {Entry} from './source/entry';
|
|
29
29
|
export {Entries} from './source/entries';
|
|
30
|
+
export {SetReturnType} from './source/set-return-type';
|
|
31
|
+
export {Asyncify} from './source/asyncify';
|
|
30
32
|
|
|
31
33
|
// Miscellaneous
|
|
32
34
|
export {PackageJson} from './source/package-json';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-fest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "A collection of essential TypeScript types",
|
|
5
5
|
"license": "(MIT OR CC0-1.0)",
|
|
6
6
|
"repository": "sindresorhus/type-fest",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"json"
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"tsd": "^0.
|
|
35
|
+
"tsd": "^0.13.1",
|
|
36
36
|
"xo": "^0.28.2"
|
|
37
37
|
},
|
|
38
38
|
"types": "index.d.ts",
|
package/readme.md
CHANGED
|
@@ -86,6 +86,8 @@ Click the type names for complete docs.
|
|
|
86
86
|
- [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
|
|
87
87
|
- [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
|
|
88
88
|
- [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
|
|
89
|
+
- [`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.
|
|
90
|
+
- [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
|
|
89
91
|
|
|
90
92
|
### Miscellaneous
|
|
91
93
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {PromiseValue} from './promise-value';
|
|
2
|
+
import {SetReturnType} from './set-return-type';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Create an async version of the given function type, by boxing the return type in `Promise` while keeping the same parameter types.
|
|
6
|
+
|
|
7
|
+
Use-case: You have two functions, one synchronous and one asynchronous that do the same thing. Instead of having to duplicate the type definition, you can use `Asyncify` to reuse the synchronous type.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import {Asyncify} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
// Synchronous function.
|
|
14
|
+
function getFooSync(someArg: SomeType): Foo {
|
|
15
|
+
// …
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type AsyncifiedFooGetter = Asyncify<typeof getFooSync>;
|
|
19
|
+
//=> type AsyncifiedFooGetter = (someArg: SomeType) => Promise<Foo>;
|
|
20
|
+
|
|
21
|
+
// Same as `getFooSync` but asynchronous.
|
|
22
|
+
const getFooAsync: AsyncifiedFooGetter = (someArg) => {
|
|
23
|
+
// TypeScript now knows that `someArg` is `SomeType` automatically.
|
|
24
|
+
// It also knows that this function must return `Promise<Foo>`.
|
|
25
|
+
// If you have `@typescript-eslint/promise-function-async` linter rule enabled, it will even report that "Functions that return promises must be async.".
|
|
26
|
+
|
|
27
|
+
// …
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
*/
|
|
31
|
+
export type Asyncify<Fn extends (...args: any[]) => any> = SetReturnType<Fn, Promise<PromiseValue<ReturnType<Fn>>>>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
type IsAny<T> = 0 extends (1 & T) ? true : false; // https://stackoverflow.com/a/49928360/3406963
|
|
2
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
3
|
+
type IsUnknown<T> = IsNever<T> extends false ? T extends unknown ? unknown extends T ? IsAny<T> extends false ? true : false : false : false : false;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Create a function type with a return type of your choice and the same parameters as the given function type.
|
|
7
|
+
|
|
8
|
+
Use-case: You want to define a wrapped function that returns something different while receiving the same parameters. For example, you might want to wrap a function that can throw an error into one that will return `undefined` instead.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import {SetReturnType} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type MyFunctionThatCanThrow = (foo: SomeType, bar: unknown) => SomeOtherType;
|
|
15
|
+
|
|
16
|
+
type MyWrappedFunction = SetReturnType<MyFunctionThatCanThrow, SomeOtherType | undefined>;
|
|
17
|
+
//=> type MyWrappedFunction = (foo: SomeType, bar: unknown) => SomeOtherType | undefined;
|
|
18
|
+
```
|
|
19
|
+
*/
|
|
20
|
+
export type SetReturnType<Fn extends (...args: any[]) => any, TypeToReturn> =
|
|
21
|
+
// Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter.
|
|
22
|
+
Fn extends (this: infer ThisArg, ...args: infer Arguments) => any ? (
|
|
23
|
+
// If a function did not specify the `this` fake parameter, it will be inferred to `unknown`.
|
|
24
|
+
// We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE.
|
|
25
|
+
IsUnknown<ThisArg> extends true ? (...args: Arguments) => TypeToReturn : (this: ThisArg, ...args: Arguments) => TypeToReturn
|
|
26
|
+
) : (
|
|
27
|
+
// This part should be unreachable, but we make it meaningful just in case…
|
|
28
|
+
(...args: Parameters<Fn>) => TypeToReturn
|
|
29
|
+
);
|