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

Typing issue with useAsync and TypeScript #256

Open
samit4me opened this issue Feb 18, 2020 · 6 comments
Open

Typing issue with useAsync and TypeScript #256

samit4me opened this issue Feb 18, 2020 · 6 comments

Comments

@samit4me
Copy link

Experiencing TypeScript errors when using useAsync version 10.0.0.

I feel like I'm probably doing something stupid, but I can replicate and seems like an issue.

Repo demonstrating the issue: https://github.com/samit4me/react-async-ts-issue


Given the data fetching method:

async function dataFetching(
  { id }: Props,
  { signal }: AbortController
): Promise<UserResponse> {
  const res = await fetch(`https://reqres.in/api/users/${id}`, { signal });
  if (!res.ok) throw new Error(res.statusText);
  const { data } = await res.json();
  return data;
}

When using useAsync via the shorthand method:

const { data, error, isPending } = useAsync(dataFetching, { id });

I get the following error:

Argument of type '({ id }: Props, { signal }: AbortController) => Promise<UserResponse>' is not assignable to parameter of type 'PromiseFn<UserResponse>'.
  Types of parameters '__0' and 'props' are incompatible.
    Property 'id' is missing in type 'AsyncProps<UserResponse>' but required in type 'Props'.ts(2345)

When using useAsync via the configuration object:

const { data, error, isPending } = useAsync({ promiseFn: dataFetching, id });

I get the following error:

No overload matches this call.
  Overload 1 of 2, '(options: AsyncOptions<UserResponse>): BaseAsyncState<UserResponse, AbstractState<UserResponse>>', gave the following error.
    Type '({ id }: Props, { signal }: AbortController) => Promise<UserResponse>' is not assignable to type 'PromiseFn<UserResponse>'.
      Types of parameters '__0' and 'props' are incompatible.
        Property 'id' is missing in type 'AsyncProps<UserResponse>' but required in type 'Props'.
  Overload 2 of 2, '(promiseFn: PromiseFn<any>, options?: AsyncOptions<any> | undefined): BaseAsyncState<any, AbstractState<any>>', gave the following error.
    Argument of type '{ promiseFn: ({ id }: Props, { signal }: AbortController) => Promise<UserResponse>; id: number; }' is not assignable to parameter of type 'PromiseFn<any>'.
      Object literal may only specify known properties, and 'promiseFn' does not exist in type 'PromiseFn<any>'.ts(2769)
User.tsx(5, 3): 'id' is declared here.
index.d.ts(82, 5): The expected type comes from property 'promiseFn' which is declared here on type 'AsyncOptions<UserResponse>'
@stphnlngdncoding
Copy link

Running in to this as well. Is there something we're missing here?

@stphnlngdncoding
Copy link

stphnlngdncoding commented Mar 19, 2020

I got around this by using the PromiseFn to type my async function from the lib as seen here: https://github.com/async-library/react-async/blob/next/examples/with-typescript/src/App.tsx#L14

@TroySchmidt
Copy link

The MUI project has a passthru typing that might work here. Their makeStyles createStyles functions take your named properties and pass them thru successfully so that you get the named properties thru. Right now we cannot pass additional properties and have them pass thru to the async functions because it is just string key any typing. It is somehow done using typing with Record but it is beyond my package typing skills at this moment.

@ghengeveld
Copy link
Member

I'm not exactly a TypeScript expert. I'd be happy to take a PR to resolve these issues. There's this one still waiting for someone with actual TS knowledge to review it: #247

Oh, the tragedy of converting to TS. Now I'm constantly resolving TS issues without knowing what I'm doing, instead of building new features and improving the lib itself.

@moroine
Copy link

moroine commented Jul 20, 2020

I think api wise it would be better to have in callback separate param between options and context related to the lib

@johnnyreilly
Copy link

For those battling with TypeScript support, there is https://www.npmjs.com/package/react-async-hook

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

6 participants