Skip to content

Commit

Permalink
Updated typings
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Oct 30, 2019
1 parent 7335671 commit f8c65f0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
31 changes: 31 additions & 0 deletions __tests__/produce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,34 @@ it("works with generic array", () => {
expect(queueAfter).toEqual([1, 2, 3, 4])
expect(queueBefore).toEqual([1, 2, 3])
})

it("works with Map and Set", () => {
const m = new Map([["a", {x: 1}]])
const s = new Set([{x: 2}])

const res1 = produce(m, draft => {
assert(draft, _ as Map<string, {x: number}>)
})
assert(res1, _ as Map<string, {x: number}>)

const res2 = produce(s, draft => {
assert(draft, _ as Set<{x: number}>)
})
assert(res2, _ as Set<{x: number}>)
})

it("works with readonly Map and Set", () => {
type S = {readonly x: number}
const m = new Map<string, S>([["a", {x: 1}]])
const s = new Set<S>([{x: 2}])

const res1 = produce(m, (draft: Draft<Map<string, S>>) => {
assert(draft, _ as Map<string, {readonly x: number}>) // TODO: drop readonly in TS 3.7
})
assert(res1, _ as Map<string, {readonly x: number}>)

const res2 = produce(s, (draft: Draft<Set<S>>) => {
assert(draft, _ as Set<{readonly x: number}>) // TODO: drop readonly in TS 3.7
})
assert(res2, _ as Set<{readonly x: number}>)
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"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": {
Expand Down
22 changes: 20 additions & 2 deletions src/immer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ type Tail<T extends any[]> = ((...t: T) => any) extends ((
/** Object types that should never be mapped */
type AtomicObject =
| Function
| Map<any, any>
| WeakMap<any, any>
| Set<any>
| WeakSet<any>
| Promise<any>
| Date
Expand All @@ -21,17 +19,37 @@ type AtomicObject =

export type Draft<T> = T extends AtomicObject
? T
: T extends Map<infer K, infer V>
? DraftMap<K, V>
: T extends Set<infer V>
? DraftSet<V>
: T extends object
? {-readonly [K in keyof T]: Draft<T[K]>}
: T

// Inline these in ts 3.7
interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}

// Inline these in ts 3.7
interface DraftSet<V> extends Set<Draft<V>> {}

/** Convert a mutable type into a readonly type */
export type Immutable<T> = T extends AtomicObject
? T
: T extends Map<infer K, infer V>
? // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">
ImmutableMap<K, V>
: T extends Set<infer V>
? // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">
ImmutableSet<V>
: T extends object
? {readonly [K in keyof T]: Immutable<T[K]>}
: T

interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}

interface ImmutableSet<V> extends Set<Immutable<V>> {}

export interface Patch {
op: "replace" | "remove" | "add"
path: (string | number)[]
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6054,10 +6054,10 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

typescript@3.4.3:
version "3.4.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.3.tgz#0eb320e4ace9b10eadf5bc6103286b0f8b7c224f"
integrity sha512-FFgHdPt4T/duxx6Ndf7hwgMZZjZpB+U0nMNGVCYPq0rEzWKjEDobm4J6yb3CS7naZ0yURFqdw9Gwc7UOh/P9oQ==
typescript@3.6.4:
version "3.6.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.4.tgz#b18752bb3792bc1a0281335f7f6ebf1bbfc5b91d"
integrity sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==

uglify-js@^3.1.4:
version "3.5.3"
Expand Down

0 comments on commit f8c65f0

Please sign in to comment.