Action type doesn't support Promise as return type
treeforever opened this issue · comments
I am using React + Typescript + redux-zero. But I experienced some difficulties when trying to make async calls in actions. I think the error is becauseAction
needs to return Partial<State>
and it doesn't allow Promise
:
export type Action<S> = (state: S, ...args: any[]) => Partial<S>;
(https://github.com/redux-zero/redux-zero/blob/master/src/types/index.ts#L3)
In the following code, I am returning a Promise<State>
:
const actions = (store: Store<GlobalState>) => ({
asyncGetUsers: (state: GlobalState) => {
return client
.get("/users")
.then(payload => ({ users: payload }))
.catch(error => ({ error }));
}
});
That results in an error that says:
Type '(state: GlobalState) => Promise<{ users: User[] }>' is not assignable to type 'Action<GlobalState>'.
Type 'Promise<{ users: User[] }>' has no properties in common with type 'Partial<GlobalState>'.
Let me know if my understanding is correct. If it indeed is because of Action
doesn't support Promise
, what do you suggest to do instead? Thank you.
Hey @treeforever
Thank you for letting me know about this, and sorry about the delay.
Would you be willing to open a PR fixing this?