How to properly use TypeScript with redux-actions?
See original GitHub issueHi.
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:
- Created 5 years ago
- Reactions:33
- Comments:21 (2 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
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.
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?