Skip to content
Myr

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 file

Take it

Add mapped `Draft` type.Kenneth Powersbefore adoption673db94

line 1why, one step back
1// Mapped type to remove readonly modifiers from state

Extend Draft type to support `Array`s and `ReadonlyArray`s.Kenneth Powersbefore adoption70ca1ff

lines 2–3why, one step back
2// Based on https://github.com/Microsoft/TypeScript/blob/d4dc67aab233f5a8834dff16531baf99b16fea78/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L120-L129
3export type DraftObject<T> = {

Make mapped Draft type recursive.Kenneth Powersbefore adoptiona9c12a5

line 4why, one step back
4 -readonly [P in keyof T]: Draft<T[P]>;

Add mapped `Draft` type.Kenneth Powersbefore adoption673db94

line 5why, one step back
5};

Extend Draft type to support `Array`s and `ReadonlyArray`s.Kenneth Powersbefore adoption70ca1ff

lines 6–11why, one step back
6interface 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 back
12

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 13–15why, one step back
13/**
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 back
16 * 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 back
17 *

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 18–19why, one step back
18 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe
19 * any time it is called with the current state.

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 20–21why, one step back
20 *
21 * @param currentState - the state to start with

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 22–23why, one step back
22 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified
23 * @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 undefined

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 24–27why, one step back
24 * @returns The next state: a new state, or the current state if nothing was modified
25 */26export default function<S = any>(27 currentState: S,

Add mapped `Draft` type.Kenneth Powersbefore adoption673db94

line 28why, one step back
28 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 29why, one step back
29): S

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 30–32why, one step back
30
31// curried invocations with default initial state32// 0 additional arguments

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 33why, one step back
33export default function<S = any>(

Add mapped `Draft` type.Kenneth Powersbefore adoption673db94

line 34why, one step back
34 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 back
35 initialState: S
36): (currentState: S | undefined) => S37// 1 additional argument of type A

Merge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6

line 38why, one step back
38export default function<S = any, A = any>(

Add mapped `Draft` type.Kenneth Powersbefore adoption673db94

line 39why, one step back
39 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 back
40 initialState: S
41): (currentState: S | undefined, a: A) => S42// 2 additional arguments of types A and B

Merge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6

line 43why, one step back
43export default function<S = any, A = any, B = any>(

Add mapped `Draft` type.Kenneth Powersbefore adoption673db94

line 44why, one step back
44 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 back
45 initialState: S
46): (currentState: S | undefined, a: A, b: B) => S47// 3 additional arguments of types A, B and C

Merge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6

line 48why, one step back
48export default function<S = any, A = any, B = any, C = any>(

Add mapped `Draft` type.Kenneth Powersbefore adoption673db94

line 49why, one step back
49 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 back
50 initialState: S
51): (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 back
56 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 back
57 initialState: S
58): (currentState: S | undefined, ...extraArgs: any[]) => S

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 59why, one step back
59

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 60–62why, one step back
60// curried invocations without default initial state
61// 0 additional arguments62export default function<S = any>(

Add mapped `Draft` type.Kenneth Powersbefore adoption673db94

line 63why, one step back
63 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 64–66why, one step back
64): (currentState: S) => S
65// 1 additional argument of type A66export default function<S = any, A = any>(

Add mapped `Draft` type.Kenneth Powersbefore adoption673db94

line 67why, one step back
67 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 68–70why, one step back
68): (currentState: S, a: A) => S
69// 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 back
71 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 72–74why, one step back
72): (currentState: S, a: A, b: B) => S
73// 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 back
75 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 76–80why, one step back
76): (currentState: S, a: A, b: B, c: C) => S
77// 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 back
81 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

line 82why, one step back
82): (currentState: S, ...extraArgs: any[]) => S

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 83–95why, one step back
83/**
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