arktype 2.0.0-dev.9 → 2.0.0-rc.1

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 (263) hide show
  1. package/LICENSE +7 -0
  2. package/out/config.d.ts +25 -0
  3. package/out/config.js +2 -0
  4. package/out/generic.d.ts +157 -18
  5. package/out/generic.js +37 -23
  6. package/out/index.d.ts +7 -0
  7. package/out/index.js +7 -0
  8. package/out/intersect.d.ts +28 -0
  9. package/out/intersect.js +1 -0
  10. package/out/keys.d.ts +16 -0
  11. package/out/keys.js +1 -0
  12. package/out/keywords/ark.d.ts +39 -0
  13. package/out/keywords/ark.js +24 -0
  14. package/out/keywords/ast.d.ts +193 -0
  15. package/out/keywords/ast.js +2 -0
  16. package/out/keywords/builtins.d.ts +17 -0
  17. package/out/keywords/builtins.js +10 -0
  18. package/out/keywords/constructors/Array.d.ts +20 -0
  19. package/out/keywords/constructors/Array.js +19 -0
  20. package/out/keywords/constructors/FormData.d.ts +15 -0
  21. package/out/keywords/constructors/FormData.js +38 -0
  22. package/out/keywords/constructors/TypedArray.d.ts +19 -0
  23. package/out/keywords/constructors/TypedArray.js +14 -0
  24. package/out/keywords/constructors/constructors.d.ts +31 -0
  25. package/out/keywords/constructors/constructors.js +16 -0
  26. package/out/keywords/number/epoch.d.ts +10 -0
  27. package/out/keywords/number/epoch.js +26 -0
  28. package/out/keywords/number/integer.d.ts +3 -0
  29. package/out/keywords/number/integer.js +5 -0
  30. package/out/keywords/number/number.d.ts +28 -0
  31. package/out/keywords/number/number.js +9 -0
  32. package/out/keywords/string/alpha.d.ts +7 -0
  33. package/out/keywords/string/alpha.js +2 -0
  34. package/out/keywords/string/alphanumeric.d.ts +7 -0
  35. package/out/keywords/string/alphanumeric.js +2 -0
  36. package/out/keywords/string/capitalize.d.ts +15 -0
  37. package/out/keywords/string/capitalize.js +12 -0
  38. package/out/keywords/string/creditCard.d.ts +8 -0
  39. package/out/keywords/string/creditCard.js +37 -0
  40. package/out/keywords/string/date.d.ts +59 -0
  41. package/out/keywords/string/date.js +98 -0
  42. package/out/keywords/string/digits.d.ts +7 -0
  43. package/out/keywords/string/digits.js +2 -0
  44. package/out/keywords/string/email.d.ts +7 -0
  45. package/out/keywords/string/email.js +4 -0
  46. package/out/keywords/string/integer.d.ts +16 -0
  47. package/out/keywords/string/integer.js +16 -0
  48. package/out/keywords/string/ip.d.ts +20 -0
  49. package/out/keywords/string/ip.js +23 -0
  50. package/out/keywords/string/json.d.ts +15 -0
  51. package/out/keywords/string/json.js +45 -0
  52. package/out/keywords/string/lower.d.ts +15 -0
  53. package/out/keywords/string/lower.js +12 -0
  54. package/out/keywords/string/normalize.d.ts +92 -0
  55. package/out/keywords/string/normalize.js +43 -0
  56. package/out/keywords/string/numeric.d.ts +15 -0
  57. package/out/keywords/string/numeric.js +13 -0
  58. package/out/keywords/string/semver.d.ts +7 -0
  59. package/out/keywords/string/semver.js +4 -0
  60. package/out/keywords/string/string.d.ts +112 -0
  61. package/out/keywords/string/string.js +41 -0
  62. package/out/keywords/string/trim.d.ts +15 -0
  63. package/out/keywords/string/trim.js +14 -0
  64. package/out/keywords/string/upper.d.ts +15 -0
  65. package/out/keywords/string/upper.js +12 -0
  66. package/out/keywords/string/url.d.ts +15 -0
  67. package/out/keywords/string/url.js +37 -0
  68. package/out/keywords/string/utils.d.ts +2 -0
  69. package/out/keywords/string/utils.js +10 -0
  70. package/out/keywords/string/uuid.d.ts +42 -0
  71. package/out/keywords/string/uuid.js +19 -0
  72. package/out/keywords/ts.d.ts +64 -0
  73. package/out/keywords/ts.js +53 -0
  74. package/out/keywords/utils.d.ts +1 -0
  75. package/out/keywords/utils.js +2 -0
  76. package/out/module.d.ts +21 -9
  77. package/out/module.js +3 -3
  78. package/out/parser/definition.d.ts +18 -15
  79. package/out/parser/definition.js +5 -5
  80. package/out/parser/objectLiteral.d.ts +65 -35
  81. package/out/parser/objectLiteral.js +75 -50
  82. package/out/parser/semantic/bounds.d.ts +12 -9
  83. package/out/parser/semantic/default.d.ts +9 -0
  84. package/out/parser/semantic/default.js +1 -0
  85. package/out/parser/semantic/divisor.d.ts +5 -5
  86. package/out/parser/semantic/divisor.js +1 -0
  87. package/out/parser/semantic/infer.d.ts +29 -14
  88. package/out/parser/semantic/infer.js +1 -0
  89. package/out/parser/semantic/keyof.d.ts +7 -0
  90. package/out/parser/semantic/keyof.js +1 -0
  91. package/out/parser/semantic/utils.d.ts +4 -4
  92. package/out/parser/semantic/utils.js +1 -0
  93. package/out/parser/semantic/validate.d.ts +19 -18
  94. package/out/parser/semantic/validate.js +0 -1
  95. package/out/parser/string/reduce/dynamic.d.ts +16 -14
  96. package/out/parser/string/reduce/dynamic.js +8 -7
  97. package/out/parser/string/reduce/shared.d.ts +15 -15
  98. package/out/parser/string/reduce/shared.js +4 -2
  99. package/out/parser/string/reduce/static.d.ts +22 -23
  100. package/out/parser/string/reduce/static.js +1 -0
  101. package/out/parser/string/shift/operand/date.d.ts +2 -4
  102. package/out/parser/string/shift/operand/date.js +1 -1
  103. package/out/parser/string/shift/operand/enclosed.d.ts +13 -7
  104. package/out/parser/string/shift/operand/enclosed.js +5 -4
  105. package/out/parser/string/shift/operand/genericArgs.d.ts +13 -13
  106. package/out/parser/string/shift/operand/genericArgs.js +7 -12
  107. package/out/parser/string/shift/operand/operand.d.ts +8 -7
  108. package/out/parser/string/shift/operand/operand.js +5 -6
  109. package/out/parser/string/shift/operand/unenclosed.d.ts +28 -16
  110. package/out/parser/string/shift/operand/unenclosed.js +9 -12
  111. package/out/parser/string/shift/operator/bounds.d.ts +18 -22
  112. package/out/parser/string/shift/operator/bounds.js +11 -16
  113. package/out/parser/string/shift/operator/default.d.ts +19 -0
  114. package/out/parser/string/shift/operator/default.js +17 -0
  115. package/out/parser/string/shift/operator/divisor.d.ts +3 -3
  116. package/out/parser/string/shift/operator/divisor.js +1 -1
  117. package/out/parser/string/shift/operator/operator.d.ts +7 -6
  118. package/out/parser/string/shift/operator/operator.js +2 -2
  119. package/out/parser/string/shift/scanner.d.ts +29 -31
  120. package/out/parser/string/shift/scanner.js +24 -21
  121. package/out/parser/string/string.d.ts +17 -13
  122. package/out/parser/string/string.js +5 -2
  123. package/out/parser/tuple.d.ts +21 -19
  124. package/out/parser/tuple.js +12 -23
  125. package/out/scope.d.ts +116 -55
  126. package/out/scope.js +73 -59
  127. package/out/subtypes/array.d.ts +13 -0
  128. package/out/subtypes/array.js +1 -0
  129. package/out/subtypes/base.d.ts +85 -0
  130. package/out/subtypes/base.js +1 -0
  131. package/out/subtypes/date.d.ts +11 -0
  132. package/out/subtypes/date.js +1 -0
  133. package/out/subtypes/instantiate.d.ts +16 -0
  134. package/out/subtypes/instantiate.js +1 -0
  135. package/out/subtypes/morph.d.ts +13 -0
  136. package/out/subtypes/morph.js +1 -0
  137. package/out/subtypes/number.d.ts +12 -0
  138. package/out/subtypes/number.js +1 -0
  139. package/out/subtypes/object.d.ts +33 -0
  140. package/out/subtypes/object.js +1 -0
  141. package/out/subtypes/string.d.ts +13 -0
  142. package/out/subtypes/string.js +1 -0
  143. package/out/subtypes/validator.d.ts +11 -0
  144. package/out/subtypes/validator.js +1 -0
  145. package/out/type.d.ts +32 -49
  146. package/out/type.js +24 -14
  147. package/package.json +46 -45
  148. package/__tests__/array.test.ts +0 -391
  149. package/__tests__/badDefinitionType.test.ts +0 -58
  150. package/__tests__/basis.test.ts +0 -50
  151. package/__tests__/bounds.test.ts +0 -318
  152. package/__tests__/cast.test.ts +0 -37
  153. package/__tests__/completions.test.ts +0 -51
  154. package/__tests__/config.test.ts +0 -96
  155. package/__tests__/dateLiteral.test.ts +0 -36
  156. package/__tests__/declared.test.ts +0 -124
  157. package/__tests__/discrimination.test.ts +0 -74
  158. package/__tests__/divisor.test.ts +0 -114
  159. package/__tests__/enclosed.test.ts +0 -107
  160. package/__tests__/examples.test.ts +0 -56
  161. package/__tests__/expressions.test.ts +0 -210
  162. package/__tests__/generateBenchData.ts +0 -89
  163. package/__tests__/generated/cyclic.ts +0 -3058
  164. package/__tests__/generic.test.ts +0 -313
  165. package/__tests__/group.test.ts +0 -85
  166. package/__tests__/hkt.test.ts +0 -22
  167. package/__tests__/imports.test.ts +0 -126
  168. package/__tests__/instanceof.test.ts +0 -104
  169. package/__tests__/intersection.test.ts +0 -108
  170. package/__tests__/keyTraversal.test.ts +0 -77
  171. package/__tests__/keyof.test.ts +0 -150
  172. package/__tests__/keywords.test.ts +0 -214
  173. package/__tests__/literal.test.ts +0 -90
  174. package/__tests__/match.bench.ts +0 -31
  175. package/__tests__/match.test.ts +0 -234
  176. package/__tests__/methods.bench.ts +0 -22
  177. package/__tests__/narrow.test.ts +0 -100
  178. package/__tests__/object.bench.ts +0 -28
  179. package/__tests__/objectLiteral.test.ts +0 -442
  180. package/__tests__/operand.bench.ts +0 -45
  181. package/__tests__/operator.bench.ts +0 -130
  182. package/__tests__/pipe.test.ts +0 -501
  183. package/__tests__/realWorld.test.ts +0 -63
  184. package/__tests__/regex.test.ts +0 -68
  185. package/__tests__/scope.bench.ts +0 -39
  186. package/__tests__/scope.test.ts +0 -374
  187. package/__tests__/serialization.test.ts +0 -19
  188. package/__tests__/string.test.ts +0 -52
  189. package/__tests__/submodule.test.ts +0 -86
  190. package/__tests__/this.test.ts +0 -83
  191. package/__tests__/thunk.test.ts +0 -86
  192. package/__tests__/traverse.test.ts +0 -141
  193. package/__tests__/tsGenerics.test.ts +0 -12
  194. package/__tests__/type.test.ts +0 -36
  195. package/__tests__/typeReference.test.ts +0 -39
  196. package/__tests__/unenclosed.test.ts +0 -136
  197. package/__tests__/union.test.ts +0 -214
  198. package/ark.ts +0 -47
  199. package/generic.ts +0 -69
  200. package/main.ts +0 -6
  201. package/match.ts +0 -218
  202. package/module.ts +0 -15
  203. package/out/ark.d.ts +0 -28
  204. package/out/ark.js +0 -10
  205. package/out/ark.js.map +0 -1
  206. package/out/generic.js.map +0 -1
  207. package/out/main.d.ts +0 -6
  208. package/out/main.js +0 -5
  209. package/out/main.js.map +0 -1
  210. package/out/match.d.ts +0 -71
  211. package/out/match.js +0 -52
  212. package/out/match.js.map +0 -1
  213. package/out/module.js.map +0 -1
  214. package/out/parser/definition.js.map +0 -1
  215. package/out/parser/generic.d.ts +0 -16
  216. package/out/parser/generic.js +0 -15
  217. package/out/parser/generic.js.map +0 -1
  218. package/out/parser/objectLiteral.js.map +0 -1
  219. package/out/parser/semantic/bounds.js.map +0 -1
  220. package/out/parser/semantic/divisor.js.map +0 -1
  221. package/out/parser/semantic/infer.js.map +0 -1
  222. package/out/parser/semantic/utils.js.map +0 -1
  223. package/out/parser/semantic/validate.js.map +0 -1
  224. package/out/parser/string/reduce/dynamic.js.map +0 -1
  225. package/out/parser/string/reduce/shared.js.map +0 -1
  226. package/out/parser/string/reduce/static.js.map +0 -1
  227. package/out/parser/string/shift/operand/date.js.map +0 -1
  228. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  229. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  230. package/out/parser/string/shift/operand/operand.js.map +0 -1
  231. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  232. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  233. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  234. package/out/parser/string/shift/operator/operator.js.map +0 -1
  235. package/out/parser/string/shift/scanner.js.map +0 -1
  236. package/out/parser/string/string.js.map +0 -1
  237. package/out/parser/tuple.js.map +0 -1
  238. package/out/scope.js.map +0 -1
  239. package/out/type.js.map +0 -1
  240. package/parser/definition.ts +0 -143
  241. package/parser/generic.ts +0 -66
  242. package/parser/objectLiteral.ts +0 -233
  243. package/parser/semantic/bounds.ts +0 -62
  244. package/parser/semantic/divisor.ts +0 -11
  245. package/parser/semantic/infer.ts +0 -141
  246. package/parser/semantic/utils.ts +0 -15
  247. package/parser/semantic/validate.ts +0 -117
  248. package/parser/string/reduce/dynamic.ts +0 -208
  249. package/parser/string/reduce/shared.ts +0 -99
  250. package/parser/string/reduce/static.ts +0 -296
  251. package/parser/string/shift/operand/date.ts +0 -55
  252. package/parser/string/shift/operand/enclosed.ts +0 -123
  253. package/parser/string/shift/operand/genericArgs.ts +0 -128
  254. package/parser/string/shift/operand/operand.ts +0 -42
  255. package/parser/string/shift/operand/unenclosed.ts +0 -217
  256. package/parser/string/shift/operator/bounds.ts +0 -270
  257. package/parser/string/shift/operator/divisor.ts +0 -33
  258. package/parser/string/shift/operator/operator.ts +0 -71
  259. package/parser/string/shift/scanner.ts +0 -210
  260. package/parser/string/string.ts +0 -85
  261. package/parser/tuple.ts +0 -542
  262. package/scope.ts +0 -291
  263. 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
- })