question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

hooks: useUpdate onError signature typescript issue

See original GitHub issue

Since the v4 upgrade of react-admin, I have the following typing issue on the useUpdate function when I try to access the error.message property of the parameter provided by the onError function:

TS2571: Object is of type 'unknown'.
    23 |     {
    24 |       onError: (error) => {
  > 25 |         notify(`Error while updating: ${error.message}`);
       |                                         ^^^^^
    26 |       }
    27 |     }
    28 |   )

I also try to specify the type of the error like that: (error: Error) => ..., but this leads to the following another error:

TS2322: Type '(error: Error) => void' is not assignable to type '(error: unknown, variables: Partial<UseUpdateMutateParams<RaRecord>>, context: unknown) => void | Promise<unknown>'.
  Types of parameters 'error' and 'error' are incompatible.
    Type 'unknown' is not assignable to type 'Error'.
    22 |     },
    23 |     {
  > 24 |       onError: (error: Error) => {
       |       ^^^^^^^
    25 |         notify(`Error while updating: ${error.message}`);
    26 |       }
    27 |     }

Steps to reproduce:

  1. Clone the referenced PR below
  2. Run npm start

Related code:

Sandbox PR: https://github.com/soullivaneuh/react-admin-sandbox/pull/1

Other information:

Environment

  • React-admin version: v4.0.3
  • Last version that did not exhibit the issue (if applicable): v3
  • React version: v18

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
soullivaneuhcommented, May 16, 2022

@slax57 I found a way to resolve the issue: https://github.com/marmelab/react-admin/pull/7698

Would you mind take a look? 😃

1reaction
soullivaneuhcommented, May 16, 2022

@slax57 Well… 🙃

Next time I’ll re-read my copy/paste, thanks! 😉

My setup:

const [setup, { isLoading }] = useUpdate(
  'users',
  {
  },
  {
    onSuccess: () => {
      refresh();
    },
    onError: (error) => {
      const message = error instanceof Error ? error.message : error instanceof String ? error : undefined;
      notify(`Echec : ${message}`, {
        type: 'error',
      });
      refresh();
    },
  },
);

Works! However, this double ternary shows indeed the need of a stronger typing to be provided by useUpdate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[TypeScript] Fix cannot specify the error type in mutation hooks by ...
Provides a way to set the expected type of the error instance that will be propagated to the related functions. closes #7653.
Read more >
How to solve customHooks call signature error in react with ...
My question is I would like to use the custom hooks (useCardContext) without the error. How I can solve this issue? reactjs ·...
Read more >
React-admin - Querying the API - Marmelab
Querying the API. React-admin provides special hooks to emit read and write queries to the dataProvider , which in turn sends requests to...
Read more >
Hooks - Apollo GraphQL Docs
A callback function that's called when your query successfully completes with zero errors (or if errorPolicy is ignore and partial data is returned)....
Read more >
react-admin - Projects | FOSSA
License scan found 10 issues. Last scanned 4 hours ago. Found dependencies 15 levels deep. 1665 dependencies · 88 licenses · 11 obligations ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found