Skip to content
Myr

Merge branch 'readonly_types' of https://github.com/Mossop/immer into improved-types

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

authored by Michel Weststrate · committed by Michel Weststrate · parents 71f67e5, cdc50a9 · Browse files · 4 files+86 added−22 removed
This commit

State

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

Diff stats

4 files+86 added−22 removed

  • tests:+68 added−2 removed
  • other:+18 added−20 removed

4 files+86 added−22 removed

modified:__tests__/draft.ts+14 added
185 unmodified lines
186 assert(toDraft(weak), weak)187 assert(fromDraft(toDraft(weak)), weak)188 }189190 // ReadonlyMap instance191 {192 let val: ReadonlyMap<any, any> = _193 let draft: Map<any, any> = _194 assert(toDraft(val), draft)195 }196197 // Set instance198 {199 let val: Set<any> = _200 assert(toDraft(val), val)
4 unmodified lines
205 assert(toDraft(weak), weak)206 assert(fromDraft(toDraft(weak)), weak)207 }208209 // ReadonlySet instance210 {211 let val: ReadonlySet<any> = _212 let draft: Set<any> = _213 assert(toDraft(val), draft)214 }215216 // Promise object217 {218 let val: Promise<any> = _219 assert(toDraft(val), val)
86 unmodified lines
modified:__tests__/immutable.ts+36 added
modified:__tests__/produce.ts+18 added-2 removed
436 unmodified lines
437 const m = new Map<string, S>([["a", {x: 1}]])438 const s = new Set<S>([{x: 2}])439440 const res1 = produce(m, (draft: Draft<Map<string, S>>) => {441 assert(draft, _ as Map<string, {readonly x: number}>) // TODO: drop readonly in TS 3.7441 assert(draft, _ as Map<string, {x: number}>)442 })443 assert(res1, _ as Map<string, {readonly x: number}>)444445 const res2 = produce(s, (draft: Draft<Set<S>>) => {446 assert(draft, _ as Set<{readonly x: number}>) // TODO: drop readonly in TS 3.7446 assert(draft, _ as Set<{x: number}>)447 })448 assert(res2, _ as Set<{readonly x: number}>)449})450451it("works with ReadonlyMap and ReadonlySet", () => {452 type S = {readonly x: number}453 const m: ReadonlyMap<string, S> = new Map([["a", {x: 1}]])454 const s: ReadonlySet<S> = new Set([{x: 2}])455456 const res1 = produce(m, (draft: Draft<Map<string, S>>) => {457 assert(draft, _ as Map<string, {x: number}>)458 })459 assert(res1, _ as ReadonlyMap<string, {readonly x: number}>)460461 const res2 = produce(s, (draft: Draft<Set<S>>) => {462 assert(draft, _ as Set<{x: number}>)463 })464 assert(res2, _ as ReadonlySet<{readonly x: number}>)465})
modified:src/types-external.ts+18 added-20 removed
8 unmodified lines
910/** Object types that should never be mapped */11type AtomicObject =12 | Function13 | WeakMap<any, any>14 | WeakSet<any>13 | Promise<any>14 | Date15 | RegExp16 | Boolean17 | Number18 | String1920/**21 * These should also never be mapped but must be tested after regular Map and22 * Set23 */24type WeakReferences = WeakMap<any, any> | WeakSet<any>2526export type Draft<T> = T extends AtomicObject27 ? T24 : T extends Map<infer K, infer V>25 ? DraftMap<K, V>26 : T extends Set<infer V>27 ? DraftSet<V>28 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap29 ? Map<Draft<K>, Draft<V>>30 : T extends ReadonlySet<infer V> // Set extends ReadonlySet31 ? Set<Draft<V>>32 : T extends WeakReferences33 ? T34 : T extends object35 ? {-readonly [K in keyof T]: Draft<T[K]>}36 : T3732// Inline these in ts 3.733interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}3435// Inline these in ts 3.736interface DraftSet<V> extends Set<Draft<V>> {}3738/** Convert a mutable type into a readonly type */39export type Immutable<T> = T extends AtomicObject40 ? T41 : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">42 ? ImmutableMap<K, V>43 : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">44 ? ImmutableSet<V>41 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap42 ? ReadonlyMap<Immutable<K>, Immutable<V>>43 : T extends ReadonlySet<infer V> // Set extends ReadonlySet44 ? ReadonlySet<Immutable<V>>45 : T extends WeakReferences46 ? T47 : T extends object48 ? {readonly [K in keyof T]: Immutable<T[K]>}49 : T5049interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}5051interface ImmutableSet<V> extends Set<Immutable<V>> {}5251export interface Patch {52 op: "replace" | "remove" | "add"53 path: (string | number)[]54 value?: any
111 unmodified lines