Skip to content

Commit 03ec620

Browse files
committed
fix(ts): return type of produce()
To avoid certain edge cases, we must use the expected draft type (instead of the base type) when determining the return type.
1 parent a52614a commit 03ec620

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

src/immer.d.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ type IsVoidLike<T> =
5656
type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T
5757

5858
/** The inferred return type of `produce` */
59-
type Produced<Base, Return> = 1 extends HasVoidLike<Return>
59+
type Produced<T, Return> = 1 extends HasVoidLike<Return>
6060
? 1 extends IsVoidLike<Return>
61-
? Immutable<Base>
62-
: Immutable<Base> | FromNothing<Exclude<Return, void>>
61+
? Immutable<T>
62+
: Immutable<T> | FromNothing<Exclude<Return, void>>
6363
: FromNothing<Return>
6464

6565
type ImmutableTuple<T extends ReadonlyArray<any>> = {
@@ -97,32 +97,22 @@ export interface IProduce {
9797
* @param {Function} patchListener - optional function that will be called with all the patches produced here
9898
* @returns {any} a new state, or the initial state if nothing was modified
9999
*/
100-
<Base, Proxy = Draft<Base>, Return = void>(
101-
base: Base,
102-
recipe: (this: Proxy, draft: Proxy) => Return,
100+
<T = any, D = Draft<T>, Return = void>(
101+
base: T,
102+
recipe: (this: D, draft: D) => Return,
103103
listener?: PatchListener
104-
): Produced<Base, Return>
104+
): Produced<D, Return>
105105

106106
/** Curried producer with a default value */
107-
<Default = any, Base = Default, Rest extends any[] = [], Return = void>(
108-
recipe: (
109-
this: Draft<Base>,
110-
draft: Draft<Base>,
111-
...rest: Rest
112-
) => Return,
113-
defaultBase: Default
114-
): <T>(
115-
base: (Draft<T> extends Draft<Base> ? T : Base) | undefined,
116-
...rest: Rest
117-
) => Produced<Base, Return>
107+
<T = any, D = Draft<T>, Rest extends any[] = [], Return = void>(
108+
recipe: (this: D, draft: D, ...rest: Rest) => Return,
109+
defaultBase: T
110+
): (base: Immutable<D> | undefined, ...rest: Rest) => Produced<D, Return>
118111

119112
/** Curried producer with no default value */
120-
<Base = any, Rest extends any[] = [], Return = void>(
121-
recipe: (this: Draft<Base>, draft: Draft<Base>, ...rest: Rest) => Return
122-
): <T>(
123-
base: Draft<T> extends Draft<Base> ? T : Base,
124-
...rest: Rest
125-
) => Produced<Base, Return>
113+
<T = any, Rest extends any[] = [], Return = void>(
114+
recipe: (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return
115+
): (base: Immutable<T>, ...rest: Rest) => Produced<T, Return>
126116
}
127117

128118
export const produce: IProduce

0 commit comments

Comments
 (0)