-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Dumb dataProvider doesn't type check #5476
Comments
Thanks for reporting. Reproduced |
In the meantime, you can make it compile with import React from 'react';
-import {Admin} from "react-admin";
+import { Admin, DataProvider } from "react-admin";
const dataProvider = {
getList: (resource: string, params: any) => Promise.resolve({data: [{id: 1, toto: "tata"},{id: 2, toto: "tata"}], total: 0}),
getOne: (resource: string, params: any) => Promise.resolve({data: {id: 1, toto: "tata"}}),
getMany: (resource: string, params: any) => Promise.resolve({data: [{id: 1, toto: "tata"}]}),
getManyReference: (resource: string, params: any) => Promise.resolve({data: [{id: 1, toto: "tata"}]}),
create: (resource: string, params: any) => Promise.resolve({data: {id: 1, toto: "tata"}}),
update: (resource: string, params: any) => Promise.resolve({data: {id: 1, toto: "tata"}}),
updateMany: (resource: string, params: any) => Promise.resolve({data: [1]}),
delete: (resource: string, params: any) => Promise.resolve({data: {id: 1, toto: "tata"}}),
deleteMany: (resource: string, params: any) => Promise.resolve({data: [1]}),
-};
+} as DataProvider; |
Thanks, I confirm this "trick" works. This unblocks me at least. Leaving the issue opened since there seems to be a deeper problem. Note that the notation |
We struggled with the same and fixed as follows, to have typing of a data provider that includes custom methods:
Caveat: this may not be 100% correct to cast what returns from The main reason we use a class is since our data provider is composed of different "sub-data providers" (one for each backend API) . Basically it's an abstract class where we can do things for initialization of each sub-data provider. Another benefit is that you can use decorators on class methods (eg as in above, decorators for logging and for returning a rejected promise from a thrown error, as required by react-admin) |
It's been 7 months since this was opened. Any possibility of a fix sometime soon? This is one of the most basic use cases. |
@harishy100 This is an open-source project. Anyone can contribute. It's not a critical problem for the core team, so we dedicate time to other problems for now. If you want the bug fixed quickly, the best way is to fork the project, fix it yourself, and open a PR to have your fix merged in the react-admin code. If you don't have the time or skills to do so, consider hiring Marmelab to fix the bug for you. |
Just adding my two cents as I've also stumbled upon this in my first project with RA.
At least that's how I understood that longy explanation: https://stackoverflow.com/a/59363875 |
Had the similar issue on
And my import { GetOneParams } from 'react-admin';
// ... code
getOne: async (resource: any, { id }: GetOneParams) => {
return { data: { id } };
}, I think the key here is Setting proper return type import { GetOneParams, GetOneResult } from 'react-admin';
// ... code
getOne: async (resource: any, { id }: GetOneParams): Promise<GetOneResult> => {
return { data: { id } };
} |
The type GetOneResult is generic and cannot be instantiated with any concrete type (except any). Most people stumbling upon this would want to use a concrete GetOneResult return type like for example: interface PoiDTO extends RaRecord { id: number; geom: PointGeometry; [key: string]: any; };
async getOne(resource: string, params: GetOneParams): Promise<GetOneResult<PoiDTO>> Which is pretty much impossible. I am using the generic defaults (any) now, like @coldshine mentioned. It would be really nice to have this mentioned in the documentation somewhere... The generic type parameter for GetOneResult creates the wrong expectation to provide a concrete type for RecordType. |
@maze-le I don't understand your point. If you call |
@fzaninotto: Calling it like this works, you are right. But I have a slightly different situation: We basically have 2 backend services -- one for read-operations based on elasticsearch and one for write-operations that operates directly on the Postgres database. For this I have started to implement a custom data provider -- and this works well, except when it comes to the generic type restrictions: const poiProvider: DataProvider = {
async getOne(resource: string, params: GetOneParams): Promise<GetOneResult> {
const resourceId = params.id;
const url = `${apiBaseUrl}/${resource}/${resourceId}`;
const httpReturn: HttpReturn<PoiDTO> = await httpClient.fetch<PoiDTO>(url);
return { data: httpReturn.json };
};
// ... and so on...
} This works well. But what I wanted to do is to restrict the return type of the getOne() method to types of the type PoiDTO (which extends from RaRecord, so subtyping should be possible) like this: const poiProvider: DataProvider = {
async getOne(resource: string, params: GetOneParams): Promise<GetOneResult<PoiDTO>> {
// ...
};
// ... and so on...
} This fails, because the 'RecordType' could be instantiated with a different subtype of constraint 'RaRecord'. |
Right, gotcha. If you have an idea of how to solve this, please open a PR! |
Can we reopen this issue? The fact that you can't do this very simple typing is kind of a problem, especially if you do anything sophisticated with splitting up data providers. |
We'd love this issue to be solved, too, but we've already spent quite some time on it without finding a solution. This is somehow a TypeScript limitation. Besides, there is a working workaround. Reopening the issue won't solve it. We welcome any PR providing a solution. |
What you were expecting:
Bootstrap a dummy application with create-react-app with typescript, and implement a dummy dataProvider. It should compile just fine.
What happened instead:
Big Typescript error :
Steps to reproduce:
npx create-react-app my-app --template typescript
cd my_app
npm install react-admin
npm start
Related code:
In
App.tsx
:Other information:
Environment
The text was updated successfully, but these errors were encountered: