-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Alternate: Add ability for slices to listen to other actions #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alternate: Add ability for slices to listen to other actions #86
Conversation
Deploy preview for redux-starter-kit-docs ready! Built with commit 09abf7b https://deploy-preview-86--redux-starter-kit-docs.netlify.com |
Per my comment in the other thread, this appears to be removing functionality that I want to keep. I don't think this is the direction I want to go. |
@markerikson In that case, I've reverted the changes so The only changes here are to the typing of |
Okay. one other question. @Jessidhia mentioned something in Reactiflux about
Would that allow the action creator itself to be used directly as a key in an object, without a TS2464 error like microsoft/TypeScript#14207 ? |
@markerikson I've tried that and while doing something like export function createAction<P = any, T extends string = string>(
type: T
): PayloadActionCreator<P, T> & T { // <- note the `& T`
function actionCreator(): Action<T>
function actionCreator(payload: P): PayloadAction<P, T>
function actionCreator(payload?: P): Action<T> | PayloadAction<P, T> {
return { type, payload }
}
actionCreator.toString = (): T => `${type}` as T
actionCreator.type = type
return actionCreator as any // <- typecast as any to prevent errors
} allows you to use the action creator as object key without TS errors. Typescript will just treat it like a generic string in that case which breaks type safety. const reducers = {
[increment]: (state,payload) => state + payload
}
// reducers will have type { [x:string]: (number, number) => number } So I think it's better for TS users to explicitly use the action creator |
* Add `type` field to action creators * Update createSlice to handle other actions * Fill out createSlice docs * Formatting * Hacky attempt to fix createAction type tests * Fix example typo * Alternate: Add ability for slices to listen to other actions (#86) Implemented my proposed changes from #83 (comment) @denisw @markerikson your inputs are appreciated *Note: this PR is for the `feature/other-slice-action` branch not `master`* * ~~Removed `getType()` utility~~ * ~~`slice` is no longer attached to `actionsMap` and `reducerMap`~~ * ~~Removed `reducerMap` and directly forward `reducers` to `createReducer`~~ - [x] `createAction()` action creators `type` property returns a `string literal` for better type saftey - [x] Fixed tests - [x] Added tests * Play with type tests a bit
Implemented my proposed changes from #83 (comment)
@denisw @markerikson your inputs are appreciated
Note: this PR is for the
feature/other-slice-action
branch notmaster
RemovedgetType()
utilityslice
is no longer attached toactionsMap
andreducerMap
RemovedreducerMap
and directly forwardreducers
tocreateReducer
createAction()
action creatorstype
property returns astring literal
for better type safteyFixed tests
Added tests