arktype 2.0.0-dev.10 → 2.0.0-dev.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/out/api.d.ts +6 -0
  2. package/out/api.js +5 -0
  3. package/out/ark.d.ts +1 -1
  4. package/out/config.d.ts +1 -0
  5. package/out/config.js +2 -0
  6. package/out/generic.d.ts +3 -3
  7. package/out/match.js +1 -1
  8. package/out/module.d.ts +7 -6
  9. package/out/module.js +2 -3
  10. package/out/parser/definition.d.ts +11 -9
  11. package/out/parser/definition.js +2 -2
  12. package/out/parser/objectLiteral.d.ts +8 -13
  13. package/out/parser/objectLiteral.js +20 -23
  14. package/out/parser/semantic/bounds.d.ts +3 -3
  15. package/out/parser/semantic/divisor.d.ts +2 -2
  16. package/out/parser/semantic/infer.d.ts +1 -1
  17. package/out/parser/semantic/validate.d.ts +6 -4
  18. package/out/parser/string/reduce/dynamic.d.ts +6 -6
  19. package/out/parser/string/reduce/shared.d.ts +16 -16
  20. package/out/parser/string/reduce/shared.js +4 -2
  21. package/out/parser/string/shift/operand/date.d.ts +2 -2
  22. package/out/parser/string/shift/operand/enclosed.d.ts +17 -3
  23. package/out/parser/string/shift/operand/enclosed.js +2 -1
  24. package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
  25. package/out/parser/string/shift/operand/unenclosed.d.ts +9 -7
  26. package/out/parser/string/shift/operand/unenclosed.js +2 -2
  27. package/out/parser/string/shift/operator/bounds.d.ts +5 -5
  28. package/out/parser/string/shift/operator/bounds.js +3 -3
  29. package/out/parser/string/shift/operator/divisor.d.ts +1 -1
  30. package/out/parser/string/shift/operator/operator.d.ts +1 -1
  31. package/out/parser/string/string.d.ts +2 -2
  32. package/out/parser/tuple.d.ts +11 -11
  33. package/out/parser/tuple.js +3 -3
  34. package/out/scope.d.ts +15 -13
  35. package/out/scope.js +7 -7
  36. package/out/type.d.ts +4 -4
  37. package/out/type.js +3 -3
  38. package/package.json +8 -10
  39. package/__tests__/array.test.ts +0 -391
  40. package/__tests__/badDefinitionType.test.ts +0 -58
  41. package/__tests__/basis.test.ts +0 -50
  42. package/__tests__/bounds.test.ts +0 -318
  43. package/__tests__/cast.test.ts +0 -37
  44. package/__tests__/completions.test.ts +0 -51
  45. package/__tests__/config.test.ts +0 -96
  46. package/__tests__/dateLiteral.test.ts +0 -36
  47. package/__tests__/declared.test.ts +0 -124
  48. package/__tests__/discrimination.test.ts +0 -74
  49. package/__tests__/divisor.test.ts +0 -114
  50. package/__tests__/enclosed.test.ts +0 -107
  51. package/__tests__/examples.test.ts +0 -56
  52. package/__tests__/expressions.test.ts +0 -210
  53. package/__tests__/generateBenchData.ts +0 -89
  54. package/__tests__/generated/cyclic.ts +0 -3058
  55. package/__tests__/generic.test.ts +0 -313
  56. package/__tests__/group.test.ts +0 -85
  57. package/__tests__/hkt.test.ts +0 -22
  58. package/__tests__/imports.test.ts +0 -126
  59. package/__tests__/instanceof.test.ts +0 -104
  60. package/__tests__/intersection.test.ts +0 -108
  61. package/__tests__/keyTraversal.test.ts +0 -77
  62. package/__tests__/keyof.test.ts +0 -150
  63. package/__tests__/keywords.test.ts +0 -214
  64. package/__tests__/literal.test.ts +0 -90
  65. package/__tests__/match.bench.ts +0 -31
  66. package/__tests__/match.test.ts +0 -234
  67. package/__tests__/methods.bench.ts +0 -22
  68. package/__tests__/narrow.test.ts +0 -100
  69. package/__tests__/object.bench.ts +0 -28
  70. package/__tests__/objectLiteral.test.ts +0 -442
  71. package/__tests__/operand.bench.ts +0 -45
  72. package/__tests__/operator.bench.ts +0 -130
  73. package/__tests__/pipe.test.ts +0 -501
  74. package/__tests__/realWorld.test.ts +0 -63
  75. package/__tests__/regex.test.ts +0 -68
  76. package/__tests__/scope.bench.ts +0 -39
  77. package/__tests__/scope.test.ts +0 -374
  78. package/__tests__/serialization.test.ts +0 -19
  79. package/__tests__/string.test.ts +0 -52
  80. package/__tests__/submodule.test.ts +0 -86
  81. package/__tests__/this.test.ts +0 -83
  82. package/__tests__/thunk.test.ts +0 -86
  83. package/__tests__/traverse.test.ts +0 -141
  84. package/__tests__/tsGenerics.test.ts +0 -12
  85. package/__tests__/type.test.ts +0 -36
  86. package/__tests__/typeReference.test.ts +0 -39
  87. package/__tests__/unenclosed.test.ts +0 -136
  88. package/__tests__/union.test.ts +0 -214
  89. package/ark.ts +0 -47
  90. package/generic.ts +0 -69
  91. package/main.ts +0 -6
  92. package/match.ts +0 -218
  93. package/module.ts +0 -15
  94. package/out/ark.js.map +0 -1
  95. package/out/generic.js.map +0 -1
  96. package/out/main.js.map +0 -1
  97. package/out/match.js.map +0 -1
  98. package/out/module.js.map +0 -1
  99. package/out/parser/definition.js.map +0 -1
  100. package/out/parser/generic.js.map +0 -1
  101. package/out/parser/objectLiteral.js.map +0 -1
  102. package/out/parser/semantic/bounds.js.map +0 -1
  103. package/out/parser/semantic/divisor.js.map +0 -1
  104. package/out/parser/semantic/infer.js.map +0 -1
  105. package/out/parser/semantic/utils.js.map +0 -1
  106. package/out/parser/semantic/validate.js.map +0 -1
  107. package/out/parser/string/reduce/dynamic.js.map +0 -1
  108. package/out/parser/string/reduce/shared.js.map +0 -1
  109. package/out/parser/string/reduce/static.js.map +0 -1
  110. package/out/parser/string/shift/operand/date.js.map +0 -1
  111. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  112. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  113. package/out/parser/string/shift/operand/operand.js.map +0 -1
  114. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  115. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  116. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  117. package/out/parser/string/shift/operator/operator.js.map +0 -1
  118. package/out/parser/string/shift/scanner.js.map +0 -1
  119. package/out/parser/string/string.js.map +0 -1
  120. package/out/parser/tuple.js.map +0 -1
  121. package/out/scope.js.map +0 -1
  122. package/out/type.js.map +0 -1
  123. package/parser/definition.ts +0 -143
  124. package/parser/generic.ts +0 -66
  125. package/parser/objectLiteral.ts +0 -233
  126. package/parser/semantic/bounds.ts +0 -62
  127. package/parser/semantic/divisor.ts +0 -11
  128. package/parser/semantic/infer.ts +0 -141
  129. package/parser/semantic/utils.ts +0 -15
  130. package/parser/semantic/validate.ts +0 -117
  131. package/parser/string/reduce/dynamic.ts +0 -208
  132. package/parser/string/reduce/shared.ts +0 -99
  133. package/parser/string/reduce/static.ts +0 -296
  134. package/parser/string/shift/operand/date.ts +0 -55
  135. package/parser/string/shift/operand/enclosed.ts +0 -123
  136. package/parser/string/shift/operand/genericArgs.ts +0 -128
  137. package/parser/string/shift/operand/operand.ts +0 -42
  138. package/parser/string/shift/operand/unenclosed.ts +0 -217
  139. package/parser/string/shift/operator/bounds.ts +0 -270
  140. package/parser/string/shift/operator/divisor.ts +0 -33
  141. package/parser/string/shift/operator/operator.ts +0 -71
  142. package/parser/string/shift/scanner.ts +0 -210
  143. package/parser/string/string.ts +0 -85
  144. package/parser/tuple.ts +0 -542
  145. package/scope.ts +0 -291
  146. package/type.ts +0 -266
@@ -1,391 +0,0 @@
1
- import { attest, contextualize } from "@arktype/attest"
2
- import { writeUnresolvableMessage } from "@arktype/schema"
3
- import { scope, type } from "arktype"
4
- import { incompleteArrayTokenMessage } from "../parser/string/shift/operator/operator.js"
5
- import {
6
- multipleVariadicMesage,
7
- writeNonArraySpreadMessage
8
- } from "../parser/tuple.js"
9
-
10
- contextualize(
11
- "non-tuple",
12
- () => {
13
- it("allows and apply", () => {
14
- const t = type("string[]")
15
- attest<string[]>(t.infer)
16
- attest(t.allows([])).equals(true)
17
- attest(t([])).snap([])
18
- attest(t.allows(["foo", "bar"])).equals(true)
19
- attest(t(["foo", "bar"])).snap(["foo", "bar"])
20
- attest(t.allows(["foo", "bar", 5])).equals(false)
21
- attest(t(["foo", "bar", 5]).toString()).snap(
22
- "value at [2] must be a string (was number)"
23
- )
24
- attest(t.allows([5, "foo", "bar"])).equals(false)
25
- attest(t([5, "foo", "bar"]).toString()).snap(
26
- "value at [0] must be a string (was number)"
27
- )
28
- })
29
-
30
- it("nested", () => {
31
- const t = type("string[][]")
32
- attest<string[][]>(t.infer)
33
- attest(t.allows([])).equals(true)
34
- attest(t([])).snap([])
35
- attest(t.allows([["foo"]])).equals(true)
36
- attest(t([["foo"]])).snap([["foo"]])
37
- attest(t.allows(["foo"])).equals(false)
38
- attest(t(["foo"]).toString()).snap(
39
- "value at [0] must be an array (was string)"
40
- )
41
- attest(t.allows([["foo", 5]])).equals(false)
42
- attest(t([["foo", 5]]).toString()).snap(
43
- "value at [0][1] must be a string (was number)"
44
- )
45
- })
46
-
47
- it("tuple expression", () => {
48
- const t = type(["string", "[]"])
49
- attest<string[]>(t.infer)
50
- attest(t.json).equals(type("string[]").json)
51
- })
52
-
53
- it("root expression", () => {
54
- const t = type("string", "[]")
55
- attest<string[]>(t.infer)
56
- attest(t.json).equals(type("string[]").json)
57
- })
58
-
59
- it("chained", () => {
60
- const t = type({ a: "string" }).array()
61
- attest<{ a: string }[]>(t.infer)
62
-
63
- // @ts-expect-error
64
- attest(() => type({ a: "hmm" }).array()).throwsAndHasTypeError(
65
- writeUnresolvableMessage("hmm")
66
- )
67
- })
68
-
69
- it("incomplete token", () => {
70
- // @ts-expect-error
71
- attest(() => type("string[")).throwsAndHasTypeError(
72
- incompleteArrayTokenMessage
73
- )
74
- })
75
- },
76
- "non-variadic tuple",
77
- () => {
78
- it("shallow", () => {
79
- const t = type(["string", "number"])
80
- attest<[string, number]>(t.infer)
81
- attest(t.allows(["", 0])).equals(true)
82
- attest(t(["", 0])).snap(["", 0])
83
- attest(t.allows([true, 0])).equals(false)
84
- attest(t([true, 0]).toString()).snap(
85
- "value at [0] must be a string (was true)"
86
- )
87
- attest(t.allows([0, false])).equals(false)
88
- attest(t([0, false]).toString())
89
- .snap(`value at [0] must be a string (was number)
90
- value at [1] must be a number (was false)`)
91
- // too short
92
- attest(t.allows([""])).equals(false)
93
- attest(t([""]).toString()).snap('must be exactly length 2 (was [""])')
94
- // too long
95
- attest(t.allows(["", 0, 1])).equals(false)
96
- attest(t(["", 0, 1]).toString()).snap(
97
- 'must be exactly length 2 (was ["",0,1])'
98
- )
99
- // non-array
100
- attest(
101
- t.allows({
102
- length: 2,
103
- 0: "",
104
- 1: 0
105
- })
106
- ).equals(false)
107
- attest(
108
- t({
109
- length: 2,
110
- 0: "",
111
- 1: 0
112
- }).toString()
113
- ).snap("must be an array (was object)")
114
- })
115
-
116
- it("nested", () => {
117
- const t = type([["string", "number"], [{ a: "bigint", b: ["null"] }]])
118
- attest<
119
- [
120
- [string, number],
121
- [
122
- {
123
- a: bigint
124
- b: [null]
125
- }
126
- ]
127
- ]
128
- >(t.infer)
129
- const valid: typeof t.infer = [["", 0], [{ a: 0n, b: [null] }]]
130
- attest(t.allows(valid)).equals(true)
131
- attest(t(valid)).equals(valid)
132
- const invalid = [["", 0], [{ a: 0n, b: [undefined] }]]
133
- attest(t.allows(invalid)).equals(false)
134
- attest(t(invalid).toString()).snap(
135
- "value at [1][0].b[0] must be null (was undefined)"
136
- )
137
- })
138
-
139
- it("optional tuple", () => {
140
- const t = type(["string", "?"])
141
- attest<[string?]>(t.infer)
142
- attest(t([])).equals([])
143
- attest(t(["foo"])).equals(["foo"])
144
- attest(t([5]).toString()).snap(
145
- "value at [0] must be a string (was number)"
146
- )
147
- attest(t(["foo", "bar"]).toString()).snap(
148
- "must be at most length 1 (was 2)"
149
- )
150
- })
151
-
152
- it("nested optional tuple", () => {
153
- const t = type([["string", "?"], "string", "?"])
154
- attest<[[string?], string?]>(t.infer)
155
- })
156
- },
157
- "variadic tuple",
158
- () => {
159
- it("spreads simple arrays", () => {
160
- const wellRested = type(["string", "...", "number[]"])
161
- attest<[string, ...number[]]>(wellRested.infer)
162
- attest(wellRested(["foo"])).equals(["foo"])
163
- attest(wellRested(["foo", 1, 2])).equals(["foo", 1, 2])
164
- })
165
-
166
- it("spreads array expressions", () => {
167
- const greatSpread = type(["0", "...", "(Date|RegExp)[]"])
168
- attest<[0, ...(RegExp | Date)[]]>(greatSpread.infer)
169
- })
170
-
171
- it("distributes spread unions", () => {
172
- const t = type(["1", "...", "(Date[] | RegExp[])"])
173
- attest<[1, ...(Date[] | RegExp[])]>(t.infer)
174
- const expected = type(["1", "...", "Date[]"]).or(["1", "...", "RegExp[]"])
175
- attest(t.json).equals(expected.json)
176
- })
177
-
178
- it("distributes spread union tuples", () => {
179
- const counting = ["2", "3", "4"] as const
180
- const fibbing = ["1", "2", "3", "5", "8"] as const
181
- const countOrFib = type(counting, "|", fibbing)
182
- attest<[2, 3, 4] | [1, 2, 3, 5, 8]>(countOrFib.infer)
183
- const t = type(["1", "...", countOrFib])
184
- attest<[1, 2, 3, 4] | [1, 1, 2, 3, 5, 8]>(t.infer)
185
- const expected = type(["1", ...counting]).or(["1", ...fibbing])
186
- attest(t.json).equals(expected.json)
187
- })
188
-
189
- it("allows array keyword", () => {
190
- const types = scope({
191
- myArrayKeyword: "boolean[]",
192
- myVariadicKeyword: ["string", "...", "myArrayKeyword"]
193
- }).export()
194
- attest<[string, ...boolean[]]>(types.myVariadicKeyword.infer)
195
- })
196
-
197
- it("errors on non-array", () => {
198
- attest(() =>
199
- // @ts-expect-error
200
- type(["number", "...", "string"])
201
- ).throwsAndHasTypeError(writeNonArraySpreadMessage("string"))
202
- })
203
-
204
- it("allows multiple fixed spreads", () => {
205
- const t = type([
206
- "string",
207
- "...",
208
- "number[]",
209
- "...",
210
- ["boolean", "bigint"],
211
- "...",
212
- ["symbol"]
213
- ])
214
- const expected = type([
215
- "string",
216
- "...",
217
- "number[]",
218
- "boolean",
219
- "bigint",
220
- "symbol"
221
- ])
222
- attest<[string, ...number[], boolean, bigint, symbol]>(t.infer)
223
- attest<typeof expected.infer>(t.infer)
224
- attest(t.json).equals(expected.json)
225
- })
226
-
227
- it("errors on multiple variadic", () => {
228
- attest(() =>
229
- type([
230
- "...",
231
- "string[]",
232
- // @ts-expect-error
233
- "...",
234
- "number[]"
235
- ])
236
- ).throwsAndHasTypeError(multipleVariadicMesage)
237
- })
238
- },
239
- "intersection",
240
- () => {
241
- it("shallow array intersection", () => {
242
- const t = type("string[]&'foo'[]")
243
- const expected = type("'foo'[]")
244
- attest(t.json).equals(expected.json)
245
- })
246
-
247
- it("deep array intersection", () => {
248
- const t = type([{ a: "string" }, "[]"]).and([{ b: "number" }, "[]"])
249
- const expected = type([{ a: "string", b: "number" }, "[]"])
250
- attest(t.json).equals(expected.json)
251
- })
252
-
253
- it("tuple intersection", () => {
254
- const t = type([[{ a: "string" }], "&", [{ b: "boolean" }]])
255
- const expected = type([{ a: "string", b: "boolean" }])
256
- attest<typeof expected>(t)
257
- attest(t.json).equals(expected.json)
258
- })
259
-
260
- it("tuple and array", () => {
261
- const tupleAndArray = type([
262
- [{ a: "string" }],
263
- "&",
264
- [{ b: "boolean" }, "[]"]
265
- ])
266
- const arrayAndTuple = type([
267
- [{ b: "boolean" }, "[]"],
268
- "&",
269
- [{ a: "string" }]
270
- ])
271
-
272
- const expected = type([{ a: "string", b: "boolean" }])
273
- attest<typeof expected>(tupleAndArray)
274
-
275
- attest<typeof expected>(arrayAndTuple)
276
-
277
- attest(tupleAndArray.json).equals(expected.json)
278
- attest(arrayAndTuple.json).equals(expected.json)
279
- })
280
-
281
- it("variadic and tuple", () => {
282
- const b = type([{ b: "boolean" }, "[]"])
283
- const t = type([{ a: "string" }, "...", b]).and([
284
- { c: "number" },
285
- { d: "Date" }
286
- ])
287
- const expected = type([
288
- { a: "string", c: "number" },
289
- { b: "boolean", d: "Date" }
290
- ])
291
- attest(t.json).equals(expected.json)
292
- })
293
-
294
- it("variadic and array", () => {
295
- const b = type({ b: "boolean" }, "[]")
296
- const t = type([{ a: "string" }, "...", b]).and([{ c: "number" }, "[]"])
297
- const expected = type([
298
- { a: "string", c: "number" },
299
- "...",
300
- [{ b: "boolean", c: "number" }, "[]"]
301
- ])
302
- attest<typeof expected.infer>(t.infer)
303
- attest(t.json).equals(expected.json)
304
- })
305
-
306
- // based on the equivalent type-level test from @arktype/util
307
- it("kitchen sink", () => {
308
- const l = type([
309
- { a: "0" },
310
- { b: "1" },
311
- "?",
312
- { c: "2" },
313
- "?",
314
- "...",
315
- [{ d: "3" }, "[]"]
316
- ])
317
- const r = type([
318
- { e: "4" },
319
- "?",
320
- { f: "5" },
321
- "?",
322
- "...",
323
- [{ g: "6" }, "[]"]
324
- ])
325
- const result = l.and(r)
326
-
327
- const expected = type([
328
- { a: "0", e: "4" },
329
- { b: "1", f: "5" },
330
- "?",
331
- { c: "2", g: "6" },
332
- "?",
333
- "...",
334
- [{ d: "3", g: "6" }, "[]"]
335
- ])
336
-
337
- attest(expected.expression).snap(
338
- "[{ a: 0, e: 4 }, { b: 1, f: 5 }?, { c: 2, g: 6 }?, ...{ d: 3, g: 6 }[]]"
339
- )
340
-
341
- attest<typeof expected>(result)
342
- attest(result.json).equals(expected.json)
343
- })
344
-
345
- it("prefix and postfix", () => {
346
- const l = type(["...", [{ a: "0" }, "[]"], { b: "0" }, { c: "0" }])
347
- const r = type([{ x: "0" }, { y: "0" }, "...", [{ z: "0" }, "[]"]])
348
-
349
- // currently getting this "Expected" result at a type-level incurs
350
- // too high a performance cost for such a niche intersection.
351
- const expected = type([
352
- { a: "0", x: "0" },
353
- { a: "0", y: "0" },
354
- "...",
355
- [{ a: "0", z: "0" }, "[]"],
356
- { b: "0", z: "0" },
357
- { c: "0", z: "0" }
358
- ])
359
- .or([
360
- { a: "0", x: "0" },
361
- { b: "0", y: "0" },
362
- { c: "0", z: "0" }
363
- ])
364
- .or([
365
- { b: "0", x: "0" },
366
- { c: "0", y: "0" }
367
- ])
368
-
369
- const lrResult = l.and(r)
370
- attest(lrResult.json).snap(expected.json)
371
- const rlResult = r.and(l)
372
- attest(rlResult.json).snap(expected.json)
373
- })
374
-
375
- it("reduces minLength", () => {
376
- const t = type(["number", "number", "...", "number[]", "number"])
377
- const expected = type("number[]>=3")
378
- attest(t.json).equals(expected.json)
379
- })
380
- }
381
- )
382
-
383
- // TODO: reenable
384
- // describe("traversal", () => {
385
- // it("multiple errors", () => {
386
- // const stringArray = type("string[]")
387
- // attest(stringArray([1, 2]).toString()).snap(
388
- // "Item at index 0 must be a string (was number)\nItem at index 1 must be a string (was number)"
389
- // )
390
- // })
391
- // })
@@ -1,58 +0,0 @@
1
- import { attest, contextualize } from "@arktype/attest"
2
- import { type } from "arktype"
3
- import { writeBadDefinitionTypeMessage } from "../parser/definition.js"
4
-
5
- contextualize(() => {
6
- it("undefined", () => {
7
- // @ts-expect-error
8
- attest(() => type({ bad: undefined })).throwsAndHasTypeError(
9
- writeBadDefinitionTypeMessage("undefined")
10
- )
11
- })
12
-
13
- it("null", () => {
14
- // @ts-expect-error
15
- attest(() => type({ bad: null })).throwsAndHasTypeError(
16
- writeBadDefinitionTypeMessage("null")
17
- )
18
- })
19
-
20
- it("boolean", () => {
21
- // @ts-expect-error
22
- attest(() => type({ bad: true })).throwsAndHasTypeError(
23
- writeBadDefinitionTypeMessage("boolean")
24
- )
25
- })
26
-
27
- it("number", () => {
28
- // @ts-expect-error
29
- attest(() => type({ bad: 5 })).throwsAndHasTypeError(
30
- writeBadDefinitionTypeMessage("number")
31
- )
32
- })
33
-
34
- it("bigint", () => {
35
- // @ts-expect-error
36
- attest(() => type({ bad: 99999n })).throwsAndHasTypeError(
37
- writeBadDefinitionTypeMessage("bigint")
38
- )
39
- })
40
-
41
- it("symbol", () => {
42
- // @ts-expect-error
43
- attest(() => type({ bad: Symbol() })).throwsAndHasTypeError(
44
- writeBadDefinitionTypeMessage("symbol")
45
- )
46
- })
47
-
48
- it("any", () => {
49
- // doesn't error, so this test is just to ensure it doesn't infinitely recurse
50
- const t = type({ bad: {} as any })
51
- attest<{ bad: any }>(t.infer)
52
- })
53
-
54
- it("unknown", () => {
55
- // @ts-expect-error just results in base completions, so we just check there's an error
56
- attest(() => type({ bad: {} as unknown })).type.errors("")
57
- })
58
- })
@@ -1,50 +0,0 @@
1
- import { attest, contextualize } from "@arktype/attest"
2
- import { type } from "arktype"
3
- import { writeUnsatisfiableExpressionError } from "../parser/semantic/validate.js"
4
-
5
- contextualize("intersections", () => {
6
- it("class & literal", () => {
7
- const a = [0]
8
- const literal = type("===", a)
9
- const cls = type("instanceof", Array)
10
- const lr = literal.and(cls)
11
- attest<number[]>(lr.infer)
12
- attest(lr.json).equals(literal.json)
13
- const rl = cls.and(literal)
14
- attest<number[]>(rl.infer)
15
- attest(rl.json).equals(literal.json)
16
- })
17
-
18
- it("unsatisfiable class & literal", () => {
19
- const a = [0]
20
- const literal = type("===", a)
21
- const cls = type("instanceof", Date)
22
- attest(() => literal.and(cls)).throws(writeUnsatisfiableExpressionError(""))
23
- attest(() => cls.and(literal)).throws(writeUnsatisfiableExpressionError(""))
24
- })
25
-
26
- it("domain & literal", () => {
27
- const literal = type("'foo'")
28
- const domain = type("string")
29
- attest(literal.and(domain).json).equals(literal.json)
30
- attest(domain.and(literal).json).equals(literal.json)
31
- })
32
-
33
- it("unsatisfiable domain & literal", () => {
34
- const literal = type("'foo'")
35
- const domain = type("number")
36
- attest(() => literal.and(domain)).throws(
37
- writeUnsatisfiableExpressionError("")
38
- )
39
- attest(() => domain.and(literal)).throws(
40
- writeUnsatisfiableExpressionError("")
41
- )
42
- })
43
-
44
- it("domain & class", () => {
45
- const domain = type("object")
46
- const cls = type("instanceof", Date)
47
- attest(domain.and(cls).json).equals(cls.json)
48
- attest(cls.and(domain).json).equals(cls.json)
49
- })
50
- })