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
Provenance
- Commit
f8c65f0- Tree
857d881- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
7335671
4 files changed+56 added−7 removed
modified:__tests__/produce.ts+31 added
414 unmodified lines415 416 expect(queueAfter).toEqual([1, 2, 3, 4])417 expect(queueBefore).toEqual([1, 2, 3])418 })419+420+it("works with Map and Set", () => {421+ const m = new Map([["a", {x: 1}]])422+ const s = new Set([{x: 2}])423+424+ const res1 = produce(m, draft => {425+ assert(draft, _ as Map<string, {x: number}>)426+ })427+ assert(res1, _ as Map<string, {x: number}>)428+429+ const res2 = produce(s, draft => {430+ assert(draft, _ as Set<{x: number}>)431+ })432+ assert(res2, _ as Set<{x: number}>)433+})434+435+it("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}])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.7442+ })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.7447+ })448+ assert(res2, _ as Set<{readonly x: number}>)449+})modified:src/immer.d.ts+20 added-2 removed
6 unmodified lines7 8 /** Object types that should never be mapped */9 type AtomicObject =10 | Function11- | Map<any, any>11 | WeakMap<any, any>13- | Set<any>12 | WeakSet<any>13 | Promise<any>14 | Date15 | RegExp2 unmodified lines18 | String19 20 export 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 : T29 30+// Inline these in ts 3.731+interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}32+33+// Inline these in ts 3.734+interface DraftSet<V> extends Set<Draft<V>> {}35+36 /** Convert a mutable type into a readonly type */37 export 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 : T48 49+interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}50+51+interface ImmutableSet<V> extends Set<Immutable<V>> {}52+53 export interface Patch {54 op: "replace" | "remove" | "add"55 path: (string | number)[]56 value?: any253 unmodified linesmodified:package.json+1 added-1 removed
71 unmodified lines727374757676777879808 unmodified lines71 unmodified lines "regenerator-runtime": "^0.11.1", "rimraf": "^2.6.2", "seamless-immutable": "^7.1.3", "spec.ts": "^1.1.0", "typescript": "3.4.3", "typescript": "3.6.4", "yarn-or-npm": "^2.0.4" }, "jest": { "moduleFileExtensions": [8 unmodified linesmodified:yarn.lock+4 added-4 removed
6052 unmodified lines6053 integrity sha1-WITKtRLPHTVeP7eE8wgEsrUg23I=6054 dependencies:6055 prelude-ls "~1.1.2"6056 6057-typescript@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==6057+typescript@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==6061 6062 uglify-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