Search code examples
typescriptreact-admin

Using DataProvider in ReactAdmin, how to fix an errorr: the expected type comes from the return type of this signature


I'm following the tutorial to implement dataprovider in react-admin. Here is the link: https://marmelab.com/react-admin/Tutorial.html#connecting-to-a-real-api

Below is the sample implementation of tutorial

  create: (resource, params) =>
    httpClient(`${apiUrl}/${resource}`, {
        method: 'POST',
        body: JSON.stringify(params.data),
    }).then(({ json }) => ({
        data: { ...params.data, id: json.id },
    })),

But in typescript it raise an error the expected type comes from the return type of this signature.

How can I fix the error?


Solution

  • According to this PR, you could type data with any as follows:

    create: (resource, params) =>
        httpClient(`${apiUrl}/${resource}`, {
            method: 'POST',
            body: JSON.stringify(params.data),
        }).then(({ json }) => ({
            data: { ...params.data, id: json.id } as any,
        })),
    

    It should fix the TS error you're facing.