Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

112 lines · 4.9 KiB · TypeScript

Last changed by

Added typings and typings testsMichel Weststratebefore adoption4eb6bce

History of this file

Take it

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

lines 1–12why, 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-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;12

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

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

Merge branch 'master' into patchesMichel Weststratebefore adoption2ddcda2

line 14why, one step back
14 op: "replace" | "remove" | "add",

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 15–20why, one step back
15 path: (string|number)[]
16 value?: any17}1819export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void20

Merge branch 'master' into patchesMichel Weststratebefore adoption2ddcda2

lines 21–39why, one step back
21export 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,37 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,38 listener?: PatchListener39 ): S

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

line 40why, one step back
40

Merge branch 'master' into patchesMichel Weststratebefore adoption2ddcda2

lines 41–68why, one step back
41 // curried invocations with default initial state
42 // 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[]) => S

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 69why, one step back
69

Merge branch 'master' into patchesMichel Weststratebefore adoption2ddcda2

lines 70–97why, one step back
70 // curried invocations without default initial state
71 // 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 produce97

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 98–110why, one step back
98/**
99 * Automatically freezes any state trees generated by immer.100 * This protects against accidental modifications of the state tree outside of an immer function.101 * This comes with a performance impact, so it is recommended to disable this option in production.102 * It is by default enabled.103 */104export function setAutoFreeze(autoFreeze: boolean): void105106/**107 * Manually override whether proxies should be used.108 * By default done by using feature detection109 */110export function setUseProxies(useProxies: boolean): void

Added typings and typings testsMichel Weststratebefore adoption4eb6bce

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