Skip to content
Myr

draft.ts

This file

Path

__tests__/draft.ts

287 lines · 5.5 KiB · TypeScript

Last changed by

style: use tabsuse-tabsbefore adoptiona9c0fd0

History of this file

Take it

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 1why, one step back
1import {assert, _} from "spec.ts"

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

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

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 8–9why, one step back
8// DraftArray<T>
9{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 10–14why, one step back
10 // NOTE: As of 3.2.2, everything fails without "extends any"
11 ;<Value extends any>(val: ReadonlyArray<Value>) => {12 val = _ as Draft<typeof val>13 let elem: Value = _ as Draft<Value>14 }

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 15–16why, one step back
15}
16

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

lines 17–18why, one step back
17// Tuple
18{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 19–21why, one step back
19 let val: [1, 2] = _
20 assert(toDraft(val), val)21 assert(fromDraft(toDraft(val)), val)

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

lines 22–25why, one step back
22}
2324// Tuple (nested in a tuple)25{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 26–28why, one step back
26 let val: [[1, 2]] = _
27 assert(toDraft(val), val)28 assert(fromDraft(toDraft(val)), val)

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

lines 29–30why, one step back
29}
30

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 31–32why, one step back
31// Tuple (nested in two mutable arrays)
32{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 33–36why, one step back
33 let val: [1, 2][][] = _
34 let draft: typeof val = _35 val = assert(toDraft(val), draft)36 assert(fromDraft(draft), val)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 37–38why, one step back
37}
38

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

lines 39–40why, one step back
39// Tuple (nested in two readonly arrays)
40{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 41–43why, one step back
41 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _
42 let draft: [1, 2][][] = _43 val = assert(toDraft(val), draft)

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

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

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 46–47why, one step back
46// Readonly tuple
47{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 48–53why, one step back
48 // TODO: Uncomment this when readonly tuples are supported.
49 // More info: https://stackoverflow.com/a/53822074/222855950 // let val: Readonly<[1, 2]> = _51 // let draft: [1, 2] = _52 // draft = assert(toDraft(val), draft)53 // val = fromDraft(draft)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 54–55why, one step back
54}
55

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

lines 56–57why, one step back
56// Mutable array
57{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 58–60why, one step back
58 let val: string[] = _
59 assert(toDraft(val), val)60 assert(fromDraft(toDraft(val)), val)

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

lines 61–64why, one step back
61}
6263// Mutable array (nested in tuple)64{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 65–67why, one step back
65 let val: [string[]] = _
66 assert(toDraft(val), val)67 assert(fromDraft(toDraft(val)), val)

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

lines 68–71why, one step back
68}
6970// Readonly array71{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 72–75why, one step back
72 let val: ReadonlyArray<string> = _
73 let draft: string[] = _74 val = assert(toDraft(val), draft)75 fromDraft(draft)

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

lines 76–79why, one step back
76}
7778// Readonly array (nested in readonly object)79{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 80–83why, one step back
80 let val: {readonly a: ReadonlyArray<string>} = _
81 let draft: {a: string[]} = _82 val = assert(toDraft(val), draft)83 fromDraft(draft)

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

lines 84–87why, one step back
84}
8586// Mutable object87{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 88–90why, one step back
88 let val: {a: 1} = _
89 assert(toDraft(val), val)90 assert(fromDraft(toDraft(val)), val)

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

lines 91–94why, one step back
91}
9293// Mutable object (nested in mutable object)94{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 95–97why, one step back
95 let val: {a: {b: 1}} = _
96 assert(toDraft(val), val)97 assert(fromDraft(toDraft(val)), val)

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

lines 98–101why, one step back
98}
99100// Interface101{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 102–107why, one step back
102 interface Foo {
103 a: {b: number}104 }105 let val: Foo = _106 assert(toDraft(val), val)107 assert(fromDraft(toDraft(val)), val)

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

lines 108–111why, one step back
108}
109110// Interface (nested in interface)111{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 112–120why, one step back
112 interface Foo {
113 a: {b: number}114 }115 interface Bar {116 foo: Foo117 }118 let val: Bar = _119 assert(toDraft(val), val)120 assert(fromDraft(toDraft(val)), val)

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

lines 121–124why, one step back
121}
122123// Readonly object124{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 125–127why, one step back
125 let val: {readonly a: 1} = _
126 let draft: {a: 1} = _127 val = assert(toDraft(val), draft)

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

lines 128–131why, one step back
128}
129130// Readonly object (nested in tuple)131{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 132–134why, one step back
132 let val: [{readonly a: 1}] = _
133 let draft: [{a: 1}] = _134 val = assert(toDraft(val), draft)

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

lines 135–138why, one step back
135}
136137// Loose function138{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 139–141why, one step back
139 let val: Function = _
140 assert(toDraft(val), val)141 assert(fromDraft(toDraft(val)), val)

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

lines 142–145why, one step back
142}
143144// Strict function145{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 146–148why, one step back
146 let val: () => void = _
147 assert(toDraft(val), val)148 assert(fromDraft(toDraft(val)), val)

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

lines 149–150why, one step back
149}
150

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 151–152why, one step back
151// Class type (mutable)
152{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 153–161why, one step back
153 class Foo {
154 private test: any155 constructor(public bar: string) {}156 }157 let val: Foo = _158 // TODO: Uncomment this when plain object types can be distinguished from class types.159 // More info here: https://github.com/Microsoft/TypeScript/issues/29063160 // assert(toDraft(val), val)161 // assert(fromDraft(toDraft(val)), val)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 162–165why, one step back
162}
163164// Class type (readonly)165{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 166–174why, one step back
166 class Foo {
167 private test: any168 constructor(readonly 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 // assert(toDraft(val), val)174 // assert(fromDraft(toDraft(val)), val)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 175–176why, one step back
175}
176

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

lines 177–178why, one step back
177// Map instance
178{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 179–181why, one step back
179 let val: Map<any, any> = _
180 assert(toDraft(val), val)181 assert(fromDraft(toDraft(val)), val)

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

line 182why, one step back
182

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 183–186why, one step back
183 // Weak maps
184 let weak: WeakMap<any, any> = _185 assert(toDraft(weak), weak)186 assert(fromDraft(toDraft(weak)), weak)

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

lines 187–190why, one step back
187}
188189// Set instance190{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 191–193why, one step back
191 let val: Set<any> = _
192 assert(toDraft(val), val)193 assert(fromDraft(toDraft(val)), val)

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

line 194why, one step back
194

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 195–198why, one step back
195 // Weak sets
196 let weak: WeakSet<any> = _197 assert(toDraft(weak), weak)198 assert(fromDraft(toDraft(weak)), weak)

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

lines 199–202why, one step back
199}
200201// Promise object202{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 203–205why, one step back
203 let val: Promise<any> = _
204 assert(toDraft(val), val)205 assert(fromDraft(toDraft(val)), val)

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

lines 206–209why, one step back
206}
207208// Date instance209{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 210–212why, one step back
210 let val: Date = _
211 assert(toDraft(val), val)212 assert(fromDraft(toDraft(val)), val)

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

lines 213–216why, one step back
213}
214215// RegExp instance216{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 217–219why, one step back
217 let val: RegExp = _
218 assert(toDraft(val), val)219 assert(fromDraft(toDraft(val)), val)

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

lines 220–223why, one step back
220}
221222// Boxed primitive223{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 224–226why, one step back
224 let val: Boolean = _
225 assert(toDraft(val), val)226 assert(fromDraft(toDraft(val)), val)

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

lines 227–230why, one step back
227}
228229// String literal230{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 231–233why, one step back
231 let val: string = _
232 assert(toDraft(val), val)233 assert(fromDraft(toDraft(val)), val)

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

lines 234–237why, one step back
234}
235236// Any237{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 238–240why, one step back
238 let val: any = _
239 assert(toDraft(val), val)240 assert(fromDraft(toDraft(val)), val)

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

lines 241–244why, one step back
241}
242243// Never244{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 245–247why, one step back
245 let val: never = _ as never
246 assert(toDraft(val), val)247 assert(fromDraft(toDraft(val)), val)

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

lines 248–251why, one step back
248}
249250// Unknown251{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 252–254why, one step back
252 let val: unknown = _
253 assert(toDraft(val), val)254 assert(fromDraft(toDraft(val)), val)

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

lines 255–258why, one step back
255}
256257// Numeral258{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 259–261why, one step back
259 let val: 1 = _
260 assert(toDraft(val), val)261 assert(fromDraft(toDraft(val)), val)

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

lines 262–265why, one step back
262}
263264// Union of numerals265{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 266–268why, one step back
266 let val: 1 | 2 | 3 = _
267 assert(toDraft(val), val)268 assert(fromDraft(toDraft(val)), val)

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

lines 269–272why, one step back
269}
270271// Union of tuple, array, object272{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 273–275why, one step back
273 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _
274 let draft: [0] | string[] | {a: 1} = _275 val = assert(toDraft(val), draft)

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

lines 276–279why, one step back
276}
277278// Generic type279{

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 280–286why, one step back
280 // NOTE: "extends any" only helps a little.
281 const $ = <T extends any>(val: ReadonlyArray<T>) => {282 let draft: Draft<typeof val> = _283 val = assert(toDraft(val), draft)284 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]285 // assert(fromDraft(draft), draft)286 }

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

line 287why, one step back
287}