Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

165 lines · 5.7 KiB · TypeScript

Last changed by

feat: export `isDraft` function (#220)Alec Larsonbefore adoption172d76a

History of this file

Take it

improve Draft<T> in TypeScriptAlec Larsonbefore adoption512256b

line 1why, one step back
1/** Object types that should never be mapped */

fix(ts): avoid mapping more built-in typesaleclarsonbefore adoption51f6184

lines 2–13why, one step back
2type AtomicObject =
3 | Function4 | Map<any, any>5 | WeakMap<any, any>6 | Set<any>7 | WeakSet<any>8 | Promise<any>9 | Date10 | RegExp11 | Boolean12 | Number13 | String

improve Draft<T> in TypeScriptAlec Larsonbefore adoption512256b

lines 14–29why, one step back
14
15/** Use type inference to know when an array is finite */16type IsFinite<T extends any[]> = T extends never[]17 ? true18 : T extends ReadonlyArray<infer U> ? (U[] extends T ? false : true) : true1920export type DraftObject<T> = T extends object21 ? T extends AtomicObject ? T : {-readonly [P in keyof T]: Draft<T[P]>}22 : T2324export type DraftArray<T> = Array<25 T extends ReadonlyArray<any>26 ? {[P in keyof T]: Draft<T>}[keyof T]27 : DraftObject<T>28>29

fix(ts): Draft<T> where T is a tuple that contains one or many readonly objects/arraysaleclarsonbefore adoption6d9b8d9

lines 30–33why, one step back
30export type DraftTuple<T extends any[]> = {
31 [P in keyof T]: T[P] extends T[number] ? Draft<T[P]> : never32}33

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

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

fix(ts): Draft<T> where T is a tuple that contains one or many readonly objects/arraysaleclarsonbefore adoption6d9b8d9

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

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

lines 36–38why, one step back
36 : T extends ReadonlyArray<any>
37 ? DraftArray<T[number]>38 : T extends object ? DraftObject<T> : T

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

line 39why, one step back
39

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 43–47why, one step back
43 value?: any
44}4546export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void47

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

lines 48–63why, one step back
48export interface IProduce {
49 /**50 * Immer takes a state, and runs a function against it.51 * That function can freely mutate the state, as it will create copies-on-write.52 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.53 *54 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe55 * any time it is called with the current state.56 *57 * @param currentState - the state to start with58 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified59 * @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 undefined60 * @returns The next state: a new state, or the current state if nothing was modified61 */62 <S = any>(63 currentState: S,

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

line 66why, one step back
66 ): S

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

line 67why, one step back
67

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

lines 68–85why, one step back
68 // curried invocations with default initial state
69 // 0 additional arguments70 <S = any>(71 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,72 initialState: S73 ): (currentState: S | undefined) => S74 // 1 additional argument of type A75 <S = any, A = any>(76 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S,77 initialState: S78 ): (currentState: S | undefined, a: A) => S79 // 2 additional arguments of types A and B80 <S = any, A = any, B = any>(81 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S,82 initialState: S83 ): (currentState: S | undefined, a: A, b: B) => S84 // 3 additional arguments of types A, B and C85 <S = any, A = any, B = any, C = any>(

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

lines 86–92why, one step back
86 recipe: (
87 this: Draft<S>,88 draftState: Draft<S>,89 a: A,90 b: B,91 c: C92 ) => void | S,

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

lines 93–98why, one step back
93 initialState: S
94 ): (currentState: S | undefined, a: A, b: B, c: C) => S95 // any number of additional arguments, but with loss of type safety96 // this may be alleviated if "variadic kinds" makes it into Typescript:97 // https://github.com/Microsoft/TypeScript/issues/545398 <S = any>(

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

lines 99–103why, one step back
99 recipe: (
100 this: Draft<S>,101 draftState: Draft<S>,102 ...extraArgs: any[]103 ) => void | S,

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

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

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 106why, one step back
106

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

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

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

lines 109–111why, one step back
109 <S = any>(recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): (
110 currentState: S111 ) => S

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

lines 112–121why, one step back
112 // 1 additional argument of type A
113 <S = any, A = any>(114 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S115 ): (currentState: S, a: A) => S116 // 2 additional arguments of types A and B117 <S = any, A = any, B = any>(118 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S119 ): (currentState: S, a: A, b: B) => S120 // 3 additional arguments of types A, B and C121 <S = any, A = any, B = any, C = any>(

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

lines 122–128why, one step back
122 recipe: (
123 this: Draft<S>,124 draftState: Draft<S>,125 a: A,126 b: B,127 c: C128 ) => void | S

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

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

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

lines 134–138why, one step back
134 recipe: (
135 this: Draft<S>,136 draftState: Draft<S>,137 ...extraArgs: any[]138 ) => void | S

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

lines 139–144why, one step back
139 ): (currentState: S, ...extraArgs: any[]) => S
140}141142export const produce: IProduce143export default produce144

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 145–146why, one step back
145export const nothing: undefined
146

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 147–159why, one step back
147/**
148 * Automatically freezes any state trees generated by immer.149 * This protects against accidental modifications of the state tree outside of an immer function.150 * This comes with a performance impact, so it is recommended to disable this option in production.151 * It is by default enabled.152 */153export function setAutoFreeze(autoFreeze: boolean): void154155/**156 * Manually override whether proxies should be used.157 * By default done by using feature detection158 */159export function setUseProxies(useProxies: boolean): void

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

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

feat: export `isDraft` function (#220)Alec Larsonbefore adoption172d76a

lines 164–165why, one step back
164
165export function isDraft(value: any): boolean