Skip to content

Commit

Permalink
fix(ts): return undefined from produce
Browse files Browse the repository at this point in the history
Fixes #276
  • Loading branch information
aleclarson committed Dec 18, 2018
1 parent 9536348 commit d60db14
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/immer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ export interface Patch {

export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void

/** Includes 1 when `void` or `undefined` exists in type `T` */
type HasVoidLike<T> = (void extends T ? 1 : 0) | (undefined extends T ? 1 : 0)

/** Includes 1 when type `T` is `void` or `undefined` (or both) */
type IsVoidLike<T> =
| (T extends void ? 1 : 0)
| (T extends undefined ? 1 : 0)
| (T extends void | undefined ? 1 : 0)

/** Converts `nothing` into `undefined` */
type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T

/** The inferred return type of `produce` */
type Produced<Base, Return> = void extends Return
? Return extends void
type Produced<Base, Return> = 1 extends HasVoidLike<Return>
? 1 extends IsVoidLike<Return>
? Base
: Base | FromNothing<Exclude<Return, void>>
: FromNothing<Return>
Expand Down

0 comments on commit d60db14

Please sign in to comment.