Skip to content

Instantly share code, notes, and snippets.

@dennis-8
Forked from milankorsos/redux-actions.ts
Created June 6, 2019 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennis-8/b1b928ad5575d9bad2282144a94db127 to your computer and use it in GitHub Desktop.
Save dennis-8/b1b928ad5575d9bad2282144a94db127 to your computer and use it in GitHub Desktop.
Correct TypeScript typing example for Redux Thunk actions
import {Action, ActionCreator, Dispatch} from 'redux';
import {ThunkAction} from 'redux-thunk';
// Redux action
const reduxAction: ActionCreator<Action> = (text: string) => {
return {
type: SET_TEXT,
text
};
};
// Redux-Thunk action
const thunkAction: ActionCreator<ThunkAction<Action, IState, void>> = (
text: string
) => {
return (dispatch: Dispatch<IState>): Action => {
return dispatch({
type: SET_TEXT,
text
});
};
};
// Async Redux-Thunk action
const asyncThinkAction: ActionCreator<
ThunkAction<Promise<Action>, IState, void>
> = () => {
return async (dispatch: Dispatch<IState>): Promise<Action> => {
try {
const text = await Api.call();
return dispatch({
type: SET_TEXT,
text
});
} catch (e) {}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment