Skip to content
Myr

Make explicit use of TypeScript's ReadonlySet and ReadonlyMap types. Fixes #494.

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

TypeScript provides the ReadonlySet and ReadonlyMap types which match their normal counterparts but without any methods that mutate their data. This change makes Immutable<Map> and Immutable<Set> map to those types directly. Worth noting that Map extends ReadonlyMap so we only need to check for one to know what the resulting type is, the same goes for Set and ReadonlySet. This also removes the intermediate Set/Map types since TypeScript no longer seems to need those (likely since TypeScript 3.7, see #448).

authored by Dave Townsend · committed by Dave Townsend · parent b55ef46 · Browse files · 3 files+68 added−20 removed
This commit

State

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

Diff stats

3 files+68 added−20 removed

  • tests:+50 added−0 removed
  • other:+18 added−20 removed

3 files+68 added−20 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:src/types.ts+18 added-20 removed
19 unmodified lines
2021/** Object types that should never be mapped */22type AtomicObject =23 | Function24 | WeakMap<any, any>25 | WeakSet<any>24 | Promise<any>25 | Date26 | RegExp27 | Boolean28 | Number29 | String3031/**32 * These should also never be mapped but must be tested after regular Map and33 * Set34 */35type WeakReferences = WeakMap<any, any> | WeakSet<any>3637export type Draft<T> = T extends AtomicObject38 ? T35 : T extends Map<infer K, infer V>36 ? DraftMap<K, V>37 : T extends Set<infer V>38 ? DraftSet<V>39 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap40 ? Map<Draft<K>, Draft<V>>41 : T extends ReadonlySet<infer V> // Set extends ReadonlySet42 ? Set<Draft<V>>43 : T extends WeakReferences44 ? T45 : T extends object46 ? {-readonly [K in keyof T]: Draft<T[K]>}47 : T4843// Inline these in ts 3.744interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}4546// Inline these in ts 3.747interface DraftSet<V> extends Set<Draft<V>> {}4849/** Convert a mutable type into a readonly type */50export type Immutable<T> = T extends AtomicObject51 ? T52 : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">53 ? ImmutableMap<K, V>54 : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">55 ? ImmutableSet<V>52 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap53 ? ReadonlyMap<Immutable<K>, Immutable<V>>54 : T extends ReadonlySet<infer V> // Set extends ReadonlySet55 ? ReadonlySet<Immutable<V>>56 : T extends WeakReferences57 ? T58 : T extends object59 ? {readonly [K in keyof T]: Immutable<T[K]>}60 : T6160interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}6162interface ImmutableSet<V> extends Set<Immutable<V>> {}6362export interface Patch {63 op: "replace" | "remove" | "add"64 path: (string | number)[]65 value?: any
120 unmodified lines