Closed
Description
Hi,
I've been using a utility type like this for a while:
import { CaseReducer, PayloadAction } from '@reduxjs/toolkit'
export type MapReducerPayloads<State, ReducerPayloadMap> = {
[K in keyof ReducerPayloadMap]: CaseReducer<State, PayloadAction<ReducerPayloadMap[K]>>
}
Which allows you to succinctly create a CaseReducers
type like so:
interface RoomState {
id: string | null;
peer?: Peer;
owner?: boolean;
connections: string[];
}
type Reducers = MapReducerPayloads<RoomState, {
createRoom: void;
joinRoom: {
roomId: string;
owner: boolean;
};
}>
const initialState: RoomState = {
id: null,
connections: []
}
export const roomSlice = createSlice<RoomState, Reducers>({
name: 'room',
initialState,
reducers: {
createRoom: (state) => {},
joinRoom: (state, { payload: { roomId, owner } }) => {
state.id = roomId
state.peer = new Peer(roomId)
state.owner = owner
}
}
})
I really like this approach as you get the types flowing through into your payload objects inside your reducers functions.
Just wanted to ask:
A) Is there a similar, built-in way of accomplishing the same? (without defining the reducers outside of the createSlice
fn and letting the types get inferred - I find this less readable too)
B) Is this a pattern the maintainers would be interested in incorporating into the lib? Or is it too subjective/niche/complex etc?
Cheers
Metadata
Metadata
Assignees
Labels
No labels