Improve type
This commit is contained in:
parent
063ee2a2c0
commit
26cd95cfda
1 changed files with 10 additions and 4 deletions
|
|
@ -1,17 +1,23 @@
|
||||||
|
type ResolveFunction<T> = undefined extends T
|
||||||
|
? (value?: T) => unknown
|
||||||
|
: (value: T) => unknown;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A type-safe utility function to create a Promise with resolve and reject functions.
|
* A type-safe utility function to create a Promise with resolve and reject functions.
|
||||||
* @returns A tuple containing a Promise, a resolve function, and a reject function.
|
* @returns A tuple containing a Promise, a resolve function, and a reject function.
|
||||||
*/
|
*/
|
||||||
export function createPromise<T = unknown>(): [
|
export function createPromise<T = unknown | undefined>(): [
|
||||||
Promise<T>,
|
Promise<T>,
|
||||||
(value: T) => unknown,
|
ResolveFunction<T>,
|
||||||
(error: unknown) => unknown
|
(error: unknown) => unknown
|
||||||
] {
|
] {
|
||||||
let resolve: undefined | ((resolved: T) => unknown) = undefined;
|
let resolve: undefined | ResolveFunction<T> = undefined;
|
||||||
let reject: undefined | ((error: unknown) => unknown) = undefined;
|
let reject: undefined | ((error: unknown) => unknown) = undefined;
|
||||||
|
|
||||||
const creationPromise = new Promise<T>(
|
const creationPromise = new Promise<T>(
|
||||||
(resolve_, reject_) => ((resolve = resolve_), (reject = reject_))
|
(resolve_, reject_) => (
|
||||||
|
(resolve = resolve_ as ResolveFunction<T>), (reject = reject_)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue