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.

How to properly use TypeScript with redux-actions?

See original GitHub issue

Hi.

I couldn’t get my typings to work with redux-actions. Would be great to have a section in the documentation showing the usage.

It’s always returning a failure of typings, I don’t understand what else I need to do in order to make it work.

This “works”:

const reducer: Reducer<State> = handleActions<State>(
  {
    [actions.request.toString()]: (state: State, payload) => ({
      ...state,
      someData: payload,
    }),
   initialState,
);

The problem starts when I try to add types to my payload. None of below worked:

const reducer: Reducer<State> = handleActions<State>(
  {
    [actions.request.toString()]: (state: State, payload: Payload) => ({
      ...state,
      someData: payload,
    }),
   initialState,
);

const reducer: Reducer<State> = handleActions<State, Payload>(
  {
    [actions.request.toString()]: (state: State, payload: Payload) => ({
      ...state,
      someData: payload,
    }),
   initialState,
);

I’m having the same difficulties for using createActions.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:33
  • Comments:21 (2 by maintainers)

github_iconTop GitHub Comments

48reactions
the-dr-lazycommented, Jan 20, 2019

Hello all.

I don’t think the pattern of redux-actions can give a full type inferring capability. at least not yet. Also, there is no official support for typescript in redux-actions. All of these makes using redux-actions in typescript hard and misleading.

That’s why I have just published thebrodmann/deox as a good alternative to redux-actions for typescript use cases with full power of type inferring without any type information losing. I hope it makes sense.

26reactions
jake-danielscommented, May 12, 2018

I agree with @karol-majewski here. We use redux-actions in production together with TypeScript. The redux store should be one of the key parts of the system with bulletproof types. Could you, guys, take a look at it, adjust the types and make it work also with Redux 4?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Usage With TypeScript - Redux
All generated actions should be defined using the PayloadAction<T> type from Redux Toolkit, which takes the type of the action.payload field as ...
Read more >
How to type Redux actions and Redux reducers in TypeScript?
Use actionCreator<P> to define your actions and action creators: export const helloWorldAction = actionCreator<{foo: string}>('HELLO_WORLD'); export ...
Read more >
How to Use Redux in Your React TypeScript App
In this guide, I will show you how to use Redux in your React TypeScript project by building an app that allows you...
Read more >
How to use Redux with TypeScript ? - DEV Community ‍ ‍
How to setUp a react-typescript project; How to setUp a redux store in our project · React; Redux · types/react-redux : for defining...
Read more >
Redux Toolkit with Typescript: How to Get Started
It is essential to highlight that a learning curve is necessary to use both of these approaches in the best way possible. This...
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