Last active
November 19, 2019 17:27
-
-
Save twavv/89b2d27db4a44e6e85be99e1fce67543 to your computer and use it in GitHub Desktop.
Redux Toolkit TS Example API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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