Skip to content
Myr

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

2 files+9 added−40 removed

modified:__tests__/draft.ts+1 added-2 removed
1import {Draft, DraftArray} from "../dist/immer.js"1import {Draft} from "../dist/immer.js"23// For checking if a type is assignable to its draft type (and vice versa)4declare const toDraft: <T>(value: T) => Draft<T>5declare const fromDraft: <T>(draft: Draft<T>) => T
16 unmodified lines
22{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 lines
modified:src/immer.d.ts+8 added-38 removed
10 unmodified lines
11 | Boolean12 | Number13 | String1415type ArrayMethod = Exclude<keyof [], number>16type Indices<T> = Exclude<keyof T, ArrayMethod>15export type Draft<T> = T extends AtomicObject16 ? T17 : T extends object18 ? {-readonly [K in keyof T]: Draft<T[K]>}19 : T // mostly: unknown & any2018export type DraftArray<T extends ReadonlyArray<any>> = Array<19 {[P in Indices<T>]: Draft<T[P]>}[Indices<T>]20>2122export type DraftTuple<T extends ReadonlyArray<any>> = {23 [P in keyof T]: P extends Indices<T> ? Draft<T[P]> : never24}2526export type Draft<T> = T extends never[]21/** Convert a mutable type into a readonly type */22export 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 : T2728export interface Patch {29 op: "replace" | "remove" | "add"
11 unmodified lines
41 ? Base42 : Return extends Promise<infer Result>43 ? Promise<Result extends void ? Base : FromNothing<Result>>44 : FromNothing<Return>5556type ImmutableArray<T extends ReadonlyArray<any>> = {57 [P in Extract<keyof T, number>]: ReadonlyArray<Immutable<T[number]>>58}[Extract<keyof T, number>]5960type ImmutableTuple<T extends ReadonlyArray<any>> = {61 readonly [P in keyof T]: Immutable<T[P]>62}6364/** Convert a mutable type into a readonly type */65export 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 : T7445export 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 is
184 unmodified lines