immer.ts
This file
Path
src/middleware/immer.ts
87 lines · 2.5 KiB · TypeScript
Last changed by
chore(deps): update dev dependencies (#3560)Daishi Katobefore adoption1f531ba
History of this fileTake it
The walk stopped after 200 commits: lines older than f85898f point at it.
chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 1–8why, one step back1import { produce } from 'immer'2import type { Draft } from 'immer'3import type { StateCreator, StoreMutatorIdentifier } from '../vanilla.ts'45type Immer = <6 T,7 Mps extends [StoreMutatorIdentifier, unknown][] = [],8 Mcs extends [StoreMutatorIdentifier, unknown][] = [],fix(immer): Proper typing for immer middleware in combination with slices (#3371)Manuel Krebberbefore adoption3871d53
line 9why, one step back9 U = T,chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 10why, one step back10>(fix(immer): Proper typing for immer middleware in combination with slices (#3371)Manuel Krebberbefore adoption3871d53
lines 11–12why, one step back11 initializer: StateCreator<T, [...Mps, ['zustand/immer', never]], Mcs, U>,12) => StateCreator<T, Mps, [['zustand/immer', never], ...Mcs], U>chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 13–60why, one step back1314declare module '../vanilla' {15 // eslint-disable-next-line @typescript-eslint/no-unused-vars16 interface StoreMutators<S, A> {17 ['zustand/immer']: WithImmer<S>18 }19}2021type Write<T, U> = Omit<T, keyof U> & U22type SkipTwo<T> = T extends { length: 0 }23 ? []24 : T extends { length: 1 }25 ? []26 : T extends { length: 0 | 1 }27 ? []28 : T extends [unknown, unknown, ...infer A]29 ? A30 : T extends [unknown, unknown?, ...infer A]31 ? A32 : T extends [unknown?, unknown?, ...infer A]33 ? A34 : never3536type SetStateType<T extends unknown[]> = Exclude<T[0], (...args: any[]) => any>3738type WithImmer<S> = Write<S, StoreImmer<S>>3940type StoreImmer<S> = S extends {41 setState: infer SetState42}43 ? SetState extends {44 (...args: infer A1): infer Sr145 (...args: infer A2): infer Sr246 }47 ? {48 // Ideally, we would want to infer the `nextStateOrUpdater` `T` type from the49 // `A1` type, but this is infeasible since it is an intersection with50 // a partial type.51 setState(52 nextStateOrUpdater:53 | SetStateType<A2>54 | Partial<SetStateType<A2>>55 | ((state: Draft<SetStateType<A2>>) => void),56 shouldReplace?: false,57 ...args: SkipTwo<A1>58 ): Sr159 setState(60 nextStateOrUpdater:chore(deps): update dev dependencies (#3560)Daishi Katobefore adoption1f531ba
line 61why, one step back61 SetStateType<A2> | ((state: Draft<SetStateType<A2>>) => void),chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 62–87why, one step back62 shouldReplace: true,63 ...args: SkipTwo<A2>64 ): Sr265 }66 : never67 : never6869type ImmerImpl = <T>(70 storeInitializer: StateCreator<T, [], []>,71) => StateCreator<T, [], []>7273const immerImpl: ImmerImpl = (initializer) => (set, get, store) => {74 type T = ReturnType<typeof initializer>7576 store.setState = (updater, replace, ...args) => {77 const nextState = (78 typeof updater === 'function' ? produce(updater as any) : updater79 ) as ((s: T) => T) | T | Partial<T>8081 return set(nextState, replace as any, ...args)82 }8384 return initializer(store.setState, get, store)85}8687export const immer = immerImpl as unknown as Immer