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.

[bug: typescript] ActionCreator lose it's arguments type

See original GitHub issue

What is the current behavior?

https://github.com/reduxjs/redux/blob/beb1fc29ca6ebe45226caa3a064476072cd9ed26/test/typescript/actionCreators.ts#L13-L18

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:open
  • Created 4 years ago
  • Reactions:9
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
antmelnykcommented, Apr 22, 2020

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:

export const doSomething: ActionCreator<AppAction> = (value: number) => {
    return {
        type: TYPE.DO_SOMETHING,
        value
    };
};

Do this:

export const doSomething = (value: number): AppAction => {
    return {
        type: TYPE.DO_SOMETHING,
        value
    };
};

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:

export const doSomething: ThunkActionCreator = (value: number) => {
    return dispatch => {
        dispatch(anotherActionCreator());
    };
};

Do this:

export const doSomething = (value: number): ThunkActionType => {
    return dispatch => {
        dispatch(anotherActionCreator());
    };
};

Where ThunkActionType is this:

(dispatch: ThunkDispatch<AppState, any, AppAction>, getState?: () => AppState) => void
2reactions
markeriksoncommented, Aug 6, 2020

@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

Read more comments on GitHub >

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

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