[bug: typescript] ActionCreator lose it's arguments type
See original GitHub issueWhat is the current behavior?
We declare the addTodo
function that accepts one string argument text
,
but we could pass any argument to it.
const addTodoAction: AddTodoAction = addTodo(false, 0, null);
👆👆👆 //no ts error!
There may be a problem (...args: any[])
https://github.com/reduxjs/redux/blob/c676a25525bd7e4f2bf407cf1a135f355a2c1d79/index.d.ts#L440-L442
What is the expected behavior?
Raise a type error
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:11 (6 by maintainers)
Top Results From Across the Web
False positives from TypeScript for Redux Action Creator's ...
I've added a TypeScript type to my function parameter. I don't know if it matters but this function is a Redux Action Creator:...
Read more >Usage With TypeScript - Redux
Reducers are pure functions that receive the current state and incoming action as arguments, and return a new state. If you are using...
Read more >Redux & Typescript — Reuse the type of an Action Creator's ...
Redux & Typescript — Reuse the type of an Action Creator's return value ... I don't want to lose information about a set...
Read more >Do not create union types with Redux Action Types. It's most ...
It's most likely an antipattern. So, there is a long-standing TypeScript & Redux pattern of creating a union RootAction type that contains all ......
Read more >NgRx creator functions 101 - Tim Deschryver
Therefore we use the ReturnType utility type of TypeScript, this will get us the return value of the action creator. In other words...
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
A very sad issue, in fact, a frustrating one.
The easiest way to fix this: just do not type the action creator itself and do not use this particular Redux typing, but instead type function’s returning value directly with your Action type.
So instead:
Do this:
For redux-thunk the same approach, but it may differ based on how you actually type thunks in your app.
In my case it was instead this:
Do this:
Where
ThunkActionType
is this:@chaabaj, @pixelgoo : we specifically recommend using our official Redux Toolkit package, which A) is already written in TS, and B) will auto-generate action creators for you:
https://redux-toolkit.js.org