Skip to content
Myr

fix(ts): ensure produce returns an immutable type

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

authored by aleclarson · committed by aleclarson · parent 4bf4154 · Browse files · 3 files+91 added−8 removed
This commit

State

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

Diff stats

3 files+91 added−8 removed

  • tests:+74 added−6 removed
  • other:+17 added−2 removed

3 files+91 added−8 removed

added:__tests__/immutable.ts+66 added
modified:__tests__/produce.ts+8 added-6 removed
1 unmodified line
2 produce as produce2,3 applyPatches,4 Patch,5 nothing,6 Draft6 Draft,7 Immutable8} from "../dist/immer.js"910// prettier-ignore11type Exact<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0)
60 unmodified lines
72// NOTE: only when the function type is inferred73it("can infer state type from default state", () => {74 type Producer = <T>(75 base: (Draft<T> extends number ? T : number) | undefined75 ) => number76 ) => Immutable<T>77 let foo = produce(_ => {}, 1)78 exactType(foo, {} as Producer)79 exactType(foo(2), 0 as number)80})
2 unmodified lines
83 type Base = string | number84 type Producer = <T>(85 base: (Draft<T> extends Base ? T : Base) | undefined,86 _2: number86 ) => Base87 ) => Immutable<T>8889 let foo = produce((_: string | number, _2: number) => {}, 1)90 exactType(foo, {} as Producer)90 exactType(foo("", 0), {} as string | number)91 exactType(foo("", 0), {} as string)92 exactType(foo(0, 0), {} as number)93})9495it("cannot infer state type when the function type and default state are missing", () => {96 exactType(produce(_ => {}), {} as <T>(base: T) => any)
50 unmodified lines
147 type Foo = <T>(148 base: Draft<T> extends {} ? T : object,149 _2: number,150 _3: number149 ) => object151 ) => Immutable<T>152 let foo = produce((_1: object, _2: number, _3: number) => {})153 exactType(foo, {} as Foo)154 foo({}, 1, 2)155
1 unmodified line
157 type Bar = <T>(158 base: (Draft<T> extends {} ? T : object) | undefined,159 _2: number,160 _3: number159 ) => object161 ) => Immutable<T>162 let bar = produce((_1: object, _2: number, _3: number) => {}, {})163 exactType(bar, {} as Bar)164 bar(undefined, 1, 2)165})
37 unmodified lines
modified:src/immer.d.ts+17 added-2 removed
56 unmodified lines
5758/** The inferred return type of `produce` */59type Produced<Base, Return> = 1 extends HasVoidLike<Return>60 ? 1 extends IsVoidLike<Return>61 ? Base62 : Base | FromNothing<Exclude<Return, void>>61 ? Immutable<Base>62 : Immutable<Base> | FromNothing<Exclude<Return, void>>63 : FromNothing<Return>6465type ImmutableTuple<T extends ReadonlyArray<any>> = {66 readonly [P in keyof T]: Immutable<T[P]>67}6869/** Convert a mutable type into a readonly type */70export type Immutable<T> = T extends object71 ? T extends AtomicObject72 ? T73 : T extends ReadonlyArray<any>74 ? Array<T[number]> extends T75 ? {[P in keyof T]: ReadonlyArray<Immutable<T[number]>>}[keyof T]76 : ImmutableTuple<T>77 : {readonly [P in keyof T]: Immutable<T[P]>}78 : T7980export interface IProduce {81 /**82 * The `produce` function takes a value and a "recipe function" (whose83 * return value often depends on the base state). The recipe function is
141 unmodified lines