Skip to content

Commit

Permalink
fix(ts): edge case with generic parameters
Browse files Browse the repository at this point in the history
Includes:
- a much improved DraftArray<T>
- an "extends object" constraint added to DraftObject<T>
- a relaxed DraftTuple<T> constraint (tuples cannot be readonly)

Fixes #272
  • Loading branch information
aleclarson committed Dec 17, 2018
1 parent a74b448 commit cb195cb
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/immer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@ type IsTuple<T extends ReadonlyArray<any>> = T extends never[]
? (U[] extends T ? false : true)
: never

export type DraftObject<T> = T extends object
? T extends AtomicObject
? T
: {-readonly [P in keyof T]: Draft<T[P]>}
: T
export type DraftObject<T extends object> = T extends AtomicObject
? T
: {-readonly [P in keyof T]: Draft<T[P]>}

export type DraftArray<T> = T extends ReadonlyArray<any>
? {[P in keyof T]: Array<Draft<T>>}[keyof T]
: Array<DraftObject<T>>
export type DraftArray<T, U = keyof T> = {[P in U]: Array<T>}[U]

This comment has been minimized.

Copy link
@aleclarson

aleclarson Dec 17, 2018

Author Member

Oh crud, somehow Draft got removed from inside the mapped object type. 😢

This fix is not quite ready. 😠


type ArrayMethod = Exclude<keyof [], number>

export type DraftTuple<T extends ReadonlyArray<any>> = Id<
export type DraftTuple<T extends any[]> = Id<

This comment has been minimized.

Copy link
@aleclarson

aleclarson Dec 17, 2018

Author Member

This is unrelated to the fix.

{[P in keyof T]: P extends ArrayMethod ? never : Draft<T[P]>}
>

Expand Down

0 comments on commit cb195cb

Please sign in to comment.