Skip to content

Instantly share code, notes, and snippets.

@milankorsos
Last active November 7, 2024 10:05
Show Gist options
  • Save milankorsos/ffb9d32755db0304545f92b11f0e4beb to your computer and use it in GitHub Desktop.
Save milankorsos/ffb9d32755db0304545f92b11f0e4beb 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) {}
};
};
@shehi
Copy link

shehi commented Mar 10, 2021

I still can't locate IStoreState

@Goran7777
Copy link

Goran7777 commented Aug 9, 2021

Maybe this is right place to use "any" when i must make type for thunk action in my props.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment