-
Hey all ! I am using redux toolkit without a global store, but only with If I were doing it manually, I would do something like this: type DoSomethingAction = {
type: "DO_SOMETHING",
payload: string
}
...
interface Props {
dispatch: (a: DoSomethingAction) => void
} The best I can come with when using const aSlice = createSlice({
/*...*/
reducers: {
doSomething(state, a: PayloadAction<string>) {
/*...*/
}
})
const doSomething = aSlice.actions.doSomething
/*...*/
interface Props {
dispatch: (a: ReturnType<typeof doSomething>) => void
} Seeing the comments in #771 , I guess it is not the proper way to do it, and it’s not really nice. What would be a better way to achieve this ? If I were using a global store, according to the doc I could do: export type AppDispatch = typeof store.dispatch
export const useAppDispatch = () => useDispatch<AppDispatch>() It would not match exactly my requirements, because I'd rather be explicit about the only actions my dispatch function is supposed to receive, but it would be close enough. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The correct type for that reducer would be That is also what you would get from store - with additional middleware typings, which don't play a role in your case. |
Beta Was this translation helpful? Give feedback.
The correct type for that reducer would be
Dispatch<AnyAction>
. It doesn't matter what is dispatched as long as it is a valid action - some actions will just be ignored while others won't be.That is also what you would get from store - with additional middleware typings, which don't play a role in your case.