Proper TypeScript example for using Grid with OData

1 Answer 322 Views
Grid
Pieter
Top achievements
Rank 1
Iron
Iron
Pieter asked on 03 Aug 2021, 09:05 AM

Hi,

I was looking into the OData example (TypeScript with Hooks) of the Grid (https://www.telerik.com/kendo-react-ui/components/grid/data-operations/odata-server-operations/) and I noticed that the typing is invalid. In "products-loader.tsx" the callback function has only the products array as argument, but when that function is called later on, it has a "data" and a "total" property.

When you then add some code in the "dataReceived" function in "main.tsx" (for example iterating the products"), you'll get errors since the data coming from the "products-loader" isn't an array of products, but an object with "data" and "total".

Could you look into that (and maybe provide a correctly typed example here)?

Pieter
Top achievements
Rank 1
Iron
Iron
commented on 03 Aug 2021, 09:16 AM

As a follow up, I guess the typing of the state is also incorrect? That needs to have a "data" and "total" property as well:

const [products, setProducts] = React.useState<Product[]>([]);

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 03 Aug 2021, 09:58 AM

Hello, Pieter,

Thank you for reporting this.

Indeed, the correct type, in this case, is DataResult:

https://www.telerik.com/kendo-react-ui/components/dataquery/api/DataResult/

I updated the example as well:

https://stackblitz.com/edit/react-gabzru?file=app%2Fmain.tsx

Additionally, I have added some Telerik points to your account for burning this to our attention.

Regards,
Stefan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Pieter
Top achievements
Rank 1
Iron
Iron
commented on 04 Aug 2021, 06:49 AM

Thanks for the updated example, however, there is still an issue, you can still put anything in the "props.onDataReceived.call", probably due to the ".call" function.

 

 props.onDataReceived.call(undefined, {
            data1: json.value,
            someting: "2",
            total: json['@odata.count']
          });

 

Second question I have is: it would be very handy if Kendo does also provide a "DataResult<T>" interface, where the "data" property it typed to T, can that be considered?

Stefan
Telerik team
commented on 04 Aug 2021, 12:47 PM

Indeed, in that case, the types may not fully work. Still, please have in mind that this is only an example of one of the possible ways to achieve server operations. The developer can set this up in a different way that will best reflect the real application and provide different typization.

 

For the second question - We are discussing the idea to have a type like this to allow the developers to set their custom type `DataResult<T extends any>`. 

Tags
Grid
Asked by
Pieter
Top achievements
Rank 1
Iron
Iron
Answers by
Stefan
Telerik team
Share this question
or