Add mapped `Draft` type.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Powers · committed by Kenneth Powers · parent 2e2179b · Browse files · 1 file·+16 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+16 added−11 removed
- other:+16 added−11 removed
Provenance
- Commit
673db94- Tree
0812e28- Author
Kenneth Powers <ken@kenpowers.net>- Committer
Kenneth Powers <ken@kenpowers.net>- Parent
2e2179b
1 file changed+16 added−11 removed
modified:src/immer.d.ts+16 added-11 removed
1234567897 unmodified lines1718192016212223242526222728293031273233343536323738394041374243444546474844495051525354555156575859556061626359646566676368697071727369747576777810 unmodified lines// Mapped type to remove readonly modifiers from stateexport type Draft<T> = { -readonly [P in keyof T]: T[P];};
/** * Immer takes a state, and runs a function against it. * That function can freely mutate the state, as it will create copies-on-write. * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.7 unmodified lines * @returns The next state: a new state, or the current state if nothing was modified */export default function<S = any>( currentState: S, recipe: (this: S, draftState: S) => void | S recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): S
// curried invocations with default initial state// 0 additional argumentsexport default function<S = any>( recipe: (this: S, draftState: S) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>) => void | S, initialState: S): (currentState: S | undefined) => S// 1 additional argument of type Aexport default function<S = any, A = any>( recipe: (this: S, draftState: S, a: A) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S, initialState: S): (currentState: S | undefined, a: A) => S// 2 additional arguments of types A and Bexport default function<S = any, A = any, B = any>( recipe: (this: S, draftState: S, a: A, b: B) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S, initialState: S): (currentState: S | undefined, a: A, b: B) => S// 3 additional arguments of types A, B and Cexport default function<S = any, A = any, B = any, C = any>( recipe: (this: S, draftState: S, a: A, b: B, c: C) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S, initialState: S): (currentState: S | undefined, a: A, b: B, c: C) => S// any number of additional arguments, but with loss of type safety// this may be alleviated if "variadic kinds" makes it into Typescript:// https://github.com/Microsoft/TypeScript/issues/5453export default function<S = any>( recipe: (this: S, draftState: S, ...extraArgs: any[]) => void | S, recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S, initialState: S): (currentState: S | undefined, ...extraArgs: any[]) => S
// curried invocations without default initial state// 0 additional argumentsexport default function<S = any>( recipe: (this: S, draftState: S) => void | S recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): (currentState: S) => S// 1 additional argument of type Aexport default function<S = any, A = any>( recipe: (this: S, draftState: S, a: A) => void | S recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S): (currentState: S, a: A) => S// 2 additional arguments of types A and Bexport default function<S = any, A = any, B = any>( recipe: (this: S, draftState: S, a: A, b: B) => void | S recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S): (currentState: S, a: A, b: B) => S// 3 additional arguments of types A, B and Cexport default function<S = any, A = any, B = any, C = any>( recipe: (this: S, draftState: S, a: A, b: B, c: C) => void | S recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S): (currentState: S, a: A, b: B, c: C) => S// any number of additional arguments, but with loss of type safety// this may be alleviated if "variadic kinds" makes it into Typescript:// https://github.com/Microsoft/TypeScript/issues/5453export default function<S = any>( recipe: (this: S, draftState: S, ...extraArgs: any[]) => void | S recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S): (currentState: S, ...extraArgs: any[]) => S/** * Automatically freezes any state trees generated by immer. * This protects against accidental modifications of the state tree outside of an immer function.10 unmodified lines