Skip to content

Instantly share code, notes, and snippets.

@jakejrichards
Last active August 3, 2020 06:08
Show Gist options
  • Save jakejrichards/ba871642c95bd0e5c757a9c97dfd082d to your computer and use it in GitHub Desktop.
Save jakejrichards/ba871642c95bd0e5c757a9c97dfd082d to your computer and use it in GitHub Desktop.
import { ThunkAction } from 'redux-thunk';
import { ApplicationState, ApplicationAction } from './types';
import { loadUsersRequest, loadUsersSuccess, loadUsersError } from './actions';
import * as userService from '../services/userService';
type Effect = ThunkAction<any, ApplicationState, any, ApplicationAction>;
export const loadUsers = (): Effect => (dispatch, getState) => {
dispatch(loadUsersRequest());
// assume userService.loadUsers returns a Promise<User[]>
return userService.loadUsers()
.then(users => dispatch(loadUsersSuccess(users)))
.catch(() => dispatch(loadUsersError()));
};
@clhenrick
Copy link

what is the type Effect doing here? Why is loadUsers not typed? How does loadUsers infer typing? Not following how this example demonstrates typing an async action creator with Redux.

@jakejrichards
Copy link
Author

jakejrichards commented Jun 3, 2019

@clhenrick

what is the type Effect doing here? Why is loadUsers not typed? How does loadUsers infer typing? Not following how this example demonstrates typing an async action creator with Redux.

I've updated the gist to correctly show how loadUsers can infer the type. I accidentally left it out, thanks for pointing it out!

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