-
-
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
Specify data provider methods as generic (not just their arguments) for type safety #6143
Comments
I'm not sure to understand where you want to add these generics. We already have them in interface Product extends Record {
name: string;
sku: string;
}
const dataProvider = useDataProvider();
dataProvider.getOne<Product>('resources', 123)
.then(({ data }) => {
// TS sees data as Product
}) We can't add these generics in
const { ids, data, total, loaded } = useGetList<Product>(
'posts',
{ page: 1, perPage: 10 },
currentSort
);
// data is of type RecordMap<Product> So the only places left to add them are the remaining the specialized hooks like |
Yes also on the static create & update methods and allow to specify both the returned recordType and the type of the Currently eg
So suggestions are:
And similar for So with the overloads in the data provider, since you don't need to specify generics on the call site, a react-admin form constructed for a given resource maybe could also show an error if a required property was forgotten or a non-existing one included as you'd be able to detect that the data provider doesn't receive |
I've updated the specialized dataProvider hooks in #6168. As for moving the types from components to the dataProvider and through the form, I don't think it's possible due to the dynamic programming techniques we're using. Feel free to open a PR if you find a way to do so. |
Thanks but the typing is not as strict as what I requested. I see in the code it is now:
So:
|
I didn't understand that sentence:
Can you give examples of what you mean? As for the second point, please open a PR to implement what you want, we'll see if it makes sense to merge it in the core. |
Issue 1. I thought it was explained in my original post. We declare overloaded data provider methods based on the resource. This works if the
The TS compiler will now select the correct overloaded function based on the resource and since the declarations in the data provider specify the generic params for each resource, see my original post, you don't need to specify these when you call the methods. So easier and no risk to specify wrong type for given resource. I don't think you still need to have Issue 2. I think the benefit of what I propose is clear, right and there is no downside since the 2nd generic argument (ie the Example: |
I understand that you want to be much more type safe than what we currently target in react-admin. So please submit a PR if you feel you can contribute what you did to the core, but we won't work further on this subject for now. |
OK understood. |
I landed here today, because I wanted to run the demo application as described here https://github.com/marmelab/react-admin/tree/master/examples/demo I followed all the steps, and on the last step
I can click away the error, but have not found a resolution yet |
@WinstonN, |
Fix incoming #7045 |
Is your feature request related to a problem? Please describe.
When manually calling data provider methods, or when they are called by react-admin behind the scenes, there is no type safety for input data, or returned data.
Describe the solution you'd like
1/ Provide the option to make all data provider methods type-safe by making them generic, so that at least when they are called manually it can be type-safe.
2/ Provide type-safety also when react-admin internally calls these methods. This would lead to a great DX where you could create a form for object create and it would err when a required property is forgotten, or a form for object update that would err if a property that cannot be updated is included.
Describe alternatives you've considered
We have coded 1/ ourselves in our data providers and how we call them manually, but it would probably be useful to enhance the docs with this (now that you have #5934)
eg. for
create
withResourceName
as a union of literal types which also ensures no non-existing resource names can be passed:and then call, for resource
Shop
as:Then if you add overloaded declarations in your data provider for each of the narrowed resource types:
you can call without generics on the call site and get full type safety:
To avoid all the overloaded definitions, I tried first to do this with some kind of mapped/conditional type but that that wasn't successful so far.
Additional context
This builds further upon #5934 which introduced generics for the arguments to the data provider methods but not the methods themselves.
The text was updated successfully, but these errors were encountered: