immer.d.ts
This file
Path
src/immer.d.ts
95 lines · 4.5 KiB · TypeScript
Last changed by
Extend Draft type to support `Array`s and `ReadonlyArray`s.Kenneth Powersbefore adoption70ca1ff
History of this fileTake it
Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 1why, one step back1// Mapped type to remove readonly modifiers from stateExtend Draft type to support `Array`s and `ReadonlyArray`s.Kenneth Powersbefore adoption70ca1ff
lines 2–3why, one step back2// Based on https://github.com/Microsoft/TypeScript/blob/d4dc67aab233f5a8834dff16531baf99b16fea78/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L120-L1293export type DraftObject<T> = {Make mapped Draft type recursive.Kenneth Powersbefore adoptiona9c12a5
line 4why, one step back4 -readonly [P in keyof T]: Draft<T[P]>;Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 5why, one step back5};Extend Draft type to support `Array`s and `ReadonlyArray`s.Kenneth Powersbefore adoption70ca1ff
lines 6–11why, one step back6interface DraftArray<T> extends Array<Draft<T>> { }7export type Draft<T> =8 T extends any[] ? DraftArray<T[number]> :9 T extends ReadonlyArray<any> ? DraftArray<T[number]> :10 T extends object ? DraftObject<T> :11 T;Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 12why, one step back12Added flow types, implements #34Michel Weststratebefore adoption737e556
lines 13–15why, one step back13/**14 * Immer takes a state, and runs a function against it.15 * That function can freely mutate the state, as it will create copies-on-write.Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 16why, one step back16 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.Added flow types, implements #34Michel Weststratebefore adoption737e556
line 17why, one step back17 *Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 18–19why, one step back18 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe19 * any time it is called with the current state.Added flow types, implements #34Michel Weststratebefore adoption737e556
lines 20–21why, one step back20 *21 * @param currentState - the state to start withMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 22–23why, one step back22 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified23 * @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefinedAdded flow types, implements #34Michel Weststratebefore adoption737e556
lines 24–27why, one step back24 * @returns The next state: a new state, or the current state if nothing was modified25 */26export default function<S = any>(27 currentState: S,Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 28why, one step back28 recipe: (this: Draft<S>, draftState: Draft<S>) => void | SAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 29why, one step back29): SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 30–32why, one step back3031// curried invocations with default initial state32// 0 additional argumentsAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 33why, one step back33export default function<S = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 34why, one step back34 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 35–37why, one step back35 initialState: S36): (currentState: S | undefined) => S37// 1 additional argument of type AMerge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6
line 38why, one step back38export default function<S = any, A = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 39why, one step back39 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S,Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 40–42why, one step back40 initialState: S41): (currentState: S | undefined, a: A) => S42// 2 additional arguments of types A and BMerge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6
line 43why, one step back43export default function<S = any, A = any, B = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 44why, one step back44 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S,Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 45–47why, one step back45 initialState: S46): (currentState: S | undefined, a: A, b: B) => S47// 3 additional arguments of types A, B and CMerge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6
line 48why, one step back48export default function<S = any, A = any, B = any, C = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 49why, one step back49 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S,Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 50–55why, one step back50 initialState: S51): (currentState: S | undefined, a: A, b: B, c: C) => S52// any number of additional arguments, but with loss of type safety53// this may be alleviated if "variadic kinds" makes it into Typescript:54// https://github.com/Microsoft/TypeScript/issues/545355export default function<S = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 56why, one step back56 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S,Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 57–58why, one step back57 initialState: S58): (currentState: S | undefined, ...extraArgs: any[]) => SAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 59why, one step back59Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 60–62why, one step back60// curried invocations without default initial state61// 0 additional arguments62export default function<S = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 63why, one step back63 recipe: (this: Draft<S>, draftState: Draft<S>) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 64–66why, one step back64): (currentState: S) => S65// 1 additional argument of type A66export default function<S = any, A = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 67why, one step back67 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 68–70why, one step back68): (currentState: S, a: A) => S69// 2 additional arguments of types A and B70export default function<S = any, A = any, B = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 71why, one step back71 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 72–74why, one step back72): (currentState: S, a: A, b: B) => S73// 3 additional arguments of types A, B and C74export default function<S = any, A = any, B = any, C = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 75why, one step back75 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 76–80why, one step back76): (currentState: S, a: A, b: B, c: C) => S77// any number of additional arguments, but with loss of type safety78// this may be alleviated if "variadic kinds" makes it into Typescript:79// https://github.com/Microsoft/TypeScript/issues/545380export default function<S = any>(Add mapped `Draft` type.Kenneth Powersbefore adoption673db94
line 81why, one step back81 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
line 82why, one step back82): (currentState: S, ...extraArgs: any[]) => SAdded flow types, implements #34Michel Weststratebefore adoption737e556
lines 83–95why, one step back83/**84 * Automatically freezes any state trees generated by immer.85 * This protects against accidental modifications of the state tree outside of an immer function.86 * This comes with a performance impact, so it is recommended to disable this option in production.87 * It is by default enabled.88 */89export function setAutoFreeze(autoFreeze: boolean): void9091/**92 * Manually override whether proxies should be used.93 * By default done by using feature detection94 */95export function setUseProxies(useProxies: boolean): void