Skip to content
Myr

draft.ts

This file

Path

__tests__/draft.ts

291 lines · 5.8 KiB · TypeScript

Last changed by

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

History of this file

Take it

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 2why, one step back
2import {Draft} from "../src/index"

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

lines 3–4why, one step back
3
4// For checking if a type is assignable to its draft type (and vice versa)

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 5–6why, one step back
5const toDraft: <T>(value: T) => Draft<T> = x => x as any
6const fromDraft: <T>(draft: Draft<T>) => T = x => x as any

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

line 7why, one step back
7

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 16why, one step back
16 }

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 17why, one step back
17

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 18–23why, one step back
18 // Tuple
19 {20 let val: [1, 2] = _21 assert(toDraft(val), val)22 assert(fromDraft(toDraft(val)), val)23 }

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

line 24why, one step back
24

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 25–30why, one step back
25 // Tuple (nested in a tuple)
26 {27 let val: [[1, 2]] = _28 assert(toDraft(val), val)29 assert(fromDraft(toDraft(val)), val)30 }

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

line 31why, one step back
31

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 32–38why, one step back
32 // Tuple (nested in two mutable arrays)
33 {34 let val: [1, 2][][] = _35 let draft: typeof val = _36 val = assert(toDraft(val), draft)37 assert(fromDraft(draft), val)38 }

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 39why, one step back
39

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 40–45why, one step back
40 // Tuple (nested in two readonly arrays)
41 {42 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _43 let draft: [1, 2][][] = _44 val = assert(toDraft(val), draft)45 }

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

line 46why, one step back
46

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 47–55why, one step back
47 // Readonly tuple
48 {49 // TODO: Uncomment this when readonly tuples are supported.50 // More info: https://stackoverflow.com/a/53822074/222855951 // let val: Readonly<[1, 2]> = _52 // let draft: [1, 2] = _53 // draft = assert(toDraft(val), draft)54 // val = fromDraft(draft)55 }

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 56why, one step back
56

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 57–62why, one step back
57 // Mutable array
58 {59 let val: string[] = _60 assert(toDraft(val), val)61 assert(fromDraft(toDraft(val)), val)62 }

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

line 63why, one step back
63

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 64–69why, one step back
64 // Mutable array (nested in tuple)
65 {66 let val: [string[]] = _67 assert(toDraft(val), val)68 assert(fromDraft(toDraft(val)), val)69 }

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

line 70why, one step back
70

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 71–77why, one step back
71 // Readonly array
72 {73 let val: ReadonlyArray<string> = _74 let draft: string[] = _75 val = assert(toDraft(val), draft)76 fromDraft(draft)77 }

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

line 78why, one step back
78

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 79–85why, one step back
79 // Readonly array (nested in readonly object)
80 {81 let val: {readonly a: ReadonlyArray<string>} = _82 let draft: {a: string[]} = _83 val = assert(toDraft(val), draft)84 fromDraft(draft)85 }

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

line 86why, one step back
86

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 87–92why, one step back
87 // Mutable object
88 {89 let val: {a: 1} = _90 assert(toDraft(val), val)91 assert(fromDraft(toDraft(val)), val)92 }

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

line 93why, one step back
93

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 94–99why, one step back
94 // Mutable object (nested in mutable object)
95 {96 let val: {a: {b: 1}} = _97 assert(toDraft(val), val)98 assert(fromDraft(toDraft(val)), val)99 }

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

line 100why, one step back
100

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 101–108why, one step back
101 // Interface
102 {103 interface Foo {104 a: {b: number}105 }106 let val: Foo = _107 assert(toDraft(val), val)108 assert(fromDraft(toDraft(val)), val)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 109why, one step back
109 }

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

line 110why, one step back
110

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 111–121why, one step back
111 // Interface (nested in interface)
112 {113 interface Foo {114 a: {b: number}115 }116 interface Bar {117 foo: Foo118 }119 let val: Bar = _120 assert(toDraft(val), val)121 assert(fromDraft(toDraft(val)), val)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 122why, one step back
122 }

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 123–128why, one step back
123
124 // Readonly object125 {126 let val: {readonly a: 1} = _127 let draft: {a: 1} = _128 val = assert(toDraft(val), draft)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 129why, one step back
129 }

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

line 130why, one step back
130

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 131–136why, one step back
131 // Readonly object (nested in tuple)
132 {133 let val: [{readonly a: 1}] = _134 let draft: [{a: 1}] = _135 val = assert(toDraft(val), draft)136 }

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

line 137why, one step back
137

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 138–143why, one step back
138 // Loose function
139 {140 let val: Function = _141 assert(toDraft(val), val)142 assert(fromDraft(toDraft(val)), val)143 }

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

line 144why, one step back
144

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 145–150why, one step back
145 // Strict function
146 {147 let val: () => void = _148 assert(toDraft(val), val)149 assert(fromDraft(toDraft(val)), val)150 }

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

line 151why, one step back
151

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 152–162why, one step back
152 // Class type (mutable)
153 {154 class Foo {155 private test: any156 constructor(public bar: string) {}157 }158 let val: Foo = _159 // TODO: Uncomment this when plain object types can be distinguished from class types.160 // More info here: https://github.com/Microsoft/TypeScript/issues/29063161 // assert(toDraft(val), val)162 // assert(fromDraft(toDraft(val)), val)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 163why, one step back
163 }

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 164why, one step back
164

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 176why, one step back
176 }

chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc

line 177why, one step back
177

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 178–182why, one step back
178 // Map instance
179 {180 let val: Map<any, any> = _181 assert(toDraft(val), val)182 assert(fromDraft(toDraft(val)), val)

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

line 183why, one step back
183

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 184–188why, one step back
184 // Weak maps
185 let weak: WeakMap<any, any> = _186 assert(toDraft(weak), weak)187 assert(fromDraft(toDraft(weak)), weak)188 }

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

line 189why, one step back
189

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 190–194why, one step back
190 // Set instance
191 {192 let val: Set<any> = _193 assert(toDraft(val), val)194 assert(fromDraft(toDraft(val)), val)

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

line 195why, one step back
195

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 196–200why, one step back
196 // Weak sets
197 let weak: WeakSet<any> = _198 assert(toDraft(weak), weak)199 assert(fromDraft(toDraft(weak)), weak)200 }

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

line 201why, one step back
201

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 202–207why, one step back
202 // Promise object
203 {204 let val: Promise<any> = _205 assert(toDraft(val), val)206 assert(fromDraft(toDraft(val)), val)207 }

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

line 208why, one step back
208

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 209–214why, one step back
209 // Date instance
210 {211 let val: Date = _212 assert(toDraft(val), val)213 assert(fromDraft(toDraft(val)), val)214 }

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

line 215why, one step back
215

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 216–221why, one step back
216 // RegExp instance
217 {218 let val: RegExp = _219 assert(toDraft(val), val)220 assert(fromDraft(toDraft(val)), val)221 }

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

line 222why, one step back
222

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 223–228why, one step back
223 // Boxed primitive
224 {225 let val: Boolean = _226 assert(toDraft(val), val)227 assert(fromDraft(toDraft(val)), val)228 }

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

line 229why, one step back
229

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 230–235why, one step back
230 // String literal
231 {232 let val: string = _233 assert(toDraft(val), val)234 assert(fromDraft(toDraft(val)), val)235 }

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

line 236why, one step back
236

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 237–242why, one step back
237 // Any
238 {239 let val: any = _240 assert(toDraft(val), val)241 assert(fromDraft(toDraft(val)), val)242 }

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

line 243why, one step back
243

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 244–249why, one step back
244 // Never
245 {246 let val: never = _ as never247 assert(toDraft(val), val)248 assert(fromDraft(toDraft(val)), val)249 }

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

line 250why, one step back
250

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 251–256why, one step back
251 // Unknown
252 {253 let val: unknown = _254 assert(toDraft(val), val)255 assert(fromDraft(toDraft(val)), val)256 }

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

line 257why, one step back
257

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 258–263why, one step back
258 // Numeral
259 {260 let val: 1 = _261 assert(toDraft(val), val)262 assert(fromDraft(toDraft(val)), val)263 }

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

line 264why, one step back
264

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 265–270why, one step back
265 // Union of numerals
266 {267 let val: 1 | 2 | 3 = _268 assert(toDraft(val), val)269 assert(fromDraft(toDraft(val)), val)270 }

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

line 271why, one step back
271

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 272–275why, one step back
272 // Union of tuple, array, object
273 {274 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _275 let draft: [0] | string[] | {a: 1} = _

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 276–277why, one step back
276 val = assert(toDraft(val), draft)
277 }

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 278–291why, one step back
278
279 // Generic type280 {281 // NOTE: "extends any" only helps a little.282 const $ = <T extends any>(val: ReadonlyArray<T>) => {283 let draft: Draft<typeof val> = _284 val = assert(toDraft(val), draft)285 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]286 // assert(fromDraft(draft), draft)287 }288 }289290 expect(true).toBe(true)291})