Skip to content

Instantly share code, notes, and snippets.

@twavv
Last active November 19, 2019 17:27
Show Gist options
  • Save twavv/89b2d27db4a44e6e85be99e1fce67543 to your computer and use it in GitHub Desktop.
Save twavv/89b2d27db4a44e6e85be99e1fce67543 to your computer and use it in GitHub Desktop.
Redux Toolkit TS Example API
import { Reducer } from "redux";
import {
createAction,
createReducer,
PayloadAction,
PayloadActionCreator,
} from "@reduxjs/toolkit";
const fetchUserBlogPosts = createAction<{
userId: number;
categoryId?: number;
fullText?: boolean;
}>("fetch-user-blog-posts");
type TravReducer = Reducer & {
addCase<P, T extends string, M, E>(
actionType: PayloadActionCreator<P, T>,
handler: (state: null, action: PayloadAction<P, T>) => void,
): void;
};
const myReducer: TravReducer = createReducer({ posts: [] }, {}) as any;
myReducer.addCase(fetchUserBlogPosts, (state, action) => {
// type of `action` is inferred correctly here
console.log(action.payload.userId);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment