type-fest
Version:
A collection of essential TypeScript types
109 lines (80 loc) • 3.19 kB
TypeScript
/**
Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
@category Class
*/
export type Class<T, Arguments extends readonly unknown[] = any[]> = {
prototype: Pick<T, keyof T>;
new(...arguments_: Arguments): T;
};
/**
Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
@category Class
*/
export type Constructor<T, Arguments extends readonly unknown[] = any[]> = new(...arguments_: Arguments) => T;
/**
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/2/classes.html#abstract-classes-and-members).
@category Class
@privateRemarks
We cannot use a `type` here because TypeScript throws: 'abstract' modifier cannot appear on a type member. (1070)
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface AbstractClass<T, Arguments extends readonly unknown[] = any[]> extends AbstractConstructor<T, Arguments> {
prototype: Pick<T, keyof T>;
}
/**
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.
@category Class
*/
export type AbstractConstructor<T, Arguments extends readonly unknown[] = any[]> = abstract new(...arguments_: Arguments) => T;
/**
Matches an asynchronous function.
Use-cases:
- Constrain a generic type to an asynchronous function.
- Declare a reusable asynchronous callback signature.
@example
```
import type {AsyncFunction} from 'type-fest';
type FetchData = AsyncFunction<[url: URL], Uint8Array>;
const fetchData: FetchData = async url => {
const response = await fetch(url);
return new Uint8Array(await response.arrayBuffer());
};
```
@category Function
@category Async
*/
export type AsyncFunction<Arguments extends readonly unknown[] = readonly any[], ReturnValue = unknown> = (...arguments_: Arguments) => Promise<ReturnValue>;
/**
Matches any asynchronous function.
Use-cases:
- Constrain a generic type to any asynchronous function without prescribing its parameters or return value.
@example
```
import type {AnyAsyncFunction} from 'type-fest';
function register<FunctionType extends AnyAsyncFunction>(function_: FunctionType): FunctionType {
return function_;
}
const getLength = register(async (value: string) => value.length);
const result = getLength('unicorn');
//=> Promise<number>
```
Note: A function with a `never` parameter, such as `(value: never) => Promise<void>`, does not match. TypeScript skips the parameter check only for a function type that returns `unknown` or `any`. Use {@link AnyFunction} if you need to accept it.
@category Function
@category Async
*/
export type AnyAsyncFunction = (...arguments_: readonly any[]) => Promise<unknown>;
/**
Matches any function.
Use-cases:
- Constrain a generic type to any function while erasing its parameters and return value.
@example
```
import type {AnyFunction} from 'type-fest';
function identity<FunctionType extends AnyFunction>(function_: FunctionType): FunctionType {
return function_;
}
```
@category Function
*/
export type AnyFunction = (...arguments_: readonly any[]) => unknown;
export {};