immer.d.ts
This file
Path
src/immer.d.ts
165 lines · 5.7 KiB · TypeScript
Last changed by
Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
History of this fileTake it
improve Draft<T> in TypeScriptAlec Larsonbefore adoption512256b
line 1why, one step back1/** Object types that should never be mapped */fix(ts): avoid mapping more built-in typesaleclarsonbefore adoption51f6184
lines 2–13why, one step back2type AtomicObject =3 | Function4 | Map<any, any>5 | WeakMap<any, any>6 | Set<any>7 | WeakSet<any>8 | Promise<any>9 | Date10 | RegExp11 | Boolean12 | Number13 | Stringimprove Draft<T> in TypeScriptAlec Larsonbefore adoption512256b
lines 14–29why, one step back1415/** 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>29fix(ts): Draft<T> where T is a tuple that contains one or many readonly objects/arraysaleclarsonbefore adoption6d9b8d9
lines 30–33why, one step back30export type DraftTuple<T extends any[]> = {31 [P in keyof T]: T[P] extends T[number] ? Draft<T[P]> : never32}33Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57
line 34why, one step back34export 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 back35 ? IsFinite<T> extends true ? DraftTuple<T> : DraftArray<T[number]>Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57
lines 36–38why, one step back36 : T extends ReadonlyArray<any>37 ? DraftArray<T[number]>38 : T extends object ? DraftObject<T> : TMerge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c
line 39why, one step back39Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 40why, one step back40export interface Patch {Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57
lines 41–42why, one step back41 op: "replace" | "remove" | "add"42 path: (string | number)[]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 43–47why, one step back43 value?: any44}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 back48export 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 back64 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 back66 ): SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
line 67why, one step back67`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
lines 68–85why, one step back68 // curried invocations with default initial state69 // 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 back86 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 back93 initialState: S94 ): (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 back99 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 back104 initialState: S105 ): (currentState: S | undefined, ...extraArgs: any[]) => SAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 106why, one step back106`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
lines 107–108why, one step back107 // curried invocations without default initial state108 // 0 additional argumentsMerge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57
lines 109–111why, one step back109 <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 back112 // 1 additional argument of type A113 <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 back122 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 back129 ): (currentState: S, a: A, b: B, c: C) => S130 // 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 back134 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 back139 ): (currentState: S, ...extraArgs: any[]) => S140}141142export const produce: IProduce143export default produce144Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 145–146why, one step back145export const nothing: undefined146Added flow types, implements #34Michel Weststratebefore adoption737e556
lines 147–159why, one step back147/**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): voidMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 160–161why, one step back160161export function applyPatches<S>(state: S, patches: Patch[]): SMerge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
lines 162–163why, one step back162163export function original<T>(value: T): T | voidMerge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 164–165why, one step back164165export function isDraft(value: any): boolean