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
Provenance
- Commit
4204af5- Tree
0df40cb- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent 1
71f67e5- Parent 2
cdc50a9
4 files changed+86 added−22 removed
modified:__tests__/draft.ts+14 added
185 unmodified lines186 assert(toDraft(weak), weak)187 assert(fromDraft(toDraft(weak)), weak)188 }189 190+ // ReadonlyMap instance191+ {192+ let val: ReadonlyMap<any, any> = _193+ let draft: Map<any, any> = _194+ assert(toDraft(val), draft)195+ }196+197 // Set instance198 {199 let val: Set<any> = _200 assert(toDraft(val), val)4 unmodified lines205 assert(toDraft(weak), weak)206 assert(fromDraft(toDraft(weak)), weak)207 }208 209+ // ReadonlySet instance210+ {211+ let val: ReadonlySet<any> = _212+ let draft: Set<any> = _213+ assert(toDraft(val), draft)214+ }215+216 // Promise object217 {218 let val: Promise<any> = _219 assert(toDraft(val), val)86 unmodified linesmodified:__tests__/immutable.ts+36 added
54 unmodified lines55565758596061626364656667686970717273747576777879808182838485868788899091929394959654 unmodified lines let val = _ as Immutable<{a: {b: string}}> assert(val, _ as {readonly a: {readonly b: string}}) }
// Map { let val = _ as Immutable<Map<string, string>> assert(val, _ as ReadonlyMap<string, string>) }
// Already immutable Map { let val = _ as Immutable<ReadonlyMap<string, string>> assert(val, _ as ReadonlyMap<string, string>) }
// object in Map { let val = _ as Immutable<Map<{a: string}, {b: string}>> assert(val, _ as ReadonlyMap<{readonly a: string}, {readonly b: string}>) }
// Set { let val = _ as Immutable<Set<string>> assert(val, _ as ReadonlySet<string>) }
// Already immutable Set { let val = _ as Immutable<ReadonlySet<string>> assert(val, _ as ReadonlySet<string>) }
// object in Set { let val = _ as Immutable<Set<{a: string}>> assert(val, _ as ReadonlySet<{readonly a: string}>) }
expect(true).toBe(true)})modified:__tests__/produce.ts+18 added-2 removed
436 unmodified lines437 const m = new Map<string, S>([["a", {x: 1}]])438 const s = new Set<S>([{x: 2}])439 440 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}>)444 445 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 })450+451+it("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}])455+456+ 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}>)460+461+ 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 lines9 10 /** Object types that should never be mapped */11 type AtomicObject =12 | Function13- | WeakMap<any, any>14- | WeakSet<any>13 | Promise<any>14 | Date15 | RegExp16 | Boolean17 | Number18 | String19 20+/**21+ * These should also never be mapped but must be tested after regular Map and22+ * Set23+ */24+type WeakReferences = WeakMap<any, any> | WeakSet<any>25+26 export 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 : T37 32-// Inline these in ts 3.733-interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}34-35-// Inline these in ts 3.736-interface DraftSet<V> extends Set<Draft<V>> {}37-38 /** Convert a mutable type into a readonly type */39 export 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 : T50 49-interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}50-51-interface ImmutableSet<V> extends Set<Immutable<V>> {}52-51 export interface Patch {52 op: "replace" | "remove" | "add"53 path: (string | number)[]54 value?: any111 unmodified lines