get action types from the createslice command of the redux toolkit

createSlice stores the action creators as a keyed object in the property actions. What we are looking for is the union of the return types of all action creators. We will use a mapped type to get this.

type SliceActions<T> = {
    [K in keyof T]: T[K] extends (...args: any[]) => infer A ? A : never;
}[keyof T]

type ActionTypes = SliceActions<typeof slice.actions>

I used the counter example from the doc in this Playground Link and the value of ActionTypes evaluated to:

type ActionTypes = {
    payload: number;
    type: string;
} | {
    payload: undefined;
    type: string;
}

Note that redux toolkit declares its action types as just string rather than specific string literals.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top