Skip to content
Myr

draft.ts

This file

Path

__tests__/draft.ts

301 lines · 6.6 KiB · TypeScript

Last changed by

test(ts): improved exact type detectionaleclarsonbefore adoption2b7c21e

History of this file

Take it

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 1why, one step back
1import {Draft, DraftArray} 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–30why, 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 val = _ as DraftArray<typeof val>27 let elem: Value = _ as Draft<Value>28 }29}30

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 82–87why, one step back
82}
8384// Readonly array85{86 let val: ReadonlyArray<string> = _87 let draft: string[] = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 98–102why, one step back
98}
99100// Mutable object101{102 let val: {a: 1} = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 112–119why, one step back
112}
113114// Interface115{116 interface Foo {117 a: {b: number}118 }119 let val: Foo = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 122–132why, one step back
122}
123124// Interface (nested in interface)125{126 interface Foo {127 a: {b: number}128 }129 interface Bar {130 foo: Foo131 }132 let val: Bar = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 135–138why, one step back
135}
136137// Readonly object138{

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 142–145why, one step back
142}
143144// Readonly object (nested in tuple)145{

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 149–153why, one step back
149}
150151// Loose function152{153 let val: Function = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 156–160why, one step back
156}
157158// Strict function159{160 let val: () => void = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 165–190why, one step back
165// Class type (mutable)
166{167 class Foo {168 private test: any169 constructor(public bar: string) {}170 }171 let val: Foo = _172 // TODO: Uncomment this when plain object types can be distinguished from class types.173 // More info here: https://github.com/Microsoft/TypeScript/issues/29063174 // exactType(toDraft(val), val)175 // exactType(fromDraft(toDraft(val)), val)176}177178// Class type (readonly)179{180 class Foo {181 private test: any182 constructor(readonly bar: string) {}183 }184 let val: Foo = _185 // TODO: Uncomment this when plain object types can be distinguished from class types.186 // More info here: https://github.com/Microsoft/TypeScript/issues/29063187 // exactType(toDraft(val), val)188 // exactType(fromDraft(toDraft(val)), val)189}190

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 201–205why, one step back
201}
202203// Set instance204{205 let val: Set<any> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 213–217why, one step back
213}
214215// Promise object216{217 let val: Promise<any> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 220–224why, one step back
220}
221222// Date instance223{224 let val: Date = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 227–231why, one step back
227}
228229// RegExp instance230{231 let val: RegExp = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 234–238why, one step back
234}
235236// Boxed primitive237{238 let val: Boolean = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 241–245why, one step back
241}
242243// String literal244{245 let val: string = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 248–252why, one step back
248}
249250// Any251{252 let val: any = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 255–259why, one step back
255}
256257// Never258{259 let val: never = _ as never

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 262–266why, one step back
262}
263264// Unknown265{266 let val: unknown = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 269–273why, one step back
269}
270271// Numeral272{273 let val: 1 = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 276–280why, one step back
276}
277278// Union of numerals279{280 let val: 1 | 2 | 3 = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

lines 283–287why, one step back
283}
284285// Union of tuple, array, object286{287 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

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

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

line 301why, one step back
301}