Replies: 2 comments 2 replies
-
Following that pattern pretty much implies that it would be wrong to dispatch actions that no reducer is actively listening to - but that is totally okay. In a Redux store, there are always additional actions you did not dispatch, but that will arrive in your Reducers, so your reducers have to deal with unknown actions. Thus, it is perfectly fine to dispatch unknown actions. Also, it is a lot of work to "collect" these union action types, and we really don't want people to start another pattern that adds boilerplate with very little value. If you really want to make sure that only "correct" actions are dispatched in your code base, I'd recommend using a linter to forbid using inline-specified actions and only use action creators. That gets you 95% there without an additional line of code. PS: Calling |
Beta Was this translation helpful? Give feedback.
-
If anyone is curious, I settled on defining the following types in my code. This dispatch type will prevent you from forgetting to call an action creator. Note that I don't use thunks. import type { Dispatch } from 'react'
export interface MyAction {
type: string
payload?: unknown
meta?: unknown
}
export type MyDispatch = Dispatch<MyAction> |
Beta Was this translation helpful? Give feedback.
-
The following code shows how to make your dispatch function type safe with a type helper called
ActionType
. Would it make sense to include this helper in RTK? If so, I can PR.This is heavily inspired by typesafe-actions.
(https://github.com/srmagura/redux-action-type-poc)
Beta Was this translation helpful? Give feedback.
All reactions