RTK Query typing .select function #1432
-
Hey everyone, Have recently started using RTK Query and am loving it. I had a question that I have been unable to solve myself and have been unable to find an answer for in the docs or in the forums. I am wondering if there is an exportable type from the library for the expected return response of the With a relatively simple setup like this:
How would you go about typing the following?
Or is it best to simply allow the typing to be inferred? I would like to type this to make it clear at a glance what is expected to be returned by this selector method. My understanding is that the typing should look something like the following:
Thank you very much everyone, I hope to contribute to the library at some point if this is encouraged by the maintainers :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
As a general observation, TS works best when you infer as much as possible, and that's also true for working with RTK. Note that the use of I'm pretty sure that if you just do And sure, contributions are always welcome! :) |
Beta Was this translation helpful? Give feedback.
As a general observation, TS works best when you infer as much as possible, and that's also true for working with RTK.
Note that the use of
ordersSelector
there is a bit off, becauseendpoint.select()
actually creates a new selector instance every time. Also, if you have any query args involved here, those need to be passed in asendpoint.select(sameQueryArgsHere)
so that it knows which cache entry to retrieve.I'm pretty sure that if you just do
const ordersSelector = ordersService.endpoints.getOrders.select()
, this should be typed correctly already.And sure, contributions are always welcome! :)