Usage with redux-saga
See original GitHub issueI use redux-saga. Currently, I have to define a type in addition to my action:
export type CreateUserRequestedAction = Action<
ActionTypeKeys.CreateUserRequested,
User
>;
export const createUserRequested = createAction(
ActionTypeKeys.CreateUserRequested,
resolve => (user: User): CreateUserRequestedAction => resolve(user),
);
I need this CreateUserRequestedAction type to use this later in my saga for the action parameter:
export function* userEditSaga(action: CreateUserRequestedAction) {
try {
// define type explicitly because of lack of return types of generators:
// https://github.com/redux-saga/redux-saga/issues/1286#issuecomment-482866473
const fetchedUser: User = yield call(userEditRequest, {
firstName: 'john',
lastName: 'doe',
});
yield put(createUserSucceeded(fetchedUser));
} catch (error) {
yield put(createUserFailed());
}
}
export const userSagas = [
takeLatest(ActionTypeKeys.CreateUserRequested, userEditSaga),
];
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
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
About - Redux-Saga
redux -saga is a library that aims to make application side effects (i.e. asynchronous things like data fetching and impure things like accessing...
Read more >Why You Should Use Redux Saga - Igor Izraylevych
Redux Saga is an alternative approach to the organization of side effects. Instead of dispatching functions processed by Redux Thunk you create saga...
Read more >Introduction to Redux Saga | LoginRadius Blog
Redux Saga is a middleware library used to allow a Redux store to interact with resources outside of itself asynchronously. This includes making...
Read more >Understanding Redux Saga: From action creators to sagas
Using Redux Saga to work with side effects ... Redux Saga is a library that aims to make side effects easier to work...
Read more >Handling Middleware With Redux-Saga - Telerik
Redux is a central data store for all the data of an application. It helps any component from the application access the data...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi @npeham
There is a type helper called
ActionTypethat can be used to infer action type that wrapped in given action creator/reducer.https://github.com/thebrodmann/deox/blob/3fbdfd25500922410ceeff8d7eff95d1b6e5a780/src/types.ts#L35-L39
Also, there is a helper function named
getTypewhich returns the type property of an action that wrapped in given action creator.https://github.com/thebrodmann/deox/blob/3fbdfd25500922410ceeff8d7eff95d1b6e5a780/src/get-type.ts#L1-L13
According to the above helpers, your example can be re-write in the following way to hide all that type verbosity:
Yeah, I guess it makes it more readable. Thanks.