react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

TypeScript: determine DataProvider RecordType based on resource name

Open jchook opened this issue 3 years ago • 2 comments

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

jchook avatar Apr 21 '22 20:04 jchook

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!

djhi avatar Apr 22 '22 13:04 djhi

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();

jchook avatar Jul 16 '22 23:07 jchook

TypeORM

jchook avatar May 07 '23 06:05 jchook

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.

fzaninotto avatar Aug 01 '23 14:08 fzaninotto