Skip to content

Commit

Permalink
fixup Draft<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Dec 18, 2018
1 parent 738bba5 commit 8ce6fd4
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/immer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,26 @@ type AtomicObject =
| Number
| String

type IsTuple<T extends ReadonlyArray<any>> = T extends never[]
? true
: T extends ReadonlyArray<infer U>
? (U[] extends T ? false : true)
: never

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

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

type ArrayMethod = Exclude<keyof [], number>

export type DraftTuple<T extends any[]> = Id<
export type DraftTuple<T extends ReadonlyArray<any>> = Id<
{[P in keyof T]: P extends ArrayMethod ? never : Draft<T[P]>}
>

export type Draft<T> = T extends any[]
? IsTuple<T> extends true
? DraftTuple<T>
: DraftArray<T[number]>
: T extends ReadonlyArray<any>
? DraftArray<T[number]>
export type Draft<T> = T extends ReadonlyArray<any>
? T extends never[]
? T
: T[number][] extends T
? DraftArray<T>
: DraftTuple<T>
: T extends AtomicObject
? T
: T extends object
? DraftObject<T>
? {-readonly [P in keyof T]: Draft<T[P]>}
: T

export interface Patch {
Expand Down

0 comments on commit 8ce6fd4

Please sign in to comment.