Skip to content
Myr

fix: improve higher order type inference for WritableDraft (#1205)

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

* fix: improve higher order type inference for WritableDraft * fix: support tuples with spread in WritableDraft ---------

authored by Homa Wong · committed by GitHub · parent 5155a33 · Browse files · 2 files+33 added−2 removed
This commit

State

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

Trailers

  • Co-authored-by: Michel Weststrate <mweststrate@gmail.com>

Diff stats

2 files+33 added−2 removed

  • tests:+18 added−0 removed
  • other:+15 added−2 removed

2 files+33 added−2 removed

modified:__tests__/produce.ts+18 added
769 unmodified lines
770 }771 })772})773774it("allows higher order type inference", () => {775 function _test<A>() {776 type S = {prop: A | undefined}777 produce(778 draft => {779 draft.prop = undefined780 },781 {prop: undefined} as S782 )783 }784})785786it("supports tuples with spread", () => {787 type State = [boolean, ...number[]]788 const foo = (x: State) => x789 produce<State>(x => foo(x))790})791792it("#877 - produce with typed state generic requires initial state", () => {793 const reducerNoInitial = produce<{count: number}, [{type: "inc"}]>(794 (draft, action: {type: "inc"}) => {795 if (action.type === "inc") draft.count++
14 unmodified lines
modified:src/types/types-external.ts+15 added-2 removed
28 unmodified lines
29type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>3031export type WritableDraft<T> = T extends any[]32 ? number extends T["length"]33 ? IsPlainArray<T> extends true33 ? Draft<T[number]>[]34 ? Draft<T[number]>[]35 : WritableNonArrayDraft<T>36 : WritableNonArrayDraft<T>37 : WritableNonArrayDraft<T>3839/**40 * Distinguishes plain array types (`number[]`, `readonly string[]`, etc.) from41 * tuple types — including variadic tuples like `[boolean, ...number[]]` — whose42 * `length` is also `number` but must not be widened to `Element[]`.43 */44type IsPlainArray<T extends readonly any[]> = T extends readonly (infer U)[]45 ? U[] extends T46 ? true47 : false48 : false4950type WritableNonArrayDraft<T> = {38 -readonly [K in keyof T]: T[K] extends infer V51 -readonly [K in keyof T]: {_: T[K]} extends {_: infer V}52 ? V extends object53 ? Draft<V>54 : V55 : never
200 unmodified lines