-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
produce.ts
193 lines (172 loc) · 4.98 KB
/
produce.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import produce, {
produce as produce2,
applyPatches,
Patch,
nothing,
Draft
} from "../dist/immer.js"
// prettier-ignore
type AssertEqual<T, U> = (<G>() => G extends T ? 1 : 0) extends (<G>() => G extends U ? 1 : 0) ? unknown : never
/** Trigger a compiler error when a value is _not_ an exact type. */
declare const exactType: <T, U>(
draft: T & AssertEqual<T, U>,
expected: U & AssertEqual<T, U>
) => U
interface State {
readonly num: number
readonly foo?: string
bar: string
readonly baz: {
readonly x: number
readonly y: number
}
readonly arr: ReadonlyArray<{readonly value: string}>
readonly arr2: {readonly value: string}[]
}
const state: State = {
num: 0,
bar: "foo",
baz: {
x: 1,
y: 2
},
arr: [{value: "asdf"}],
arr2: [{value: "asdf"}]
}
const expectedState: State = {
num: 1,
foo: "bar",
bar: "foo",
baz: {
x: 2,
y: 3
},
arr: [{value: "foo"}, {value: "asf"}],
arr2: [{value: "foo"}, {value: "asf"}]
}
it("can update readonly state via standard api", () => {
const newState = produce<State>(state, draft => {
draft.num++
draft.foo = "bar"
draft.bar = "foo"
draft.baz.x++
draft.baz.y++
draft.arr[0].value = "foo"
draft.arr.push({value: "asf"})
draft.arr2[0].value = "foo"
draft.arr2.push({value: "asf"})
})
expect(newState).not.toBe(state)
expect(newState).toEqual(expectedState)
})
// NOTE: only when the function type is inferred
it("can infer state type from default state", () => {
type Producer = <T>(
base: (T extends number ? T : number) | undefined
) => number
let foo = produce(_ => {}, 1)
exactType(foo, {} as Producer)
exactType(foo(2), 0 as number)
})
it("can infer state type from recipe function", () => {
type Base = string | number
type Producer = <T>(
base: (T extends Base ? T : Base) | undefined,
_2: number
) => Base
let foo = produce((_: string | number, _2: number) => {}, 1)
exactType(foo, {} as Producer)
exactType(foo("", 0), {} as string | number)
})
it("cannot infer state type when the function type and default state are missing", () => {
exactType(produce(_ => {}), {} as <T>(base: T) => any)
})
it("can update readonly state via curried api", () => {
const newState = produce<State>(draft => {
draft.num++
draft.foo = "bar"
draft.bar = "foo"
draft.baz.x++
draft.baz.y++
draft.arr[0].value = "foo"
draft.arr.push({value: "asf"})
draft.arr2[0].value = "foo"
draft.arr2.push({value: "asf"})
})(state)
expect(newState).not.toBe(state)
expect(newState).toEqual(expectedState)
})
it("can update use the non-default export", () => {
const newState = produce2<State>(draft => {
draft.num++
draft.foo = "bar"
draft.bar = "foo"
draft.baz.x++
draft.baz.y++
draft.arr[0].value = "foo"
draft.arr.push({value: "asf"})
draft.arr2[0].value = "foo"
draft.arr2.push({value: "asf"})
})(state)
expect(newState).not.toBe(state)
expect(newState).toEqual(expectedState)
})
it("can apply patches", () => {
let patches: Patch[] = []
produce(
{x: 3},
d => {
d.x++
},
p => {
patches = p
}
)
expect(applyPatches({}, patches)).toEqual({x: 4})
})
it("can provide rest parameters to a curried producer", () => {
type Foo = <T>(
base: Draft<T> extends {} ? T : object,
_2: number,
_3: number
) => object
let foo = produce((_1: object, _2: number, _3: number) => {})
exactType(foo, {} as Foo)
foo({}, 1, 2)
// With initial state:
type Bar = <T>(
base: (Draft<T> extends {} ? T : object) | undefined,
_2: number,
_3: number
) => object
let bar = produce((_1: object, _2: number, _3: number) => {}, {})
exactType(bar, {} as Bar)
bar(undefined, 1, 2)
})
it("can pass readonly arrays to curried producers", () => {
let foo = produce((_: any[]) => {})
foo([] as ReadonlyArray<any>)
// With initial state:
let bar = produce((_: any[]) => {}, [])
bar([] as ReadonlyArray<any>)
})
it("can produce nothing", () => {
let val: undefined = produce({}, s => nothing)
})
it("works with `void` hack", () => {
let obj: {readonly a: number} = {a: 1}
let val: typeof obj = produce(obj, s => void s.a++)
})
it("works with generic parameters", () => {
let insert = <T>(array: ReadonlyArray<T>, index: number, elem: T) => {
// NOTE: As of 3.2.2, the explicit argument type is required.
return produce(array, (draft: T[]) => {
draft.push(elem)
draft.splice(index, 0, elem)
draft.concat([elem])
})
}
let val: {readonly a: ReadonlyArray<number>} = 0 as any
let arr: ReadonlyArray<typeof val> = 0 as any
insert(arr, 0, val)
})