Skip to content

Typescript: action type parameter for Store, Dispatch, etc. #2643

@samfrances

Description

@samfrances

Looking at the Typescript typings for Redux, it looks like there is no built-in way to type check calls to dispatch, etc. This looks like something that could be improved. For example you could envisage a store definition that looks something like the following:

interface Store<S, A extends Action = Action> {
  dispatch: Dispatch<S, A>;
  getState(): S;
  subscribe(listener: () => void): Unsubscribe;
}

export interface Dispatch<S, A extends Action = Action> {
    (action: A): A;
}

Then, with appropriate modifications elsewhere, you could something like the following:

type MyAppActions =
  | CreateTodo
  | DeleteTodo
  | UpdateTodo;

const store = createStore<MyAppState, MyAppActions>(myReducer);

I would be interested to hear what you think of this as an idea. In particular, is this an idea that has already been considered and rejected in the past?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions