Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make usePromise strongly-typed #13

Open
ryanberckmans opened this issue Jun 17, 2020 · 2 comments
Open

Make usePromise strongly-typed #13

ryanberckmans opened this issue Jun 17, 2020 · 2 comments

Comments

@ryanberckmans
Copy link

Is it possible to make usePromise strongly typed?

Currently it's

function usePromise (
  promise: (...inputs: any) => any,
  inputs: Array<any>,
  lifespan: number = 0
): any

Perhaps a strongly typed flavor could be provided, such as

function usePromise<T, A>(
  promise: (A) => Promise<T>,
  inputs: A,
  lifespan: number = 0
): T

This might require some type coercion under the hood because the global singleton const promiseCaches: PromiseCache[] can't use type T above.

@ngocdaothanh
Copy link

ngocdaothanh commented Feb 6, 2021

I would like to have this typed feature, because the current usePromise cannot catch type error like this:

function getApple(id: string): Promise<Apple> {...}

function useOrange(id: string): Orange {
  return usePromise(getApple, [id])  // <-- It returns `any`, so type error cannot be caught
}

I'm not sure it it's necessary, but probably we need to use Parameters and ReturnType:
https://www.typescriptlang.org/docs/handbook/utility-types.html

@msereniti
Copy link

msereniti commented Jan 20, 2022

You can use fork of this package that solves this issues and many others react-use-await

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants