Skip to content
Myr

Updated typings

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 · parent 7335671 · Browse files · 4 files+56 added−7 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+56 added−7 removed

  • tests:+31 added−0 removed
  • other:+25 added−7 removed

4 files+56 added−7 removed

modified:__tests__/produce.ts+31 added
414 unmodified lines
415416 expect(queueAfter).toEqual([1, 2, 3, 4])417 expect(queueBefore).toEqual([1, 2, 3])418})419420it("works with Map and Set", () => {421 const m = new Map([["a", {x: 1}]])422 const s = new Set([{x: 2}])423424 const res1 = produce(m, draft => {425 assert(draft, _ as Map<string, {x: number}>)426 })427 assert(res1, _ as Map<string, {x: number}>)428429 const res2 = produce(s, draft => {430 assert(draft, _ as Set<{x: number}>)431 })432 assert(res2, _ as Set<{x: number}>)433})434435it("works with readonly Map and Set", () => {436 type S = {readonly x: number}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.7442 })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.7447 })448 assert(res2, _ as Set<{readonly x: number}>)449})
modified:src/immer.d.ts+20 added-2 removed
6 unmodified lines
78/** Object types that should never be mapped */9type AtomicObject =10 | Function11 | Map<any, any>11 | WeakMap<any, any>13 | Set<any>12 | WeakSet<any>13 | Promise<any>14 | Date15 | RegExp
2 unmodified lines
18 | String1920export type Draft<T> = T extends AtomicObject21 ? T22 : T extends Map<infer K, infer V>23 ? DraftMap<K, V>24 : T extends Set<infer V>25 ? DraftSet<V>26 : T extends object27 ? {-readonly [K in keyof T]: Draft<T[K]>}28 : T2930// Inline these in ts 3.731interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}3233// Inline these in ts 3.734interface DraftSet<V> extends Set<Draft<V>> {}3536/** Convert a mutable type into a readonly type */37export type Immutable<T> = T extends AtomicObject38 ? T39 : T extends Map<infer K, infer V>40 ? // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">41 ImmutableMap<K, V>42 : T extends Set<infer V>43 ? // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">44 ImmutableSet<V>45 : T extends object46 ? {readonly [K in keyof T]: Immutable<T[K]>}47 : T4849interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}5051interface ImmutableSet<V> extends Set<Immutable<V>> {}5253export interface Patch {54 op: "replace" | "remove" | "add"55 path: (string | number)[]56 value?: any
253 unmodified lines
modified:package.json+1 added-1 removed
modified:yarn.lock+4 added-4 removed
6052 unmodified lines
6053 integrity sha1-WITKtRLPHTVeP7eE8wgEsrUg23I=6054 dependencies:6055 prelude-ls "~1.1.2"60566057typescript@3.4.3:6058 version "3.4.3"6059 resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.3.tgz#0eb320e4ace9b10eadf5bc6103286b0f8b7c224f"6060 integrity sha512-FFgHdPt4T/duxx6Ndf7hwgMZZjZpB+U0nMNGVCYPq0rEzWKjEDobm4J6yb3CS7naZ0yURFqdw9Gwc7UOh/P9oQ==6057typescript@3.6.4:6058 version "3.6.4"6059 resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.4.tgz#b18752bb3792bc1a0281335f7f6ebf1bbfc5b91d"6060 integrity sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==60616062uglify-js@^3.1.4:6063 version "3.5.3"6064 resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.5.3.tgz#d490bb5347f23025f0c1bc0dee901d98e4d6b063"
301 unmodified lines