Skip to content
Myr

feat(ts): improve curried producers

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

The type signature for curried producers now accepts a widened version of the expected base state.

authored by Michel Weststrate · committed by Alec Larson · parent 6624aa8 · Browse files · 2 files+33 added−12 removed
This commit

State

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

Diff stats

2 files+33 added−12 removed

  • tests:+23 added−5 removed
  • other:+10 added−7 removed

2 files+33 added−12 removed

modified:__tests__/produce.ts+23 added-5 removed
68 unmodified lines
69})7071// NOTE: only when the function type is inferred72it("can infer state type from default state", () => {73 type State = {readonly a: number} | boolean74 type Recipe = (base?: State | undefined) => State73 type State = {readonly a: number}74 type Recipe = (base?: State | boolean) => State7576 let foo = produce((x: any) => {}, {} as State)77 exactType(foo, {} as Recipe)78})
258 unmodified lines
337 title: "hi",338 done: true339 })340 }341 const newState2: Immutable<State> = produce(reducer)(state)342 const newState3: Immutable<State> = produce(reducer, state)()341342 // base case for with-initial-state343 const newState4 = produce(reducer, state)(state)344 exactType(newState4, {} as State)345 // no argument case, in that case, immutable version recipe first arg will be inferred346 const newState5 = produce(reducer, state)()347 exactType(newState5, {} as Immutable<State>)348 // we can force the return type of the reducer by passing the generic argument349 const newState3 = produce(reducer, state)<State>()350 exactType(newState3, {} as State)351})352353it("can work with readonly base types", () => {354 type State = {
29 unmodified lines
384 done: true385 })386 }387 const newState2: State = produce(reducer)(state)388 exactType(newState2, {} as State)389390 // base case for with-initial-state391 const newState4 = produce(reducer, state)(state)392 exactType(newState4, {} as State)393 // no argument case, in that case, immutable version recipe first arg will be inferred380 const newState3: State = produce(reducer, state)()394 const newState5 = produce(reducer, state)()395 exactType(newState5, {} as Immutable<State>)396 // we can force the return type of the reducer by passing the generic argument397 const newState3 = produce(reducer, state)<State>()398 exactType(newState3, {} as State)399})
modified:src/immer.d.ts+10 added-7 removed
75 unmodified lines
76 Params extends any[] = Parameters<Recipe>,77 T = Params[0]78 >(79 recipe: Recipe80 ): (81 state: Immutable<T>,80 ): <S extends Immutable<T>>(81 state: S,82 ...rest: Tail<Params>83 ) => Produced<Immutable<T>, ReturnType<Recipe>>83 ) => Produced<S, ReturnType<Recipe>>84 // ^ by making the returned type generic, the actual type of the passed in object is preferred85 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe86 // Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct!8788 /** Curried producer with initial state */89 <90 Recipe extends (...args: any[]) => any,91 Params extends any[] = Parameters<Recipe>,92 T = Params[0]93 >(94 recipe: Recipe,92 initialState: T93 ): (94 state?: Immutable<T>,95 initialState: Immutable<T>96 ): <S extends Immutable<T>>(97 state?: S,98 ...rest: Tail<Params>96 ) => Produced<Immutable<T>, ReturnType<Recipe>>99 ) => Produced<S, ReturnType<Recipe>>100101 /** Normal producer */102 <Base, D = Draft<Base>, Return = void>(103 base: Base,
145 unmodified lines