-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Type payload-less slice action creators to take no arguments #110
Conversation
Deploy preview for redux-starter-kit-docs ready! Built with commit 3d1772d https://deploy-preview-110--redux-starter-kit-docs.netlify.com |
Previously, it was not possible to assign a slice action creator without payload to `() => any` because the type was inferred as `(payload: void) => any`. Now we use a conditional type for `SliceActionCreator` that special-cases `void`.
Works for me nicely! I could go from export const slice = createSlice<
State,
any,
{
increment: CaseReducer<State, PayloadAction<number>>;
decrement: CaseReducer<State, PayloadAction<number>>;
navigation: CaseReducer<State, NavigationAction<{ counter: string }>>;
}
>({
slice: 'home',
reducers: {
increment: (state, action) => {
state.counter += action.payload;
},
decrement: (state, action) => {
state.counter -= action.payload;
},
navigation: (state, action) => {
state.counter = Number.parseInt(action.payload.routeParams.counter);
}
},
initialState: { counter: 0 }
}); to export const slice = createSlice({
slice: 'home',
reducers: {
increment: (state, action: PayloadAction<number>) => {
state.counter += action.payload;
},
decrement: (state, action: PayloadAction<number>) => {
state.counter -= action.payload;
},
navigation: (state, action: NavigationAction<{ counter: string }>) => {
state.counter = Number.parseInt(action.payload.routeParams.counter);
}
},
initialState: { counter: 0 } as State
}); The only thing that is not working is defining the State type as a Generic like this (but this is only a nice-to-have): export const slice = createSlice<State>({
slice: 'home',
reducers: {
increment: (state, action: PayloadAction<number>) => {
state.counter += action.payload;
},
decrement: (state, action: PayloadAction<number>) => {
state.counter -= action.payload;
},
navigation: (state, action: NavigationAction<{ counter: string }>) => {
state.counter = Number.parseInt(action.payload.routeParams.counter);
}
},
initialState: { counter: 0 }
}); |
Actually I am noticing right now that example B seems to be working with the current version without this patch, too, so I might have never had this specific problem & been the wrong one to test this o_O Sorry! |
Is there an ETA on this getting merged? When can we expect a new version of RSK? |
Woops, forgot this was here - sorry! I've been completely occupied with work on React-Redux v7 the last couple months. I'll try to swing back and examine this in more detail this week. @denisw : is there any chance we can make a no-param action creator actually not try to assign |
@markerikson I guess we could add a function which explicitly creates an action creator taking no parameters. Perhaps there could be separate And alternative approach is to keep a single const increment = createAction('increment')
// increment takes no payload
const multiply = createAction('multiply').withPayload()
// multiply does take a payload In TypeScript, you would then be able to specify the payload type with the const multiply = createAction('multiply').withPayload<number>() |
Superseded by #133. |
Previously, it was not possible to assign a slice action creator without payload to
() => any
because the type was inferred as(payload: void) => any
. Now we use a conditional type forSliceActionCreator
that special-casesvoid
.Fixes #108.