question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage with redux-saga

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
the-dr-lazycommented, May 1, 2019

Hi @npeham

There is a type helper called ActionType that 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 getType which 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:

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)
]
1reaction
SantoJambitcommented, Mar 4, 2020

Yeah, I guess it makes it more readable. Thanks.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found