redux-toolkit: Error with tsc when attempting to import types from RTK Query using @reduxjs/toolkit 2.0.1
Sample repo reproducing the issue can be found here: https://github.com/ryerrappa/rtk-query-type-import-issue
- @reduxjs/tookit 2.0.1
- Vite 5.0.0
- Typescript 5.2.2
- tsconfig moduleResolution “bundler”
Receive the following error when attempting to import the MutationTrigger
type.
Cannot find module @reduxjs/toolkit/dist/query/react/buildHooks
Using vite with the following tsconfig.
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Comments: 18 (1 by maintainers)
Commits related to this issue
- Fix #3970 - Expose types allowing library consumers to build generic utils on top of @reduxjs/toolkit. — committed to lcharlois-neotys/redux-toolkit by lcharlois-neotys 5 months ago
Hello,
I’m facing the same kind of issue with the UseQuery type.
The use case is globally the same, having a utility method doing generic things on hooks results.
It will be nice to see these types being exported if possible.
The type helpers from https://github.com/reduxjs/redux-toolkit/pull/4147 have been released in version 2.2.0.
For what it’s worth @kThayer2 we generally recommend using the real hooks and real network requests, and intercepting using MSW, rather than mocking hooks.
I’m in the same boat as @lcharlois-neotys. I have a function that expects a query hook as a parameter and acts as a utility to handle various status changes and extract some common data. It looks something like this:
It would be nice to have a type to easily encapsulate what a useWhateverQuery hook looks like. I’ve worked around it with my own type of
(arg?: QueryArg) => TypedUseQueryHookResult<ResultType, QueryArg, BaseQueryFn>
, but it would be nice to have a user accessible type for what a useSomethingQuery() looks like.I created a PR exposing one more type: https://github.com/reduxjs/redux-toolkit/pull/4289, merging would allow me to upgrade to RTK 2!
@tylerlaprade
TypedUseMutation
is exported, it just takes different (friendlier) generics.@tylerlaprade I’m working on a PR to expose more types.
For
EndpointBuilder
andEndpointDefinitions
you can already import them like this as they are exported.for what it’s worth, with passing triggers I tend to just type them based on how I intend to use them. For example, with the above example:
that way, you’re putting forward a minimum contract you expect from the trigger, and Typescript can compare your trigger against the contract to make sure it’s compatible.