react-use icon indicating copy to clipboard operation
react-use copied to clipboard

Improve useAsync's typings

Open dutziworks opened this issue 3 years ago • 0 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

This makes useAsync infer the type of the promise that's passed to it, and propagate it onwards to value, even when the function passed is not async.

Before:

image

After:

image

Here is the diff that solved my problem:

diff --git a/node_modules/react-use/lib/misc/types.d.ts b/node_modules/react-use/lib/misc/types.d.ts
index bc1a776..fbb2ec6 100644
--- a/node_modules/react-use/lib/misc/types.d.ts
+++ b/node_modules/react-use/lib/misc/types.d.ts
@@ -1,2 +1,2 @@
 export declare type PromiseType<P extends Promise<any>> = P extends Promise<infer T> ? T : never;
-export declare type FunctionReturningPromise = (...args: any[]) => Promise<any>;
+export declare type FunctionReturningPromise<T> = (...args: any[]) => Promise<T>;

This issue body was partially generated by patch-package.

dutziworks avatar Jun 18 '22 07:06 dutziworks