Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

148 lines · 5.4 KiB · TypeScript

Last changed by

improve Draft<T> in TypeScriptAlec Larsonbefore adoption512256b

History of this file

Take it

improve Draft<T> in TypeScriptAlec Larsonbefore adoption512256b

lines 1–18why, one step back
1/** Object types that should never be mapped */
2type AtomicObject = Function | Date | RegExp | Boolean | Number | String34/** Use type inference to know when an array is finite */5type IsFinite<T extends any[]> = T extends never[]6 ? true7 : T extends ReadonlyArray<infer U> ? (U[] extends T ? false : true) : true89export type DraftObject<T> = T extends object10 ? T extends AtomicObject ? T : {-readonly [P in keyof T]: Draft<T[P]>}11 : T1213export type DraftArray<T> = Array<14 T extends ReadonlyArray<any>15 ? {[P in keyof T]: Draft<T>}[keyof T]16 : DraftObject<T>17>18

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

line 19why, one step back
19export type Draft<T> = T extends any[]

improve Draft<T> in TypeScriptAlec Larsonbefore adoption512256b

line 20why, one step back
20 ? IsFinite<T> extends true ? T : DraftArray<T[number]>

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

lines 21–23why, one step back
21 : T extends ReadonlyArray<any>
22 ? DraftArray<T[number]>23 : T extends object ? DraftObject<T> : T

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

line 24why, one step back
24

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 25why, one step back
25export interface Patch {

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

lines 26–27why, one step back
26 op: "replace" | "remove" | "add"
27 path: (string | number)[]

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 28–32why, one step back
28 value?: any
29}3031export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void32

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 33–48why, one step back
33export interface IProduce {
34 /**35 * Immer takes a state, and runs a function against it.36 * That function can freely mutate the state, as it will create copies-on-write.37 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.38 *39 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe40 * any time it is called with the current state.41 *42 * @param currentState - the state to start with43 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified44 * @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 undefined45 * @returns The next state: a new state, or the current state if nothing was modified46 */47 <S = any>(48 currentState: S,

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 49–50why, one step back
49 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,
50 listener?: PatchListener

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

line 51why, one step back
51 ): S

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

line 52why, one step back
52

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 53–70why, one step back
53 // curried invocations with default initial state
54 // 0 additional arguments55 <S = any>(56 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,57 initialState: S58 ): (currentState: S | undefined) => S59 // 1 additional argument of type A60 <S = any, A = any>(61 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S,62 initialState: S63 ): (currentState: S | undefined, a: A) => S64 // 2 additional arguments of types A and B65 <S = any, A = any, B = any>(66 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S,67 initialState: S68 ): (currentState: S | undefined, a: A, b: B) => S69 // 3 additional arguments of types A, B and C70 <S = any, A = any, B = any, C = any>(

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

lines 71–77why, one step back
71 recipe: (
72 this: Draft<S>,73 draftState: Draft<S>,74 a: A,75 b: B,76 c: C77 ) => void | S,

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 78–83why, one step back
78 initialState: S
79 ): (currentState: S | undefined, a: A, b: B, c: C) => S80 // any number of additional arguments, but with loss of type safety81 // this may be alleviated if "variadic kinds" makes it into Typescript:82 // https://github.com/Microsoft/TypeScript/issues/545383 <S = any>(

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

lines 84–88why, one step back
84 recipe: (
85 this: Draft<S>,86 draftState: Draft<S>,87 ...extraArgs: any[]88 ) => void | S,

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 89–90why, one step back
89 initialState: S
90 ): (currentState: S | undefined, ...extraArgs: any[]) => S

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 91why, one step back
91

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 92–93why, one step back
92 // curried invocations without default initial state
93 // 0 additional arguments

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

lines 94–96why, one step back
94 <S = any>(recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): (
95 currentState: S96 ) => S

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 97–106why, one step back
97 // 1 additional argument of type A
98 <S = any, A = any>(99 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S100 ): (currentState: S, a: A) => S101 // 2 additional arguments of types A and B102 <S = any, A = any, B = any>(103 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S104 ): (currentState: S, a: A, b: B) => S105 // 3 additional arguments of types A, B and C106 <S = any, A = any, B = any, C = any>(

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

lines 107–113why, one step back
107 recipe: (
108 this: Draft<S>,109 draftState: Draft<S>,110 a: A,111 b: B,112 c: C113 ) => void | S

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 114–118why, one step back
114 ): (currentState: S, a: A, b: B, c: C) => S
115 // any number of additional arguments, but with loss of type safety116 // this may be alleviated if "variadic kinds" makes it into Typescript:117 // https://github.com/Microsoft/TypeScript/issues/5453118 <S = any>(

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

lines 119–123why, one step back
119 recipe: (
120 this: Draft<S>,121 draftState: Draft<S>,122 ...extraArgs: any[]123 ) => void | S

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 124–129why, one step back
124 ): (currentState: S, ...extraArgs: any[]) => S
125}126127export const produce: IProduce128export default produce129

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 130–131why, one step back
130export const nothing: undefined
131

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 132–144why, one step back
132/**
133 * Automatically freezes any state trees generated by immer.134 * This protects against accidental modifications of the state tree outside of an immer function.135 * This comes with a performance impact, so it is recommended to disable this option in production.136 * It is by default enabled.137 */138export function setAutoFreeze(autoFreeze: boolean): void139140/**141 * Manually override whether proxies should be used.142 * By default done by using feature detection143 */144export function setUseProxies(useProxies: boolean): void

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 145–146why, one step back
145
146export function applyPatches<S>(state: S, patches: Patch[]): S

Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2

lines 147–148why, one step back
147
148export function original<T>(value: T): T | void