refactor(ts): Simpler draft / immutable implementation
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 df68c23 · Browse files · 2 files·+9 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
2 files+9 added−40 removed
- tests:+1 added−2 removed
- other:+8 added−38 removed
Provenance
- Commit
7e971b6- Tree
e49611a- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
df68c23
2 files changed+9 added−40 removed
modified:__tests__/draft.ts+1 added-2 removed
1-import {Draft, DraftArray} from "../dist/immer.js"1+import {Draft} from "../dist/immer.js"2 3 // For checking if a type is assignable to its draft type (and vice versa)4 declare const toDraft: <T>(value: T) => Draft<T>5 declare const fromDraft: <T>(draft: Draft<T>) => T16 unmodified lines22 {23 // NOTE: As of 3.2.2, everything fails without "extends any"24 const $ = <Value extends any>(val: ReadonlyArray<Value>) => {25 val = _ as Draft<typeof val>26- val = _ as DraftArray<typeof val>26 let elem: Value = _ as Draft<Value>27 }28 }29 271 unmodified linesmodified:src/immer.d.ts+8 added-38 removed
10 unmodified lines11 | Boolean12 | Number13 | String14 15-type ArrayMethod = Exclude<keyof [], number>16-type Indices<T> = Exclude<keyof T, ArrayMethod>15+export type Draft<T> = T extends AtomicObject16+ ? T17+ : T extends object18+ ? {-readonly [K in keyof T]: Draft<T[K]>}19+ : T // mostly: unknown & any20 18-export type DraftArray<T extends ReadonlyArray<any>> = Array<19- {[P in Indices<T>]: Draft<T[P]>}[Indices<T>]20->21-22-export type DraftTuple<T extends ReadonlyArray<any>> = {23- [P in keyof T]: P extends Indices<T> ? Draft<T[P]> : never24-}25-26-export type Draft<T> = T extends never[]21+/** Convert a mutable type into a readonly type */22+export type Immutable<T> = T extends AtomicObject23 ? T28- : T extends ReadonlyArray<any>29- ? T[number][] extends T30- ? DraftArray<T>31- : DraftTuple<T>32- : T extends AtomicObject33- ? T24 : T extends object35- ? {-readonly [P in keyof T]: Draft<T[P]>}25+ ? {readonly [K in keyof T]: Immutable<T[K]>}26 : T27 28 export interface Patch {29 op: "replace" | "remove" | "add"11 unmodified lines41 ? Base42 : Return extends Promise<infer Result>43 ? Promise<Result extends void ? Base : FromNothing<Result>>44 : FromNothing<Return>55-56-type ImmutableArray<T extends ReadonlyArray<any>> = {57- [P in Extract<keyof T, number>]: ReadonlyArray<Immutable<T[number]>>58-}[Extract<keyof T, number>]59-60-type ImmutableTuple<T extends ReadonlyArray<any>> = {61- readonly [P in keyof T]: Immutable<T[P]>62-}63-64-/** Convert a mutable type into a readonly type */65-export type Immutable<T> = T extends object66- ? T extends AtomicObject67- ? T68- : T extends ReadonlyArray<any>69- ? Array<T[number]> extends T70- ? ImmutableArray<T>71- : ImmutableTuple<T>72- : {readonly [P in keyof T]: Immutable<T[P]>}73- : T74-45 export interface IProduce {46 /**47 * The `produce` function takes a value and a "recipe function" (whose48 * return value often depends on the base state). The recipe function is184 unmodified lines