How to type useSelector in a shared Redux Toolkit slice inside an Nx monorepo when the component doesn't know the app’s RootState? #5145
Unanswered
michalvadak
asked this question in
Q&A
Replies: 1 comment 4 replies
-
|
At that point, yeah, you're better off just supplying a sort of partial "NotQuiteRootState" type that only reflects what the component expects will be in the store. Not truly typesafe, in that it's not coming from the actual constructed store, but it's the best you can get when you're mixing essentially dynamic app construction with static typing. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using Redux Toolkit in an Nx monorepo and I have a shared component that lives in a library and is used across multiple apps.
This component also contains its own slice, which needs to be dynamically attached to the store in whichever app uses the component.
The problem is typing:
Typed hooks like useAppSelector require access to the application's RootState, but the shared component obviously doesn’t know which store it will be plugged into.
Because of this, I'm falling back to the untyped useSelector, but that leads to errors like:
TS2345: Argument of type 'unknown' is not assignable to type '{ autoPlay: AutoPlayState; }'Example:
Is there a recommended way to type useSelector (or selectors in general) inside a shared component that does not have access to the application's RootState?
Should slices in shared libs define their own RootState fragment types somehow, or is using the untyped useSelector the expected approach in this scenario?
Beta Was this translation helpful? Give feedback.
All reactions