immer.d.ts
This file
Path
src/immer.d.ts
116 lines · 4.9 KiB · TypeScript
Last changed by
Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
History of this fileTake it
Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c
lines 1–12why, one step back1// Mapped type to remove readonly modifiers from state2// Based on https://github.com/Microsoft/TypeScript/blob/d4dc67aab233f5a8834dff16531baf99b16fea78/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L120-L1293export type DraftObject<T> = {4 -readonly [P in keyof T]: Draft<T[P]>;5};6export interface 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;12Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 13–20why, one step back13export interface Patch {14 op: "replace" | "remove" | "add",15 path: (string|number)[]16 value?: any17}1819export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void20`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
lines 21–36why, one step back21export interface IProduce {22 /**23 * Immer takes a state, and runs a function against it.24 * That function can freely mutate the state, as it will create copies-on-write.25 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.26 *27 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe28 * any time it is called with the current state.29 *30 * @param currentState - the state to start with31 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified32 * @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 undefined33 * @returns The next state: a new state, or the current state if nothing was modified34 */35 <S = any>(36 currentState: S,Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 37–38why, one step back37 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,38 listener?: PatchListener`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
line 39why, one step back39 ): SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
line 40why, one step back40`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
lines 41–68why, one step back41 // curried invocations with default initial state42 // 0 additional arguments43 <S = any>(44 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,45 initialState: S46 ): (currentState: S | undefined) => S47 // 1 additional argument of type A48 <S = any, A = any>(49 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S,50 initialState: S51 ): (currentState: S | undefined, a: A) => S52 // 2 additional arguments of types A and B53 <S = any, A = any, B = any>(54 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S,55 initialState: S56 ): (currentState: S | undefined, a: A, b: B) => S57 // 3 additional arguments of types A, B and C58 <S = any, A = any, B = any, C = any>(59 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S,60 initialState: S61 ): (currentState: S | undefined, a: A, b: B, c: C) => S62 // any number of additional arguments, but with loss of type safety63 // this may be alleviated if "variadic kinds" makes it into Typescript:64 // https://github.com/Microsoft/TypeScript/issues/545365 <S = any>(66 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S,67 initialState: S68 ): (currentState: S | undefined, ...extraArgs: any[]) => SAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 69why, one step back69`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
lines 70–97why, one step back70 // curried invocations without default initial state71 // 0 additional arguments72 <S = any>(73 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S74 ): (currentState: S) => S75 // 1 additional argument of type A76 <S = any, A = any>(77 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S78 ): (currentState: S, 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 | S82 ): (currentState: S, a: A, b: B) => S83 // 3 additional arguments of types A, B and C84 <S = any, A = any, B = any, C = any>(85 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S86 ): (currentState: S, a: A, b: B, c: C) => S87 // any number of additional arguments, but with loss of type safety88 // this may be alleviated if "variadic kinds" makes it into Typescript:89 // https://github.com/Microsoft/TypeScript/issues/545390 <S = any>(91 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S92 ): (currentState: S, ...extraArgs: any[]) => S93}9495export const produce: IProduce96export default produce97Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 98–99why, one step back98export const nothing: undefined99Added flow types, implements #34Michel Weststratebefore adoption737e556
lines 100–112why, one step back100/**101 * Automatically freezes any state trees generated by immer.102 * This protects against accidental modifications of the state tree outside of an immer function.103 * This comes with a performance impact, so it is recommended to disable this option in production.104 * It is by default enabled.105 */106export function setAutoFreeze(autoFreeze: boolean): void107108/**109 * Manually override whether proxies should be used.110 * By default done by using feature detection111 */112export function setUseProxies(useProxies: boolean): voidMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 113–114why, one step back113114export function applyPatches<S>(state: S, patches: Patch[]): SMerge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
lines 115–116why, one step back115116export function original<T>(value: T): T | void