Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

134 lines · 5.2 KiB · TypeScript

Last changed by

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

History of this file

Take it

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

lines 1–2why, one step back
1// Mapped type to remove readonly modifiers from state
2// Based on https://github.com/Microsoft/TypeScript/blob/d4dc67aab233f5a8834dff16531baf99b16fea78/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L120-L129

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

lines 3–9why, one step back
3export type DraftObject<T> = {-readonly [P in keyof T]: Draft<T[P]>}
4export interface DraftArray<T> extends Array<Draft<T>> {}5export type Draft<T> = T extends any[]6 ? DraftArray<T[number]>7 : T extends ReadonlyArray<any>8 ? DraftArray<T[number]>9 : T extends object ? DraftObject<T> : T

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

line 10why, one step back
10

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 14–18why, one step back
14 value?: any
15}1617export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void18

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

lines 19–34why, one step back
19export interface IProduce {
20 /**21 * Immer takes a state, and runs a function against it.22 * That function can freely mutate the state, as it will create copies-on-write.23 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.24 *25 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe26 * any time it is called with the current state.27 *28 * @param currentState - the state to start with29 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified30 * @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 undefined31 * @returns The next state: a new state, or the current state if nothing was modified32 */33 <S = any>(34 currentState: S,

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

line 37why, one step back
37 ): S

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

line 38why, one step back
38

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

lines 39–56why, one step back
39 // curried invocations with default initial state
40 // 0 additional arguments41 <S = any>(42 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,43 initialState: S44 ): (currentState: S | undefined) => S45 // 1 additional argument of type A46 <S = any, A = any>(47 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S,48 initialState: S49 ): (currentState: S | undefined, a: A) => S50 // 2 additional arguments of types A and B51 <S = any, A = any, B = any>(52 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S,53 initialState: S54 ): (currentState: S | undefined, a: A, b: B) => S55 // 3 additional arguments of types A, B and C56 <S = any, A = any, B = any, C = any>(

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

lines 57–63why, one step back
57 recipe: (
58 this: Draft<S>,59 draftState: Draft<S>,60 a: A,61 b: B,62 c: C63 ) => void | S,

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

lines 64–69why, one step back
64 initialState: S
65 ): (currentState: S | undefined, a: A, b: B, c: C) => S66 // any number of additional arguments, but with loss of type safety67 // this may be alleviated if "variadic kinds" makes it into Typescript:68 // https://github.com/Microsoft/TypeScript/issues/545369 <S = any>(

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

lines 70–74why, one step back
70 recipe: (
71 this: Draft<S>,72 draftState: Draft<S>,73 ...extraArgs: any[]74 ) => void | S,

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

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

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 77why, one step back
77

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

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

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

lines 80–82why, one step back
80 <S = any>(recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): (
81 currentState: S82 ) => S

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

lines 83–92why, one step back
83 // 1 additional argument of type A
84 <S = any, A = any>(85 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S86 ): (currentState: S, a: A) => S87 // 2 additional arguments of types A and B88 <S = any, A = any, B = any>(89 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S90 ): (currentState: S, a: A, b: B) => S91 // 3 additional arguments of types A, B and C92 <S = any, A = any, B = any, C = any>(

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

lines 93–99why, one step back
93 recipe: (
94 this: Draft<S>,95 draftState: Draft<S>,96 a: A,97 b: B,98 c: C99 ) => void | S

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

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

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

lines 105–109why, one step back
105 recipe: (
106 this: Draft<S>,107 draftState: Draft<S>,108 ...extraArgs: any[]109 ) => void | S

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

lines 110–115why, one step back
110 ): (currentState: S, ...extraArgs: any[]) => S
111}112113export const produce: IProduce114export default produce115

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 116–117why, one step back
116export const nothing: undefined
117

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 118–130why, one step back
118/**
119 * Automatically freezes any state trees generated by immer.120 * This protects against accidental modifications of the state tree outside of an immer function.121 * This comes with a performance impact, so it is recommended to disable this option in production.122 * It is by default enabled.123 */124export function setAutoFreeze(autoFreeze: boolean): void125126/**127 * Manually override whether proxies should be used.128 * By default done by using feature detection129 */130export function setUseProxies(useProxies: boolean): void

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

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