react-admin
react-admin copied to clipboard
TypeScript: determine DataProvider RecordType based on resource name
Is your feature request related to a problem? Please describe.
Forgive me if I am incorrect here, but it seems... to have properly typed return values, DataProvider methods require both a resource string and a matching RecordType type parameter at the call site, e.g.
// data has type any[]
const { data } = await dataProvider.getList('orders');
// data has type Order[]
const { data } = await dataProvider.getList<Order>('orders');
Describe the solution you'd like
It would be nice if DataProvider knew the mapping between resource names and resource types. For example,
// Ideally data has type Order[] here
const { data } = await dataProvider.getList('orders');
Describe alternatives you've considered
I tried a few workarounds with a custom DataProvider to no avail.
Additional context
- https://stackoverflow.com/questions/54165536/typescript-function-return-type-based-on-input-parameter
- https://stackoverflow.com/questions/52817922/typescript-return-type-depending-on-parameter
- https://stackoverflow.com/questions/56505560/how-to-fix-ts2322-could-be-instantiated-with-a-different-subtype-of-constraint/59363875#59363875
Hi Wes,
I actually tried to achieve that but all the solutions I tried had several drawbacks. I'm still working on it but I would appreciate some help!
Thinking more about this problem, the way that folks handle this in, e.g., Apollo is with wrapper hooks, often via automatic code generation.
For example...
const { data } = await dataProvider.orders.getList();
We've explore the possible solutions further, and the only viable one uses code generation... Which we're not ready to pursue for now. So I'm closing this issue.
If you ever want to work on an implementation, please open a PR, and we'll look at it with great attention.