arktype 2.0.0-dev.2 → 2.0.0-dev.21

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 (223) hide show
  1. package/LICENSE +7 -0
  2. package/out/api.d.ts +6 -0
  3. package/out/api.js +5 -0
  4. package/out/ark.d.ts +15 -39
  5. package/out/ark.js +9 -18
  6. package/out/config.d.ts +1 -0
  7. package/out/config.js +2 -0
  8. package/out/generic.d.ts +21 -0
  9. package/out/generic.js +24 -0
  10. package/out/match.d.ts +18 -24
  11. package/out/match.js +50 -60
  12. package/out/module.d.ts +11 -0
  13. package/out/module.js +2 -0
  14. package/out/parser/definition.d.ts +13 -15
  15. package/out/parser/definition.js +10 -13
  16. package/out/parser/generic.d.ts +5 -7
  17. package/out/parser/generic.js +7 -11
  18. package/out/parser/objectLiteral.d.ts +57 -18
  19. package/out/parser/objectLiteral.js +93 -52
  20. package/out/parser/semantic/bounds.d.ts +10 -8
  21. package/out/parser/semantic/bounds.js +0 -1
  22. package/out/parser/semantic/divisor.d.ts +5 -7
  23. package/out/parser/semantic/divisor.js +0 -1
  24. package/out/parser/semantic/infer.d.ts +24 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +3 -7
  27. package/out/parser/semantic/utils.js +0 -1
  28. package/out/parser/semantic/validate.d.ts +16 -13
  29. package/out/parser/semantic/validate.js +1 -1
  30. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  31. package/out/parser/string/reduce/dynamic.js +31 -26
  32. package/out/parser/string/reduce/shared.d.ts +14 -28
  33. package/out/parser/string/reduce/shared.js +4 -3
  34. package/out/parser/string/reduce/static.d.ts +26 -23
  35. package/out/parser/string/reduce/static.js +0 -1
  36. package/out/parser/string/shift/operand/date.d.ts +2 -2
  37. package/out/parser/string/shift/operand/date.js +4 -7
  38. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  39. package/out/parser/string/shift/operand/enclosed.js +14 -14
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  41. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  42. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  43. package/out/parser/string/shift/operand/operand.js +9 -13
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -20
  45. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  46. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  47. package/out/parser/string/shift/operator/bounds.js +45 -56
  48. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  49. package/out/parser/string/shift/operator/divisor.js +1 -3
  50. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  51. package/out/parser/string/shift/operator/operator.js +12 -19
  52. package/out/parser/string/shift/scanner.d.ts +8 -7
  53. package/out/parser/string/shift/scanner.js +17 -16
  54. package/out/parser/string/string.d.ts +9 -7
  55. package/out/parser/string/string.js +1 -3
  56. package/out/parser/tuple.d.ts +101 -42
  57. package/out/parser/tuple.js +107 -86
  58. package/out/scope.d.ts +44 -115
  59. package/out/scope.js +39 -188
  60. package/out/type.d.ts +63 -61
  61. package/out/type.js +30 -135
  62. package/package.json +33 -39
  63. package/__tests__/array.test.ts +0 -310
  64. package/__tests__/astUtils.test.ts +0 -17
  65. package/__tests__/badDefinitionType.test.ts +0 -51
  66. package/__tests__/basis.test.ts +0 -46
  67. package/__tests__/bounds.test.ts +0 -388
  68. package/__tests__/cast.test.ts +0 -32
  69. package/__tests__/completions.test.ts +0 -46
  70. package/__tests__/config.test.ts +0 -77
  71. package/__tests__/cyclic.test.ts +0 -144
  72. package/__tests__/dateLiteral.test.ts +0 -32
  73. package/__tests__/declared.test.ts +0 -124
  74. package/__tests__/discrimination.test.ts +0 -74
  75. package/__tests__/divisor.test.ts +0 -90
  76. package/__tests__/enclosed.test.ts +0 -93
  77. package/__tests__/examples.test.ts +0 -56
  78. package/__tests__/expressions.test.ts +0 -138
  79. package/__tests__/generateBenchData.ts +0 -89
  80. package/__tests__/generated/cyclic.ts +0 -3058
  81. package/__tests__/generic.test.ts +0 -294
  82. package/__tests__/group.test.ts +0 -72
  83. package/__tests__/hkt.test.ts +0 -22
  84. package/__tests__/imports.test.ts +0 -113
  85. package/__tests__/instanceof.test.ts +0 -102
  86. package/__tests__/intersection.test.ts +0 -104
  87. package/__tests__/jsdoc.test.ts +0 -64
  88. package/__tests__/keyTraversal.test.ts +0 -74
  89. package/__tests__/keyof.test.ts +0 -134
  90. package/__tests__/keywords.test.ts +0 -188
  91. package/__tests__/literal.test.ts +0 -51
  92. package/__tests__/match.bench.ts +0 -31
  93. package/__tests__/match.test.ts +0 -224
  94. package/__tests__/methods.bench.ts +0 -22
  95. package/__tests__/morph.test.ts +0 -409
  96. package/__tests__/narrow.test.ts +0 -102
  97. package/__tests__/nodeDefinitions.test.ts +0 -190
  98. package/__tests__/object.bench.ts +0 -28
  99. package/__tests__/objectLiteral.test.ts +0 -301
  100. package/__tests__/operand.bench.ts +0 -45
  101. package/__tests__/operator.bench.ts +0 -130
  102. package/__tests__/regex.test.ts +0 -58
  103. package/__tests__/scope.bench.ts +0 -39
  104. package/__tests__/scope.test.ts +0 -140
  105. package/__tests__/string.test.ts +0 -48
  106. package/__tests__/submodule.test.ts +0 -99
  107. package/__tests__/this.test.ts +0 -57
  108. package/__tests__/thunk.test.ts +0 -80
  109. package/__tests__/traverse.test.ts +0 -119
  110. package/__tests__/tsGenerics.test.ts +0 -12
  111. package/__tests__/typeMethods.test.ts +0 -69
  112. package/__tests__/typeReference.test.ts +0 -39
  113. package/__tests__/unenclosed.test.ts +0 -120
  114. package/__tests__/union.test.ts +0 -147
  115. package/ark.ts +0 -91
  116. package/main.ts +0 -18
  117. package/match.ts +0 -243
  118. package/out/ark.d.ts.map +0 -1
  119. package/out/ark.js.map +0 -1
  120. package/out/arkConfig.d.ts +0 -2
  121. package/out/arkConfig.d.ts.map +0 -1
  122. package/out/arkConfig.js +0 -10
  123. package/out/arkConfig.js.map +0 -1
  124. package/out/main.d.ts +0 -7
  125. package/out/main.d.ts.map +0 -1
  126. package/out/main.js +0 -4
  127. package/out/main.js.map +0 -1
  128. package/out/match.d.ts.map +0 -1
  129. package/out/match.js.map +0 -1
  130. package/out/parser/definition.d.ts.map +0 -1
  131. package/out/parser/definition.js.map +0 -1
  132. package/out/parser/generic.d.ts.map +0 -1
  133. package/out/parser/generic.js.map +0 -1
  134. package/out/parser/objectLiteral.d.ts.map +0 -1
  135. package/out/parser/objectLiteral.js.map +0 -1
  136. package/out/parser/semantic/bounds.d.ts.map +0 -1
  137. package/out/parser/semantic/bounds.js.map +0 -1
  138. package/out/parser/semantic/divisor.d.ts.map +0 -1
  139. package/out/parser/semantic/divisor.js.map +0 -1
  140. package/out/parser/semantic/intersections.d.ts +0 -14
  141. package/out/parser/semantic/intersections.d.ts.map +0 -1
  142. package/out/parser/semantic/intersections.js +0 -2
  143. package/out/parser/semantic/intersections.js.map +0 -1
  144. package/out/parser/semantic/semantic.d.ts +0 -33
  145. package/out/parser/semantic/semantic.d.ts.map +0 -1
  146. package/out/parser/semantic/semantic.js +0 -2
  147. package/out/parser/semantic/semantic.js.map +0 -1
  148. package/out/parser/semantic/utils.d.ts.map +0 -1
  149. package/out/parser/semantic/utils.js.map +0 -1
  150. package/out/parser/semantic/validate.d.ts.map +0 -1
  151. package/out/parser/semantic/validate.js.map +0 -1
  152. package/out/parser/shared.d.ts +0 -56
  153. package/out/parser/shared.d.ts.map +0 -1
  154. package/out/parser/shared.js +0 -55
  155. package/out/parser/shared.js.map +0 -1
  156. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  157. package/out/parser/string/reduce/dynamic.js.map +0 -1
  158. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  159. package/out/parser/string/reduce/shared.js.map +0 -1
  160. package/out/parser/string/reduce/static.d.ts.map +0 -1
  161. package/out/parser/string/reduce/static.js.map +0 -1
  162. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/date.js.map +0 -1
  164. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  166. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  168. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/operand.js.map +0 -1
  170. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  171. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  172. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  174. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  176. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  177. package/out/parser/string/shift/operator/operator.js.map +0 -1
  178. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  179. package/out/parser/string/shift/scanner.js.map +0 -1
  180. package/out/parser/string/string.d.ts.map +0 -1
  181. package/out/parser/string/string.js.map +0 -1
  182. package/out/parser/tuple.d.ts.map +0 -1
  183. package/out/parser/tuple.js.map +0 -1
  184. package/out/scope.d.ts.map +0 -1
  185. package/out/scope.js.map +0 -1
  186. package/out/test.d.ts +0 -2
  187. package/out/test.d.ts.map +0 -1
  188. package/out/test.js +0 -7
  189. package/out/test.js.map +0 -1
  190. package/out/tsconfig.build.tsbuildinfo +0 -1
  191. package/out/type.d.ts.map +0 -1
  192. package/out/type.js.map +0 -1
  193. package/out/util.d.ts +0 -19
  194. package/out/util.d.ts.map +0 -1
  195. package/out/util.js +0 -7
  196. package/out/util.js.map +0 -1
  197. package/parser/definition.ts +0 -159
  198. package/parser/generic.ts +0 -68
  199. package/parser/objectLiteral.ts +0 -197
  200. package/parser/semantic/bounds.ts +0 -61
  201. package/parser/semantic/divisor.ts +0 -13
  202. package/parser/semantic/intersections.ts +0 -46
  203. package/parser/semantic/semantic.ts +0 -143
  204. package/parser/semantic/utils.ts +0 -26
  205. package/parser/semantic/validate.ts +0 -109
  206. package/parser/shared.ts +0 -167
  207. package/parser/string/reduce/dynamic.ts +0 -196
  208. package/parser/string/reduce/shared.ts +0 -101
  209. package/parser/string/reduce/static.ts +0 -298
  210. package/parser/string/shift/operand/date.ts +0 -54
  211. package/parser/string/shift/operand/enclosed.ts +0 -118
  212. package/parser/string/shift/operand/genericArgs.ts +0 -127
  213. package/parser/string/shift/operand/operand.ts +0 -48
  214. package/parser/string/shift/operand/unenclosed.ts +0 -268
  215. package/parser/string/shift/operator/bounds.ts +0 -264
  216. package/parser/string/shift/operator/divisor.ts +0 -36
  217. package/parser/string/shift/operator/operator.ts +0 -82
  218. package/parser/string/shift/scanner.ts +0 -209
  219. package/parser/string/string.ts +0 -90
  220. package/parser/tuple.ts +0 -475
  221. package/scope.ts +0 -548
  222. package/type.ts +0 -349
  223. package/util.ts +0 -36
package/package.json CHANGED
@@ -1,40 +1,34 @@
1
1
  {
2
- "name": "arktype",
3
- "description": "TypeScript's 1:1 validator, optimized from editor to runtime",
4
- "version": "2.0.0-dev.2",
5
- "license": "MIT",
6
- "author": {
7
- "name": "David Blass",
8
- "email": "david@arktype.io",
9
- "url": "https://arktype.io"
10
- },
11
- "repository": {
12
- "type": "git",
13
- "url": "https://github.com/arktypeio/arktype.git"
14
- },
15
- "type": "module",
16
- "main": "./out/main.js",
17
- "types": "./out/main.d.ts",
18
- "exports": {
19
- ".": {
20
- "types": "./out/main.d.ts",
21
- "default": "./out/main.js"
22
- },
23
- "./internal/*": {
24
- "default": "./out/*"
25
- }
26
- },
27
- "files": [
28
- "out",
29
- "!__tests__",
30
- "**/*.ts"
31
- ],
32
- "scripts": {
33
- "build": "tsx ../repo/build.ts",
34
- "test": "tsx ../repo/testPackage.ts"
35
- },
36
- "dependencies": {
37
- "@arktype/util": "0.0.20",
38
- "@arktype/schema": "0.0.4"
39
- }
40
- }
2
+ "name": "arktype",
3
+ "description": "TypeScript's 1:1 validator, optimized from editor to runtime",
4
+ "version": "2.0.0-dev.21",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "David Blass",
8
+ "email": "david@arktype.io",
9
+ "url": "https://arktype.io"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/arktypeio/arktype.git"
14
+ },
15
+ "type": "module",
16
+ "main": "./out/api.js",
17
+ "types": "./out/api.d.ts",
18
+ "exports": {
19
+ ".": "./out/api.js",
20
+ "./config": "./out/config.js",
21
+ "./internal/*": "./out/*"
22
+ },
23
+ "files": [
24
+ "out"
25
+ ],
26
+ "dependencies": {
27
+ "@arktype/util": "0.0.48",
28
+ "@arktype/schema": "0.1.13"
29
+ },
30
+ "scripts": {
31
+ "build": "tsx ../repo/build.ts",
32
+ "test": "tsx ../repo/testPackage.ts"
33
+ }
34
+ }
@@ -1,310 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { scope, type } from "arktype"
3
- import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
4
- import { incompleteArrayTokenMessage } from "../parser/string/shift/operator/operator.js"
5
- import {
6
- multipleVariadicMesage,
7
- writeNonArrayRestMessage
8
- } from "../parser/tuple.js"
9
-
10
- describe("array", () => {
11
- describe("base", () => {
12
- it("allows and apply", () => {
13
- const t = type("string[]")
14
- attest<string[]>(t.infer)
15
- attest(t.allows([])).equals(true)
16
- attest(t([]).out).snap([])
17
- attest(t.allows(["foo", "bar"])).equals(true)
18
- attest(t(["foo", "bar"]).out).snap(["foo", "bar"])
19
- attest(t.allows(["foo", "bar", 5])).equals(false)
20
- attest(t(["foo", "bar", 5]).errors?.summary).snap(
21
- "Value at [2] must be a string (was number)"
22
- )
23
- attest(t.allows([5, "foo", "bar"])).equals(false)
24
- attest(t([5, "foo", "bar"]).errors?.summary).snap(
25
- "Value at [0] must be a string (was number)"
26
- )
27
- })
28
-
29
- it("nested", () => {
30
- const t = type("string[][]")
31
- attest<string[][]>(t.infer)
32
- attest(t.allows([])).equals(true)
33
- attest(t([]).out).snap([])
34
- attest(t.allows([["foo"]])).equals(true)
35
- attest(t([["foo"]]).out).snap([["foo"]])
36
- attest(t.allows(["foo"])).equals(false)
37
- attest(t(["foo"]).errors?.summary).snap(
38
- "Value at [0] must be an array (was string)"
39
- )
40
- attest(t.allows([["foo", 5]])).equals(false)
41
- attest(t([["foo", 5]]).errors?.summary).snap(
42
- "Value at [0][1] must be a string (was number)"
43
- )
44
- })
45
-
46
- it("tuple expression", () => {
47
- const t = type(["string", "[]"])
48
- attest<string[]>(t.infer)
49
- attest(t.json).equals(type("string[]").json)
50
- })
51
-
52
- describe("optional tuple literals", () => {
53
- it("string optional", () => {
54
- const t = type(["string?"])
55
- attest<[string?]>(t.infer)
56
- attest(t.json).equals(type(["string?"]).json)
57
- })
58
- it("optional tuple", () => {
59
- const t = type([["string", "?"]])
60
- attest<[string?]>(t.infer)
61
- attest(t.json).equals(type(["string?"]).json)
62
- })
63
- it("multi-optional tuple", () => {
64
- const t = type([["string?", "?"]])
65
- attest<[string?]>(t.infer)
66
- attest(t.json).equals(type(["string?"]).json)
67
- })
68
- it("nested optional tuple", () => {
69
- const t = type([["string?"], "string?"])
70
- attest<[[string?], string?]>(t.infer)
71
- })
72
- it("shallow optional string", () => {
73
- // @ts-expect-error
74
- attest(() => type("string?")).throwsAndHasTypeError(
75
- writeUnresolvableMessage("string?")
76
- )
77
- })
78
- it("shallow optional tuple", () => {
79
- // @ts-expect-error
80
- attest(() => type(["string", "?"])).throws(
81
- writeUnresolvableMessage("?")
82
- )
83
- })
84
- })
85
- it("root expression", () => {
86
- const t = type("string", "[]")
87
- attest<string[]>(t.infer)
88
- attest(t.json).equals(type("string[]").json)
89
- })
90
-
91
- it("chained", () => {
92
- const t = type({ a: "string" }).array()
93
- attest<{ a: string }[]>(t.infer)
94
-
95
- // @ts-expect-error
96
- attest(() => type({ a: "hmm" }).array()).throwsAndHasTypeError(
97
- writeUnresolvableMessage("hmm")
98
- )
99
- })
100
- it("incomplete token", () => {
101
- // @ts-expect-error
102
- attest(() => type("string[")).throwsAndHasTypeError(
103
- incompleteArrayTokenMessage
104
- )
105
- })
106
- })
107
- describe("non-variadic tuple", () => {
108
- it("shallow", () => {
109
- const t = type(["string", "number"])
110
- attest<[string, number]>(t.infer)
111
- attest(t.allows(["", 0])).equals(true)
112
- attest(t(["", 0]).out).snap()
113
- attest(t.allows([true, 0])).equals(false)
114
- attest(t([true, 0]).errors?.summary).snap()
115
- attest(t.allows([0, false])).equals(false)
116
- attest(t([0, false]).errors?.summary).snap()
117
- // too short
118
- attest(t.allows([""])).equals(false)
119
- attest(t([""]).errors?.summary).snap()
120
- // too long
121
- attest(t.allows(["", 0, 1])).equals(false)
122
- attest(t(["", 0, 1]).errors?.summary).snap()
123
- // non-array
124
- attest(
125
- t.allows({
126
- length: 2,
127
- 0: "",
128
- 1: 0
129
- })
130
- ).equals(false)
131
- attest(
132
- t({
133
- length: 2,
134
- 0: "",
135
- 1: 0
136
- }).errors?.summary
137
- ).snap()
138
- })
139
-
140
- it("nested", () => {
141
- const t = type([["string", "number"], [{ a: "boolean", b: ["null"] }]])
142
- attest<
143
- [
144
- [string, number],
145
- [
146
- {
147
- a: boolean
148
- b: [null]
149
- }
150
- ]
151
- ]
152
- >(t.infer)
153
-
154
- attest(t.allows([["", 0], [{ a: true, b: [null] }]])).equals(true)
155
- attest(t([["foo", 1], [{ a: true, b: [null] }]]).out).snap([
156
- ["foo", 1],
157
- [{ a: true, b: [null] }]
158
- ])
159
- attest(t.allows([["", 0], [{ a: true, b: [undefined] }]])).equals(false)
160
- attest(
161
- t([["foo", 1], [{ a: true, b: [undefined] }]]).errors?.summary
162
- ).snap("[1][0].b[0] must be null (was undefined)")
163
- })
164
- })
165
- describe("variadic tuple", () => {
166
- describe("variadic", () => {
167
- it("spreads simple arrays", () => {
168
- const wellRested = type(["string", "...number[]"])
169
- attest<[string, ...number[]]>(wellRested.infer)
170
- attest(wellRested(["foo"]).out).equals(["foo"])
171
- attest(wellRested(["foo", 1, 2]).out).equals(["foo", 1, 2])
172
- })
173
- it("tuple expression", () => {
174
- const wellRestedTuple = type([
175
- "number",
176
- ["...", [{ a: "string" }, "[]"]]
177
- ])
178
- attest<[number, ...{ a: string }[]]>(wellRestedTuple.infer)
179
- })
180
- it("spreads array expressions", () => {
181
- const greatSpread = type([{ a: "boolean" }, "...(Date|RegExp)[]"])
182
- attest<
183
- [
184
- {
185
- a: boolean
186
- },
187
- ...(RegExp | Date)[]
188
- ]
189
- >(greatSpread.infer)
190
- })
191
- it("allows array keyword", () => {
192
- const types = scope({
193
- myArrayKeyword: "boolean[]",
194
- myVariadicKeyword: ["string", "...myArrayKeyword"]
195
- }).export()
196
- attest<[string, ...boolean[]]>(types.myVariadicKeyword.infer)
197
- })
198
- it("errors on non-array", () => {
199
- attest(() =>
200
- // @ts-expect-error
201
- type(["email", "...symbol"])
202
- ).throwsAndHasTypeError(writeNonArrayRestMessage("symbol"))
203
- attest(() =>
204
- // @ts-expect-error
205
- type(["number", ["...", "string"]])
206
- ).throwsAndHasTypeError(writeNonArrayRestMessage("string"))
207
- })
208
- it("errors on non-last element", () => {
209
- attest(() =>
210
- // @ts-expect-error
211
- type(["...number[]", "string"])
212
- ).throwsAndHasTypeError(multipleVariadicMesage)
213
- attest(() =>
214
- // @ts-expect-error
215
- type([["...", "string[]"], "number"])
216
- ).throwsAndHasTypeError(multipleVariadicMesage)
217
- })
218
- })
219
- })
220
- describe("intersection", () => {
221
- it("shallow array intersection", () => {
222
- const actual = type("string[]&'foo'[]").json
223
- const expected = type("'foo'[]").json
224
- attest(actual).is(expected)
225
- })
226
- it("deep array intersection", () => {
227
- const actual = type([{ a: "string" }, "[]"]).and([
228
- { b: "number" },
229
- "[]"
230
- ]).json
231
- const expected = type([{ a: "string", b: "number" }, "[]"]).json
232
- attest(actual).is(expected)
233
- })
234
- it("tuple intersection", () => {
235
- const t = type([[{ a: "string" }], "&", [{ b: "boolean" }]])
236
- attest<
237
- [
238
- {
239
- a: string
240
- b: boolean
241
- }
242
- ]
243
- >(t.infer)
244
- })
245
- it("tuple and array", () => {
246
- const tupleAndArray = type([
247
- [{ a: "string" }],
248
- "&",
249
- [{ b: "boolean" }, "[]"]
250
- ])
251
- const arrayAndTuple = type([
252
- [{ b: "boolean" }, "[]"],
253
- "&",
254
- [{ a: "string" }]
255
- ])
256
- attest<
257
- [
258
- {
259
- a: string
260
- b: boolean
261
- }
262
- ]
263
- >(tupleAndArray.infer)
264
-
265
- attest<
266
- [
267
- {
268
- a: string
269
- b: boolean
270
- }
271
- ]
272
- >(arrayAndTuple.infer)
273
-
274
- const expected = type([{ a: "string", b: "boolean" }]).json
275
- attest(tupleAndArray.json).is(expected)
276
- attest(arrayAndTuple.json).is(expected)
277
- })
278
- it("variadic and tuple", () => {
279
- const b = type([{ b: "boolean" }, "[]"])
280
- const t = type([{ a: "string" }, ["...", b]]).and([
281
- { c: "number" },
282
- { d: "Date" }
283
- ])
284
- const expected = type([
285
- { a: "string", c: "number" },
286
- { b: "boolean", d: "Date" }
287
- ])
288
- attest(t.json).equals(expected.json)
289
- })
290
- it("variadic and array", () => {
291
- const b = type({ b: "boolean" }, "[]")
292
- const t = type([{ a: "string" }, ["...", b]]).and([{ c: "number" }, "[]"])
293
- const expected = type([
294
- { a: "string", c: "number" },
295
- ["...", [{ b: "boolean", c: "number" }, "[]"]]
296
- ])
297
- attest<typeof expected.infer>(t.infer)
298
- attest(t.json).equals(expected.json)
299
- })
300
- })
301
- // TODO: reenable
302
- // describe("traversal", () => {
303
- // it("multiple errors", () => {
304
- // const stringArray = type("string[]")
305
- // attest(stringArray([1, 2]).errors?.summary).snap(
306
- // "Item at index 0 must be a string (was number)\nItem at index 1 must be a string (was number)"
307
- // )
308
- // })
309
- // })
310
- })
@@ -1,17 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import type { astToString } from "../parser/semantic/utils.js"
3
-
4
- describe("astToString", () => {
5
- it("no parentheses if nested ast is an array", () => {
6
- const t = {} as unknown as astToString<[["number", "[]"], "|", "number"]>
7
- attest<"'number[]|number'">(t)
8
- })
9
- it("parentheses if nested ast is an infix expression", () => {
10
- const t = {} as unknown as astToString<[["0", "|", "1"], "|", "string"]>
11
- attest<"'(0|1)|string'">(t)
12
- })
13
- it("defaults to '...' if input is bad", () => {
14
- const t = {} as unknown as astToString<["0", "///", "1"]>
15
- attest<"'...'">(t)
16
- })
17
- })
@@ -1,51 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { type } from "arktype"
3
- import { writeBadDefinitionTypeMessage } from "../parser/definition.js"
4
-
5
- describe("definitions", () => {
6
- it("undefined", () => {
7
- // @ts-expect-error
8
- attest(() => type({ bad: undefined })).throwsAndHasTypeError(
9
- writeBadDefinitionTypeMessage("undefined")
10
- )
11
- })
12
- it("null", () => {
13
- // @ts-expect-error
14
- attest(() => type({ bad: null })).throwsAndHasTypeError(
15
- writeBadDefinitionTypeMessage("null")
16
- )
17
- })
18
- it("boolean", () => {
19
- // @ts-expect-error
20
- attest(() => type({ bad: true })).throwsAndHasTypeError(
21
- writeBadDefinitionTypeMessage("boolean")
22
- )
23
- })
24
- it("number", () => {
25
- // @ts-expect-error
26
- attest(() => type({ bad: 5 })).throwsAndHasTypeError(
27
- writeBadDefinitionTypeMessage("number")
28
- )
29
- })
30
- it("bigint", () => {
31
- // @ts-expect-error
32
- attest(() => type({ bad: 99999n })).throwsAndHasTypeError(
33
- writeBadDefinitionTypeMessage("bigint")
34
- )
35
- })
36
- it("symbol", () => {
37
- // @ts-expect-error
38
- attest(() => type({ bad: Symbol() })).throwsAndHasTypeError(
39
- writeBadDefinitionTypeMessage("symbol")
40
- )
41
- })
42
- it("any", () => {
43
- // doesn't error, so this test is just to ensure it doesn't infinitely recurse
44
- const t = type({ bad: {} as any })
45
- attest<{}>(t.infer)
46
- })
47
- it("unknown", () => {
48
- // @ts-expect-error just results in base completions, so we just check there's an error
49
- attest(() => type({ bad: {} as unknown })).type.errors("")
50
- })
51
- })
@@ -1,46 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { type } from "arktype"
3
- import { writeUnsatisfiableExpressionError } from "../parser/semantic/validate.js"
4
-
5
- describe("basis 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
- it("unsatisfiable class & literal", () => {
18
- const a = [0]
19
- const literal = type("===", a)
20
- const cls = type("instanceof", Date)
21
- attest(() => literal.and(cls)).throws(writeUnsatisfiableExpressionError(""))
22
- attest(() => cls.and(literal)).throws(writeUnsatisfiableExpressionError(""))
23
- })
24
- it("domain & literal", () => {
25
- const literal = type("'foo'")
26
- const domain = type("string")
27
- attest(literal.and(domain).json).equals(literal.json)
28
- attest(domain.and(literal).json).equals(literal.json)
29
- })
30
- it("unsatisfiable domain & literal", () => {
31
- const literal = type("'foo'")
32
- const domain = type("number")
33
- attest(() => literal.and(domain)).throws(
34
- writeUnsatisfiableExpressionError("")
35
- )
36
- attest(() => domain.and(literal)).throws(
37
- writeUnsatisfiableExpressionError("")
38
- )
39
- })
40
- it("domain & class", () => {
41
- const domain = type("object")
42
- const cls = type("instanceof", Date)
43
- attest(domain.and(cls).json).equals(cls.json)
44
- attest(cls.and(domain).json).equals(cls.json)
45
- })
46
- })