Skip to content
Myr

draft.ts

This file

Path

__tests__/draft.ts

300 lines · 6.6 KiB · TypeScript

Last changed by

refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6

History of this file

Take it

refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6

line 1why, one step back
1import {Draft} from "../dist/immer.js"

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 2–6why, one step back
2
3// For checking if a type is assignable to its draft type (and vice versa)4declare const toDraft: <T>(value: T) => Draft<T>5declare const fromDraft: <T>(draft: Draft<T>) => T6

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 7why, one step back
7// prettier-ignore

test(ts): improved exact type detectionaleclarsonbefore adoption2b7c21e

lines 8–10why, one step back
8type Exact<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0)
9 ? (A extends B ? (B extends A ? unknown : never) : never)10 : never

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 11why, one step back
11

test(ts): improved exact type detectionaleclarsonbefore adoption2b7c21e

lines 12–16why, one step back
12/** Fails when `actual` and `expected` have different types. */
13declare const exactType: <Actual, Expected>(14 actual: Actual & Exact<Actual, Expected>,15 expected: Expected & Exact<Actual, Expected>16) => Expected

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 17–20why, one step back
17
18// To remove TS2454 errors.19declare const _: any20

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 21–29why, one step back
21// DraftArray<T>
22{23 // NOTE: As of 3.2.2, everything fails without "extends any"24 const $ = <Value extends any>(val: ReadonlyArray<Value>) => {25 val = _ as Draft<typeof val>26 let elem: Value = _ as Draft<Value>27 }28}29

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 30–32why, one step back
30// Tuple
31{32 let val: [1, 2] = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 33–34why, one step back
33 exactType(toDraft(val), val)
34 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 35–39why, one step back
35}
3637// Tuple (nested in a tuple)38{39 let val: [[1, 2]] = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 40–41why, one step back
40 exactType(toDraft(val), val)
41 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 42–43why, one step back
42}
43

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 44–51why, one step back
44// Tuple (nested in two mutable arrays)
45{46 let val: [1, 2][][] = _47 let draft: typeof val = _48 val = exactType(toDraft(val), draft)49 exactType(fromDraft(draft), val)50}51

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 52–55why, one step back
52// Tuple (nested in two readonly arrays)
53{54 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _55 let draft: [1, 2][][] = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 56why, one step back
56 val = exactType(toDraft(val), draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 57–58why, one step back
57}
58

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 59–68why, one step back
59// Readonly tuple
60{61 // TODO: Uncomment this when readonly tuples are supported.62 // More info: https://stackoverflow.com/a/53822074/222855963 // let val: Readonly<[1, 2]> = _64 // let draft: [1, 2] = _65 // draft = exactType(toDraft(val), draft)66 // val = fromDraft(draft)67}68

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 69–71why, one step back
69// Mutable array
70{71 let val: string[] = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 72–73why, one step back
72 exactType(toDraft(val), val)
73 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 74–78why, one step back
74}
7576// Mutable array (nested in tuple)77{78 let val: [string[]] = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 79–80why, one step back
79 exactType(toDraft(val), val)
80 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 81–86why, one step back
81}
8283// Readonly array84{85 let val: ReadonlyArray<string> = _86 let draft: string[] = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 87–88why, one step back
87 val = exactType(toDraft(val), draft)
88 fromDraft(draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 89–94why, one step back
89}
9091// Readonly array (nested in readonly object)92{93 let val: {readonly a: ReadonlyArray<string>} = _94 let draft: {a: string[]} = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 95–96why, one step back
95 val = exactType(toDraft(val), draft)
96 fromDraft(draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 97–101why, one step back
97}
9899// Mutable object100{101 let val: {a: 1} = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 102–103why, one step back
102 exactType(toDraft(val), val)
103 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 104–108why, one step back
104}
105106// Mutable object (nested in mutable object)107{108 let val: {a: {b: 1}} = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 109–110why, one step back
109 exactType(toDraft(val), val)
110 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 111–118why, one step back
111}
112113// Interface114{115 interface Foo {116 a: {b: number}117 }118 let val: Foo = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 119–120why, one step back
119 exactType(toDraft(val), val)
120 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 121–131why, one step back
121}
122123// Interface (nested in interface)124{125 interface Foo {126 a: {b: number}127 }128 interface Bar {129 foo: Foo130 }131 let val: Bar = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 132–133why, one step back
132 exactType(toDraft(val), val)
133 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 134–137why, one step back
134}
135136// Readonly object137{

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 138why, one step back
138 let val: {readonly a: 1} = _

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

line 139why, one step back
139 let draft: {a: 1} = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 140why, one step back
140 val = exactType(toDraft(val), draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 141–144why, one step back
141}
142143// Readonly object (nested in tuple)144{

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 145–147why, one step back
145 let val: [{readonly a: 1}] = _
146 let draft: [{a: 1}] = _147 val = exactType(toDraft(val), draft)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 148–152why, one step back
148}
149150// Loose function151{152 let val: Function = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 153–154why, one step back
153 exactType(toDraft(val), val)
154 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 155–159why, one step back
155}
156157// Strict function158{159 let val: () => void = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 160–161why, one step back
160 exactType(toDraft(val), val)
161 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 162–163why, one step back
162}
163

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 164–189why, one step back
164// Class type (mutable)
165{166 class Foo {167 private test: any168 constructor(public bar: string) {}169 }170 let val: Foo = _171 // TODO: Uncomment this when plain object types can be distinguished from class types.172 // More info here: https://github.com/Microsoft/TypeScript/issues/29063173 // exactType(toDraft(val), val)174 // exactType(fromDraft(toDraft(val)), val)175}176177// Class type (readonly)178{179 class Foo {180 private test: any181 constructor(readonly bar: string) {}182 }183 let val: Foo = _184 // TODO: Uncomment this when plain object types can be distinguished from class types.185 // More info here: https://github.com/Microsoft/TypeScript/issues/29063186 // exactType(toDraft(val), val)187 // exactType(fromDraft(toDraft(val)), val)188}189

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 190–192why, one step back
190// Map instance
191{192 let val: Map<any, any> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 193–194why, one step back
193 exactType(toDraft(val), val)
194 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 195–197why, one step back
195
196 // Weak maps197 let weak: WeakMap<any, any> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 198–199why, one step back
198 exactType(toDraft(weak), weak)
199 exactType(fromDraft(toDraft(weak)), weak)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 200–204why, one step back
200}
201202// Set instance203{204 let val: Set<any> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 205–206why, one step back
205 exactType(toDraft(val), val)
206 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 207–209why, one step back
207
208 // Weak sets209 let weak: WeakSet<any> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 210–211why, one step back
210 exactType(toDraft(weak), weak)
211 exactType(fromDraft(toDraft(weak)), weak)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 212–216why, one step back
212}
213214// Promise object215{216 let val: Promise<any> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 217–218why, one step back
217 exactType(toDraft(val), val)
218 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 219–223why, one step back
219}
220221// Date instance222{223 let val: Date = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 224–225why, one step back
224 exactType(toDraft(val), val)
225 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 226–230why, one step back
226}
227228// RegExp instance229{230 let val: RegExp = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 231–232why, one step back
231 exactType(toDraft(val), val)
232 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 233–237why, one step back
233}
234235// Boxed primitive236{237 let val: Boolean = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 238–239why, one step back
238 exactType(toDraft(val), val)
239 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 240–244why, one step back
240}
241242// String literal243{244 let val: string = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 245–246why, one step back
245 exactType(toDraft(val), val)
246 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 247–251why, one step back
247}
248249// Any250{251 let val: any = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 252–253why, one step back
252 exactType(toDraft(val), val)
253 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 254–258why, one step back
254}
255256// Never257{258 let val: never = _ as never

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 259–260why, one step back
259 exactType(toDraft(val), val)
260 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 261–265why, one step back
261}
262263// Unknown264{265 let val: unknown = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 266–267why, one step back
266 exactType(toDraft(val), val)
267 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 268–272why, one step back
268}
269270// Numeral271{272 let val: 1 = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 273–274why, one step back
273 exactType(toDraft(val), val)
274 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 275–279why, one step back
275}
276277// Union of numerals278{279 let val: 1 | 2 | 3 = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 280–281why, one step back
280 exactType(toDraft(val), val)
281 exactType(fromDraft(toDraft(val)), val)

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

lines 282–286why, one step back
282}
283284// Union of tuple, array, object285{286 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 287–299why, one step back
287 let draft: [0] | string[] | {a: 1} = _
288 val = exactType(toDraft(val), draft)289}290291// Generic type292{293 // NOTE: "extends any" only helps a little.294 const $ = <T extends any>(val: ReadonlyArray<T>) => {295 let draft: Draft<typeof val> = _296 val = exactType(toDraft(val), draft)297 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]298 // exactType(fromDraft(draft), draft)299 }

chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b

line 300why, one step back
300}