Currently, I am utilizing redux-promise-middleware alongside redux-thunk to effectively chain my promises:
import { Dispatch } from 'redux';
class Actions {
private static _dispatcher: Dispatch<any>;
public static get dispatcher(): Dispatch<any> {
return Actions._dispatcher;
}
public static test() {
this.dispatcher({
type: 'MY_ACTION',
payload: new Promise(resolve => resolve('hi'));
}).then(result => {
console.log(result); // It's working fine
});
}
}
The code snippet above is functional but triggers a warning message during compilation:
TS2339: Property 'then' does not exist on type '{ type: string; payload: Promise<{}>; }'
It appears that I must include Promise<...>
somewhere as a type so TypeScript recognizes that then
is indeed a property of the object returned by dispatcher()
. Despite my efforts, I have yet to resolve this error.
https://github.com/gaearon/redux-thunk/issues/103
import { Dispatch } from 'redux';
import { ThunkAction } from 'redux-thunk';
import { getStore, IState } from './my_store';
let store = getStore();
// Define myThunkAction function with type ThunkAction<R, S, E>
let myThunkAction: ThunkAction<Promise<string>, IState, null> =
(dispatch: Dispatch<IState>, getState: () => IState) => {
return new Promise<string>((resolve, reject) => {
// Perform asynchronous operations using getState() and dispatch(), then...
resolve('done!');
});
}
store.dispatch(myThunkAction)
.then(() => {
// Execute actions after the thunk has completed...
});
This seems relevant, but where should I specify the action type, for example, MY_ACTION
?