feat(ts): Improve type inference based on the first arg of the recipe function
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Alec Larson · parent 7e971b6 · Browse files · 3 files·+79 added−40 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+79 added−40 removed
- tests:+42 added−16 removed
- other:+37 added−24 removed
Provenance
- Commit
acf8a57- Tree
9ad0f48- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
7e971b6
3 files changed+79 added−40 removed
modified:__tests__/curry.js+1 added
52 unmodified lines53545556575859606117 unmodified lines52 unmodified lines )
expect(reducer(undefined, 3)).toEqual({hello: "world", index: 3}) expect(reducer({}, 3)).toEqual({index: 3}) expect(reducer()).toEqual({hello: "world", index: undefined}) })
it("can has fun with change detection", () => { const spread = produce(Object.assign)17 unmodified linesmodified:__tests__/produce.ts+41 added-16 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 it("can infer state type from default state", () => {73 type State = {readonly a: number} | boolean74 type Recipe = (base?: State | undefined) => State75 75- let foo = produce(_ => {}, {} as State)76+ let foo = produce((x: any) => {}, {} as State)77 exactType(foo, {} as Recipe)78 })79 80 it("can infer state type from recipe function", () => {81 type State = {readonly a: string} | {readonly b: string}81- type Recipe = (base: State | undefined, arg: number) => State82+ type Recipe = (base: State) => State83 83- let foo = produce((draft: Draft<State>, arg: number) => {}, {} as any)84+ let foo = produce((draft: Draft<State>) => {})85 exactType(foo, {} as Recipe)86 })87 88+it("can infer state type from recipe function with arguments", () => {89+ type State = {readonly a: string} | {readonly b: string}90+ type Recipe = (base: State, x: number) => State91+92+ let foo = produce((draft: Draft<State>, x: number) => {})93+ exactType(foo, {} as Recipe)94+})95+96+it("can infer state type from recipe function with arguments and initial state", () => {97+ type State = {readonly a: string} | {readonly b: string}98+ type Recipe = (base: State | undefined, x: number) => State99+100+ let foo = produce((draft: Draft<State>, x: number) => {}, {} as State)101+ exactType(foo, {} as Recipe)102+})103+104 it("cannot infer state type when the function type and default state are missing", () => {88- const res = produce(_ => {})105+ const res = produce((_: any) => {})106 exactType(res, {} as (base: any) => any)107+108+ // slightly different type inference...109+ const res2 = produce(_ => {})110+ exactType(res2, {} as (base: any, ...rest: any[]) => any)111 })112 113 it("can update readonly state via curried api", () => {93- const newState = produce<State>(draft => {114+ const newState = produce((draft: Draft<State>) => {115 draft.num++116 draft.foo = "bar"117 draft.bar = "foo"118 draft.baz.x++7 unmodified lines126 expect(newState).toEqual(expectedState)127 })128 129 it("can update use the non-default export", () => {109- const newState = produce2<State>(draft => {130+ const newState = produce2((draft: Draft<State>) => {131 draft.num++132 draft.foo = "bar"133 draft.bar = "foo"134 draft.baz.x++26 unmodified lines161 it("supports rest parameters", () => {162 type State = {readonly a: 1}163 164 // No initial state:144- let foo = produce<State, number[]>(() => {})145- exactType(foo, {} as (base: State, ...args: number[]) => State)165+ let foo = produce((s: State, a: number, b: number) => {})166+ exactType(foo, {} as (base: State, x: number, y: number) => State)167 foo({} as State, 1, 2)168 169 // TODO: Using argument parameters170 // let woo = produce((state: Draft<State>, ...args: number[]) => {})4 unmodified lines175 let bar = produce(176 (state: Draft<State>, ...args: number[]) => {},177 {} as State178 )158- exactType(bar, {} as (base?: State, ...args: number[]) => State)159- bar({} as State | undefined, 1, 2)179+ exactType(bar, {} as (180+ base?: undefined | Immutable<State>,181+ ...args: number[]182+ ) => State)183+ bar({} as State, 1, 2)184+ bar({} as State)185 bar()186 187 // When args is a tuple:188 let tup = produce(10 unmodified lines199 })200 201 it("can be passed a readonly array", () => {202 // No initial state:178- let foo = produce<ReadonlyArray<any>>(() => {})179- exactType(foo, {} as (base: readonly any[]) => readonly any[])180- foo([] as ReadonlyArray<any>)203+ let foo = produce((state: string[]) => {})204+ exactType(foo, {} as (base: readonly string[]) => readonly string[])205+ foo([] as ReadonlyArray<string>)206 207 // With initial state:208 let bar = produce(() => {}, [] as ReadonlyArray<any>)209 exactType(bar, {} as (base?: readonly any[]) => readonly any[])86 unmodified lines296 exactType(copy, base)297 })298 299 it("works with generic parameters", () => {275- let insert = <T>(array: ReadonlyArray<T>, index: number, elem: T) => {276- // NOTE: As of 3.2.2, the explicit argument type is required.300+ let insert = <T>(array: readonly T[], index: number, elem: T) => {301+ // Need explicit cast on draft as T[] is wider than readonly T[]302 return produce(array, (draft: T[]) => {303 draft.push(elem)304 draft.splice(index, 0, elem)305 draft.concat([elem])6 unmodified linesmodified:src/immer.d.ts+37 added-24 removed
1+type Tail<T extends any[]> = ((...t: T) => any) extends ((2+ _: any,3+ ...tail: infer TT4+) => any)5+ ? TT6+ : []7+8 /** Object types that should never be mapped */9 type AtomicObject =10 | Function11 | Map<any, any>56 unmodified lines68 * @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 */65- <Base = any, Return = void>(66- base: Base extends Function ? never : Base,67- recipe: (this: Draft<Base>, draft: Draft<Base>) => Return,68- listener?: PatchListener69- ): Produced<Base, Return>72 71- /** Curried producer with a default value */72- <Base = any, Rest extends any[] = [], Return = void>(73- recipe: (this: Base, draft: Base, ...rest: Rest) => Return,74- defaultBase: Immutable<Base>75- ): Rest[number][] extends Rest | never[]76- ? (77- // The `base` argument is optional when `Rest` is optional.78- base?: Immutable<Base>,79- ...rest: Rest80- ) => Produced<Immutable<Base>, Return>81- : (82- // The `base` argument is required when `Rest` is required.83- base: Immutable<Base> | undefined,84- ...rest: Rest85- ) => Produced<Immutable<Base>, Return>73+ /** Curried producer */74+ <75+ Recipe extends (...args: any[]) => any,76+ Params extends any[] = Parameters<Recipe>,77+ T = Params[0]78+ >(79+ recipe: Recipe80+ ): (81+ state: Immutable<T>,82+ ...rest: Tail<Params>83+ ) => Produced<Immutable<T>, ReturnType<Recipe>>84 87- /** Curried producer with no default value */88- <Base = any, Rest extends any[] = [], Return = void>(89- recipe: (this: Draft<Base>, draft: Draft<Base>, ...rest: Rest) => Return90- ): (base: Immutable<Base>, ...rest: Rest) => Produced<Base, Return>85+ /** Curried producer with initial state */86+ <87+ Recipe extends (...args: any[]) => any,88+ Params extends any[] = Parameters<Recipe>,89+ T = Params[0]90+ >(91+ recipe: Recipe,92+ initialState: T93+ ): (94+ state?: Immutable<T>,95+ ...rest: Tail<Params>96+ ) => Produced<Immutable<T>, ReturnType<Recipe>>97+98+ /** Normal producer */99+ <Base, D = Draft<Base>, Return = void>(100+ base: Base,101+ recipe: (this: D, draft: D) => Return,102+ listener?: PatchListener103+ ): Produced<Base, Return>104 }105 106 export const produce: IProduce107 export default produce138 unmodified lines