omichelsen / redux-promise-middleware-actions

Redux action creator for making async actions compatible with redux-promise-middleware
MIT License
32 stars 3 forks source link

Typescript dispatch return type #21

Open batusai513 opened 4 years ago

batusai513 commented 4 years ago

Hi, Is there an example on how to use the library with Typescript, How do I transform the return of the createAsyncAction after dispatching it, for example, I have an createGroup action and after dispatching it, the return type is this:

var createGroup: (data: GroupForm) => {
    type: "CREATE_GROUP";
    payload: Promise<NormalizedGroup>;
    meta: number;
}

where I'm expecting this:

var createGroup: (data: GroupForm) => Promise<{
    value: NormalizedGroup;
    action: Action<SomeAction>;
}>
omichelsen commented 4 years ago

It is returning the first types because it is still a plain action for redux. The redux-promise-middleware will see that the payload is a promise and output three new actions: pending and error/success. We can't output the latter type you write since it's not a simple POJO redux action so redux wouldn't understand it.

batusai513 commented 4 years ago

@omichelsen I understand, but I think after it has been processed by the middleware there should be way to get the correct types, in case one want to handle the promise after it has been dispatched, maybe it's related to redux-promise-middleware itself?.


dispatch(createGroup())
/*
return type should be this
Promise<{
    value: NormalizedGroup;
    action: Action<SomeAction>;
}>
*/