redux-toolkit createAction() type definition in typescript

Viewed 1731

I am exploring redux-toolkit library recently but facing difficulty about type declaration of the createAction function as shown below.


createAction returns a PayloadActionCreator which has a generic of <ReturnType<PA>['payload'], T, PA>. However, what does it mean by ReturnType<PA>['payload']?

export declare function createAction<PA extends PrepareAction<any>, T extends string = string>(type: T, prepareAction: PA): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>
1 Answers

ReturnType<PA>['payload'] means the payload property of the object returned by the prepare function - as this is the signature for the prepare notation.

However, this should not concern you. The usual usage is const actionCreator = createAction<PayloadType>('some/action/type').

The alternative notation would be the prepare notation, in the form createAction('some/action/type', (payload: string) => ({ payload, meta: {something: "foo"}}).

In general, please refer to the API documentation that has almost all usage examples in TypeScript. You really should not write more types than are present there. (And yes, that's not a lot of types, that's pretty much the point ;) )

Related