Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

101 lines · 4.6 KiB · TypeScript

Last changed by

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

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

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

lines 13–30why, one step back
13export interface IProduce {
14 /**15 * Immer takes a state, and runs a function against it.16 * That function can freely mutate the state, as it will create copies-on-write.17 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.18 *19 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe20 * any time it is called with the current state.21 *22 * @param currentState - the state to start with23 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified24 * @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 undefined25 * @returns The next state: a new state, or the current state if nothing was modified26 */27 <S = any>(28 currentState: S,29 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S30 ): S

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

line 31why, one step back
31

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

lines 32–59why, one step back
32 // curried invocations with default initial state
33 // 0 additional arguments34 <S = any>(35 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,36 initialState: S37 ): (currentState: S | undefined) => S38 // 1 additional argument of type A39 <S = any, A = any>(40 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S,41 initialState: S42 ): (currentState: S | undefined, a: A) => S43 // 2 additional arguments of types A and B44 <S = any, A = any, B = any>(45 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S,46 initialState: S47 ): (currentState: S | undefined, a: A, b: B) => S48 // 3 additional arguments of types A, B and C49 <S = any, A = any, B = any, C = any>(50 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S,51 initialState: S52 ): (currentState: S | undefined, a: A, b: B, c: C) => S53 // any number of additional arguments, but with loss of type safety54 // this may be alleviated if "variadic kinds" makes it into Typescript:55 // https://github.com/Microsoft/TypeScript/issues/545356 <S = any>(57 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S,58 initialState: S59 ): (currentState: S | undefined, ...extraArgs: any[]) => S

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 60why, one step back
60

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

lines 61–88why, one step back
61 // curried invocations without default initial state
62 // 0 additional arguments63 <S = any>(64 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S65 ): (currentState: S) => S66 // 1 additional argument of type A67 <S = any, A = any>(68 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S69 ): (currentState: S, a: A) => S70 // 2 additional arguments of types A and B71 <S = any, A = any, B = any>(72 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S73 ): (currentState: S, a: A, b: B) => S74 // 3 additional arguments of types A, B and C75 <S = any, A = any, B = any, C = any>(76 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S77 ): (currentState: S, a: A, b: B, c: C) => S78 // any number of additional arguments, but with loss of type safety79 // this may be alleviated if "variadic kinds" makes it into Typescript:80 // https://github.com/Microsoft/TypeScript/issues/545381 <S = any>(82 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S83 ): (currentState: S, ...extraArgs: any[]) => S84}8586export const produce: IProduce87export default produce88

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 89–101why, one step back
89/**
90 * Automatically freezes any state trees generated by immer.91 * This protects against accidental modifications of the state tree outside of an immer function.92 * This comes with a performance impact, so it is recommended to disable this option in production.93 * It is by default enabled.94 */95export function setAutoFreeze(autoFreeze: boolean): void9697/**98 * Manually override whether proxies should be used.99 * By default done by using feature detection100 */101export function setUseProxies(useProxies: boolean): void