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

useAsync* can't be used with typescript effectively #484

Open
fabiancook opened this issue Jul 24, 2019 · 5 comments
Open

useAsync* can't be used with typescript effectively #484

fabiancook opened this issue Jul 24, 2019 · 5 comments
Labels
bug Something isn't working enhancement New feature or request
Projects

Comments

@fabiancook
Copy link

If we look at the definition of useAsyncFn or useAsync we can see that args is defined as Args | [] which means that the function passed must have only optional arguments

export default function useAsync<Result = any, Args extends any[] = any[]>(fn: (...args: Args | []) => Promise<Result>, deps?: DependencyList): import("./useAsyncFn").AsyncState<Result>;

To correct, we would remove | [] from args for all these functions, which will allow typescript to work out what Args should be, either an empty array or the parameters given.

You can see the two different generic arguments here...

http://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABBEAnVBTMUA8AlDAZxABspEBeRAQzAE8AaRAQVQHNDEMAPKLAE0606AbQC6lGvXEA+ABTAwALkRyAdBurtCK1h0QAfROICUlGYgLEyZgN4AoRE8SYoaJIoDc9gL6J79qCQsAhc3AAOGNAY-PhEpORUwkx6nDx8YIJSohJJ0mLyiirqmtq62mYUFlYJdo7Oru6IXr7+9ijoWFByctQqhFCoMGBslRbUJnIARFMm3h2Y2D3UAPz9g8Oj5jSTM3PtaIvdvWuIA0MjYzty+wtdPVd7N972PJHR-MvrF1tV13veN5RPifE7fTZXCbTWaAiLAmLLU7nCHbKH7IEfB7bJ77exAA

This issue makes all functions that use Args | [] unusable for me when using typescript.

Here is an example of what I'm running into...

image

@streamich streamich added bug Something isn't working enhancement New feature or request labels Jul 27, 2019
@streamich streamich added this to To do in react-use via automation Jul 27, 2019
@streamich
Copy link
Owner

Agreed, I believe | [] is not needed there.

@xobotyi
Copy link
Contributor

xobotyi commented Sep 4, 2019

Should be fixed with #589

@whinc
Copy link
Contributor

whinc commented Feb 23, 2020

I have create a PR #990 to fix it on version 13 without breaking changes. Hope to be review @xobotyi

@kalbert312
Copy link

I created PR #1104. Works with both cases for me.

@JoeDuncko
Copy link

Hi all! @react-hookz/web, the new library by one of react-use's former maintainers (background here and here) has implemented a useAsync hook that is much more ergonomic and typescript friendly. You might want to check it out!

For those interested, there's an official migration guide for migrating from react-use to @react-hookz/web.

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
react-use
  
To do
Development

No branches or pull requests

6 participants