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
Provenance
- Commit
dd25143- Tree
fd6e3ed- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
4bf4154
3 files changed+91 added−8 removed
added:__tests__/immutable.ts+66 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import {Immutable} from "../dist/immer.js"
// prettier-ignoretype Exact<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0) ? (A extends B ? (B extends A ? unknown : never) : never) : never
/** Fails when `actual` and `expected` have different types. */declare const exactType: <Actual, Expected>( actual: Actual & Exact<Actual, Expected>, expected: Expected & Exact<Actual, Expected>) => Expected
// array in tuple{ let val = {} as Immutable<[string[], 1]> exactType(val, {} as [ReadonlyArray<string>, 1])}
// tuple in array{ let val = {} as Immutable<[string, 1][]> exactType(val, {} as ReadonlyArray<[string, 1]>)}
// tuple in tuple{ let val = {} as Immutable<[[string, 1], 1]> exactType(val, {} as [[string, 1], 1])}
// array in array{ let val = {} as Immutable<string[][]> exactType(val, {} as ReadonlyArray<ReadonlyArray<string>>)}
// tuple in object{ let val = {} as Immutable<{a: [string, 1]}> exactType(val, {} as {readonly a: [string, 1]})}
// object in tuple{ let val = {} as Immutable<[{a: string}, 1]> exactType(val, {} as [{readonly a: string}, 1])}
// array in object{ let val = {} as Immutable<{a: string[]}> exactType(val, {} as {readonly a: ReadonlyArray<string>})}
// object in array{ let val = {} as Immutable<Array<{a: string}>> exactType(val, {} as ReadonlyArray<{readonly a: string}>)}
// object in object{ let val = {} as Immutable<{a: {b: string}}> exactType(val, {} as {readonly a: {readonly b: string}})}modified:__tests__/produce.ts+8 added-6 removed
1 unmodified line2 produce as produce2,3 applyPatches,4 Patch,5 nothing,6- Draft6+ Draft,7+ Immutable8 } from "../dist/immer.js"9 10 // prettier-ignore11 type Exact<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0)60 unmodified lines72 // NOTE: only when the function type is inferred73 it("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 lines83 type Base = string | number84 type Producer = <T>(85 base: (Draft<T> extends Base ? T : Base) | undefined,86 _2: number86- ) => Base87+ ) => Immutable<T>88 89 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 })94 95 it("cannot infer state type when the function type and default state are missing", () => {96 exactType(produce(_ => {}), {} as <T>(base: T) => any)50 unmodified lines147 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 line157 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 linesmodified:src/immer.d.ts+17 added-2 removed
56 unmodified lines57 58 /** The inferred return type of `produce` */59 type 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>64 65+type ImmutableTuple<T extends ReadonlyArray<any>> = {66+ readonly [P in keyof T]: Immutable<T[P]>67+}68+69+/** Convert a mutable type into a readonly type */70+export 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+ : T79+80 export 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 is141 unmodified lines