Skip to content
Myr

refactor(ts): tweak IProduce type

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Alec Larson · committed by Alec Larson · parent 3f40298 · Browse files · 1 file+25 added−26 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+25 added−26 removed

  • other:+25 added−26 removed

1 file+25 added−26 removed

modified:src/immer.d.ts+25 added-26 removed
48 unmodified lines
49 : Return extends Promise<infer Result>50 ? Promise<Result extends void ? Base : FromNothing<Result>>51 : FromNothing<Return>5253/**54 * The `produce` function takes a value and a "recipe function" (whose55 * return value often depends on the base state). The recipe function is56 * free to mutate its first argument however it wants. All mutations are57 * only ever applied to a __copy__ of the base state.58 *59 * Pass only a function to create a "curried producer" which relieves you60 * from passing the recipe function every time.61 *62 * Only plain objects and arrays are made mutable. All other objects are63 * considered uncopyable.64 *65 * Note: This function is __bound__ to its `Immer` instance.66 *67 * @param {any} base - the initial state68 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified69 * @param {Function} patchListener - optional function that will be called with all the patches produced here70 * @returns {any} a new state, or the initial state if nothing was modified71 */72export interface IProduce {54 /**55 * The `produce` function takes a value and a "recipe function" (whose56 * return value often depends on the base state). The recipe function is57 * free to mutate its first argument however it wants. All mutations are58 * only ever applied to a __copy__ of the base state.59 *60 * Pass only a function to create a "curried producer" which relieves you61 * from passing the recipe function every time.62 *63 * Only plain objects and arrays are made mutable. All other objects are64 * considered uncopyable.65 *66 * Note: This function is __bound__ to its `Immer` instance.67 *68 * @param {any} base - the initial state69 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified70 * @param {Function} patchListener - optional function that will be called with all the patches produced here71 * @returns {any} a new state, or the initial state if nothing was modified72 */7373 /** Curried producer */74 <75 Recipe extends (...args: any[]) => any,76 Params extends any[] = Parameters<Recipe>,77 T = Params[0]78 >(79 recipe: Recipe81 ): <S extends Immutable<T>>(82 state: S,80 ): <Base extends Immutable<T>>(81 base: Base,82 ...rest: Tail<Params>84 ) => Produced<S, ReturnType<Recipe>>83 ) => Produced<Base, ReturnType<Recipe>>84 // ^ by making the returned type generic, the actual type of the passed in object is preferred85 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe86 // Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct!87
4 unmodified lines
92 T = Params[0]93 >(94 recipe: Recipe,95 initialState: Immutable<T>97 ): <S extends Immutable<T>>(98 state?: S,96 ): <Base extends Immutable<T>>(97 base?: Base,98 ...rest: Tail<Params>100 ) => Produced<S, ReturnType<Recipe>>99 ) => Produced<Base, ReturnType<Recipe>>100101 /** Normal producer */102 <Base, D = Draft<Base>, Return = void>(103 base: Base,
145 unmodified lines