defineSlice

function
types.ts:2204
signature
defineSlice<EMAdd>(): (spec: ReducerSpec<St, EMAdd>) => ReducerSpec<St, EMAdd> & EventMapCarrier<EMAdd>

Declares a reducer spec together with the event map it contributes.

Type parameters

NameTypeDescription
EMAddextends EventMapBase

Returns

(spec: ReducerSpec<St, EMAdd>) => ReducerSpec<St, EMAdd> & EventMapCarrier<EMAdd>

Example

example
type LibEM = { "lib.transfer": { granted: { id: string } } };

const transfers = defineSlice<LibEM>()({
  state: { granted: [] as string[] },
  when: { keys: [["lib.transfer", "granted"]] },
  reducer: (s, e) => (e.type === "granted" ? { granted: [...s.granted, e.payload.id] } : s),
});

const widened = store.withSlice("transfers", transfers);
// widened.getState().transfers.granted is string[], and `lib.transfer` is emittable