Closed npeham closed 5 years ago
Hi @npeham
There is a type helper called ActionType
that can be used to infer action type that wrapped in given action creator/reducer.
Also, there is a helper function named getType
which returns the type property of an action that wrapped in given action creator.
According to the above helpers, your example can be re-write in the following way to hide all that type verbosity:
import { createActionCreator, getType, ActionType } from 'deox'
export const createUserRequested = createActionCreator(
'CREATE_USER_REQUESTED',
resolve => (user: User) => resolve(user),
);
export function* userEditSaga(action: ActionType<typeof createUserRequested>) {
// ...
}
export const userSagas = [
takeLatest(getType(createUserRequested), userEditSaga)
]
Tomorrow, I will try to make a codesandbox example of usage with redux-saga like the example of usage with redux-thunk.
The example can be found in codesandbox. I will close the issue for housekeeping purposes. Feel free to re-open it if needed.
Thank you very much this is awesome :tada:
Please keep up the great work! 👍
I've been using ReturnType without issues so far. Is the only benefit of using ActionType over ReturnType that it returns never
if the passed in type is not an action creator? Or am I missing some big benefit here?
Or am I missing some big benefit here?
It makes type definition to feel consistent. Every time I want to extract the TypeScript definition for an action I use ActionType
without thinking about the input is a reducer or an action creator.
Yeah, I guess it makes it more readable. Thanks.
I use redux-saga. Currently, I have to define a type in addition to my action:
I need this
CreateUserRequestedAction
type to use this later in my saga for the action parameter:Is there a better solution for this or do I need this type to gain type-safety and autocompletion in my
userEditSaga
?Thank you in advance!
Kind regards Nico