Calling an action from another action in a redux store #379

Closed
opened 2025-08-09 17:16:03 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @ghost on 10/6/2017

I'm sorry if this is the wrong place for this, I had a look at the README.md but I didn't see anything about issue creation rules.

I have a simple store, and I want to call one action from another action:

I check the redux documentation and it seemed straight forward enough.

dispatch(addTodo(text))

Here's a minimal repro of what I thought should work:

import { AppThunkAction } from "./";

enum ActionTypes { CreateRequest = "CREATE_REQUEST", CreateReceive = "CREATE_RECEIVE", LoadRequest = "LOAD_REQUEST", LoadReceive = "LOAD_RECEIVE" }

interface CreateRequestAction { type: ActionTypes.CreateRequest; }
interface CreateReceiveAction { type: ActionTypes.CreateReceive; }
interface LoadRequestAction { type: ActionTypes.LoadRequest; }
interface LoadReceiveAction { type: ActionTypes.LoadReceive; data: any }

type KnownAction = CreateRequestAction | CreateReceiveAction | LoadRequestAction | LoadReceiveAction;

export const actionCreators = {
    createProperty: (newItem: any): (AppThunkAction<KnownAction>) => (dispatch, getState) => {
        //Pretend to do something
        setTimeout(() => {
            dispatch({
                type: ActionTypes.CreateReceive
            });

            //This produces an error: Error	TS2345	(TS) Argument of type 'AppThunkAction<KnownAction>' is not assignable to parameter of type 'KnownAction'
            dispatch(actionCreators.load());
        }, 1000);

        dispatch({
            type: ActionTypes.CreateRequest
        });
    },
    load: (): (AppThunkAction<KnownAction>) => (dispatch, getState) => {
        //Pretend to do something
        setTimeout(() => {
            dispatch({
                type: ActionTypes.LoadReceive,
                data: []
            });
        }, 1000);

        dispatch({
            type: ActionTypes.LoadRequest
        });
    },
};

Which produces the error:

Error TS2345 (TS) Argument of type 'AppThunkAction' is not assignable to parameter of type 'KnownAction'.
Type 'AppThunkAction' is not assignable to type 'LoadReceiveAction'.
Property 'type' is missing in type 'AppThunkAction'.

So it seems that the AppThunkAction<KnownAction> typing is not permissive enough to allow the call as shown.

This issue can be worked around by casting dispatch to any:

const untypedDispath = <any>dispatch;
untypedDispath(actionCreators.load());

But it'd be nice to have a typed solution.

*Originally created by @ghost on 10/6/2017* I'm sorry if this is the wrong place for this, I had a look at the README.md but I didn't see anything about issue creation rules. I have a simple store, and I want to call one action from another action: I check the [redux documentation](http://redux.js.org/docs/basics/Actions.html#action-creators) and it seemed straight forward enough. dispatch(addTodo(text)) Here's a minimal repro of what I thought should work: import { AppThunkAction } from "./"; enum ActionTypes { CreateRequest = "CREATE_REQUEST", CreateReceive = "CREATE_RECEIVE", LoadRequest = "LOAD_REQUEST", LoadReceive = "LOAD_RECEIVE" } interface CreateRequestAction { type: ActionTypes.CreateRequest; } interface CreateReceiveAction { type: ActionTypes.CreateReceive; } interface LoadRequestAction { type: ActionTypes.LoadRequest; } interface LoadReceiveAction { type: ActionTypes.LoadReceive; data: any } type KnownAction = CreateRequestAction | CreateReceiveAction | LoadRequestAction | LoadReceiveAction; export const actionCreators = { createProperty: (newItem: any): (AppThunkAction<KnownAction>) => (dispatch, getState) => { //Pretend to do something setTimeout(() => { dispatch({ type: ActionTypes.CreateReceive }); //This produces an error: Error TS2345 (TS) Argument of type 'AppThunkAction<KnownAction>' is not assignable to parameter of type 'KnownAction' dispatch(actionCreators.load()); }, 1000); dispatch({ type: ActionTypes.CreateRequest }); }, load: (): (AppThunkAction<KnownAction>) => (dispatch, getState) => { //Pretend to do something setTimeout(() => { dispatch({ type: ActionTypes.LoadReceive, data: [] }); }, 1000); dispatch({ type: ActionTypes.LoadRequest }); }, }; Which produces the error: > Error TS2345 (TS) Argument of type 'AppThunkAction<KnownAction>' is not assignable to parameter of type 'KnownAction'. > Type 'AppThunkAction<KnownAction>' is not assignable to type 'LoadReceiveAction'. > Property 'type' is missing in type 'AppThunkAction<KnownAction>'. So it seems that the `AppThunkAction<KnownAction>` typing is not permissive enough to allow the call as shown. This issue can be worked around by casting dispatch to any: const untypedDispath = <any>dispatch; untypedDispath(actionCreators.load()); But it'd be nice to have a typed solution.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#379
No description provided.