arktype 1.1.0-dev-2 → 2.0.0-dev.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 (296) hide show
  1. package/__tests__/array.test.ts +272 -0
  2. package/__tests__/astUtils.test.ts +17 -0
  3. package/__tests__/badDefinitionType.test.ts +51 -0
  4. package/__tests__/basis.test.ts +46 -0
  5. package/__tests__/bounds.test.ts +388 -0
  6. package/__tests__/cast.test.ts +32 -0
  7. package/__tests__/completions.test.ts +46 -0
  8. package/__tests__/config.test.ts +168 -0
  9. package/__tests__/cyclic.test.ts +144 -0
  10. package/__tests__/dateLiteral.test.ts +32 -0
  11. package/__tests__/declared.test.ts +123 -0
  12. package/__tests__/discrimination.test.ts +74 -0
  13. package/__tests__/divisor.test.ts +90 -0
  14. package/__tests__/enclosed.test.ts +93 -0
  15. package/__tests__/examples.test.ts +56 -0
  16. package/__tests__/expressions.test.ts +138 -0
  17. package/__tests__/generateBenchData.ts +89 -0
  18. package/__tests__/generated/cyclic.ts +3058 -0
  19. package/__tests__/generic.test.ts +294 -0
  20. package/__tests__/group.test.ts +72 -0
  21. package/__tests__/hkt.test.ts +22 -0
  22. package/__tests__/imports.test.ts +113 -0
  23. package/__tests__/instanceof.test.ts +102 -0
  24. package/__tests__/intersection.test.ts +104 -0
  25. package/__tests__/jsdoc.test.ts +64 -0
  26. package/__tests__/keyTraversal.test.ts +73 -0
  27. package/__tests__/keyof.test.ts +134 -0
  28. package/__tests__/keywords.test.ts +188 -0
  29. package/__tests__/literal.test.ts +51 -0
  30. package/__tests__/match.test.ts +34 -0
  31. package/__tests__/methods.bench.ts +22 -0
  32. package/__tests__/morph.test.ts +409 -0
  33. package/__tests__/narrow.test.ts +102 -0
  34. package/__tests__/nodeDefinitions.test.ts +190 -0
  35. package/__tests__/object.bench.ts +28 -0
  36. package/__tests__/objectLiteral.test.ts +300 -0
  37. package/__tests__/operand.bench.ts +45 -0
  38. package/__tests__/operator.bench.ts +130 -0
  39. package/__tests__/regex.test.ts +58 -0
  40. package/__tests__/scope.bench.ts +39 -0
  41. package/__tests__/scope.test.ts +140 -0
  42. package/__tests__/string.test.ts +48 -0
  43. package/__tests__/submodule.test.ts +99 -0
  44. package/__tests__/this.test.ts +57 -0
  45. package/__tests__/thunk.test.ts +80 -0
  46. package/__tests__/traverse.test.ts +119 -0
  47. package/__tests__/tsGenerics.test.ts +12 -0
  48. package/__tests__/typeMethods.test.ts +69 -0
  49. package/__tests__/typeReference.test.ts +39 -0
  50. package/__tests__/unenclosed.test.ts +120 -0
  51. package/__tests__/union.test.ts +147 -0
  52. package/ark.ts +91 -0
  53. package/main.ts +7 -9
  54. package/match.ts +47 -68
  55. package/out/ark.d.ts +52 -0
  56. package/out/ark.d.ts.map +1 -0
  57. package/out/ark.js +19 -0
  58. package/out/ark.js.map +1 -0
  59. package/out/arkConfig.d.ts +2 -0
  60. package/out/arkConfig.d.ts.map +1 -0
  61. package/out/arkConfig.js +10 -0
  62. package/out/arkConfig.js.map +1 -0
  63. package/out/main.d.ts +7 -0
  64. package/out/main.d.ts.map +1 -0
  65. package/out/main.js +4 -0
  66. package/out/main.js.map +1 -0
  67. package/out/match.d.ts +48 -0
  68. package/out/match.d.ts.map +1 -0
  69. package/{dist → out}/match.js +0 -5
  70. package/out/match.js.map +1 -0
  71. package/{dist → out}/parser/definition.d.ts +3 -3
  72. package/out/parser/definition.d.ts.map +1 -0
  73. package/{dist → out}/parser/definition.js +8 -5
  74. package/out/parser/definition.js.map +1 -0
  75. package/out/parser/generic.d.ts +18 -0
  76. package/out/parser/generic.d.ts.map +1 -0
  77. package/out/parser/generic.js +19 -0
  78. package/out/parser/generic.js.map +1 -0
  79. package/{dist → out}/parser/objectLiteral.d.ts +14 -5
  80. package/out/parser/objectLiteral.d.ts.map +1 -0
  81. package/out/parser/objectLiteral.js +66 -0
  82. package/out/parser/objectLiteral.js.map +1 -0
  83. package/out/parser/semantic/bounds.d.ts +13 -0
  84. package/out/parser/semantic/bounds.d.ts.map +1 -0
  85. package/{dist → out}/parser/semantic/bounds.js.map +1 -1
  86. package/{dist → out}/parser/semantic/divisor.d.ts +2 -2
  87. package/{dist → out}/parser/semantic/divisor.d.ts.map +1 -1
  88. package/{dist → out}/parser/semantic/intersections.d.ts +1 -2
  89. package/out/parser/semantic/intersections.d.ts.map +1 -0
  90. package/out/parser/semantic/semantic.d.ts +33 -0
  91. package/out/parser/semantic/semantic.d.ts.map +1 -0
  92. package/{dist → out}/parser/semantic/utils.d.ts +1 -1
  93. package/{dist → out}/parser/semantic/utils.d.ts.map +1 -1
  94. package/{dist → out}/parser/semantic/validate.d.ts +3 -3
  95. package/out/parser/semantic/validate.d.ts.map +1 -0
  96. package/{dist → out}/parser/semantic/validate.js.map +1 -1
  97. package/{dist → out}/parser/shared.d.ts +7 -1
  98. package/out/parser/shared.d.ts.map +1 -0
  99. package/{dist → out}/parser/shared.js +7 -1
  100. package/out/parser/shared.js.map +1 -0
  101. package/{dist → out}/parser/string/reduce/dynamic.d.ts +11 -9
  102. package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
  103. package/{dist → out}/parser/string/reduce/dynamic.js +13 -2
  104. package/out/parser/string/reduce/dynamic.js.map +1 -0
  105. package/{dist → out}/parser/string/reduce/shared.d.ts +29 -2
  106. package/{dist → out}/parser/string/reduce/shared.d.ts.map +1 -1
  107. package/{dist → out}/parser/string/reduce/shared.js +20 -1
  108. package/out/parser/string/reduce/shared.js.map +1 -0
  109. package/{dist → out}/parser/string/reduce/static.d.ts +1 -2
  110. package/out/parser/string/reduce/static.d.ts.map +1 -0
  111. package/{dist → out}/parser/string/shift/operand/date.d.ts +3 -3
  112. package/out/parser/string/shift/operand/date.d.ts.map +1 -0
  113. package/{dist → out}/parser/string/shift/operand/date.js.map +1 -1
  114. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +2 -2
  115. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts.map +1 -1
  116. package/{dist → out}/parser/string/shift/operand/enclosed.js +4 -4
  117. package/out/parser/string/shift/operand/enclosed.js.map +1 -0
  118. package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
  119. package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
  120. package/out/parser/string/shift/operand/genericArgs.js +25 -0
  121. package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
  122. package/{dist → out}/parser/string/shift/operand/operand.d.ts.map +1 -1
  123. package/{dist → out}/parser/string/shift/operand/operand.js.map +1 -1
  124. package/{dist → out}/parser/string/shift/operand/unenclosed.d.ts +7 -9
  125. package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
  126. package/{dist → out}/parser/string/shift/operand/unenclosed.js +10 -10
  127. package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
  128. package/{dist → out}/parser/string/shift/operator/bounds.d.ts +11 -33
  129. package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
  130. package/out/parser/string/shift/operator/bounds.js +106 -0
  131. package/out/parser/string/shift/operator/bounds.js.map +1 -0
  132. package/{dist → out}/parser/string/shift/operator/divisor.js.map +1 -1
  133. package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
  134. package/{dist → out}/parser/string/shift/operator/operator.js.map +1 -1
  135. package/{dist → out}/parser/string/shift/scanner.d.ts +1 -1
  136. package/{dist → out}/parser/string/shift/scanner.d.ts.map +1 -1
  137. package/out/parser/string/shift/scanner.js.map +1 -0
  138. package/{dist → out}/parser/string/string.d.ts +9 -11
  139. package/out/parser/string/string.d.ts.map +1 -0
  140. package/out/parser/string/string.js +23 -0
  141. package/out/parser/string/string.js.map +1 -0
  142. package/{dist → out}/parser/tuple.d.ts +9 -11
  143. package/out/parser/tuple.d.ts.map +1 -0
  144. package/{dist → out}/parser/tuple.js +17 -14
  145. package/out/parser/tuple.js.map +1 -0
  146. package/{dist → out}/scope.d.ts +16 -16
  147. package/out/scope.d.ts.map +1 -0
  148. package/{dist → out}/scope.js +27 -22
  149. package/out/scope.js.map +1 -0
  150. package/out/test.d.ts +2 -0
  151. package/out/test.d.ts.map +1 -0
  152. package/out/test.js +7 -0
  153. package/out/test.js.map +1 -0
  154. package/out/tsconfig.build.tsbuildinfo +1 -0
  155. package/{dist → out}/type.d.ts +23 -47
  156. package/out/type.d.ts.map +1 -0
  157. package/{dist → out}/type.js +27 -31
  158. package/out/type.js.map +1 -0
  159. package/out/util.d.ts +19 -0
  160. package/out/util.d.ts.map +1 -0
  161. package/out/util.js +7 -0
  162. package/out/util.js.map +1 -0
  163. package/package.json +13 -15
  164. package/parser/definition.ts +58 -55
  165. package/parser/generic.ts +17 -157
  166. package/parser/objectLiteral.ts +107 -30
  167. package/parser/semantic/bounds.ts +22 -24
  168. package/parser/semantic/divisor.ts +2 -2
  169. package/parser/semantic/intersections.ts +17 -18
  170. package/parser/semantic/semantic.ts +80 -43
  171. package/parser/semantic/utils.ts +7 -7
  172. package/parser/semantic/validate.ts +40 -36
  173. package/parser/shared.ts +28 -17
  174. package/parser/string/reduce/dynamic.ts +26 -11
  175. package/parser/string/reduce/shared.ts +35 -7
  176. package/parser/string/reduce/static.ts +20 -22
  177. package/parser/string/shift/operand/date.ts +1 -4
  178. package/parser/string/shift/operand/enclosed.ts +5 -6
  179. package/parser/string/shift/operand/genericArgs.ts +127 -0
  180. package/parser/string/shift/operand/operand.ts +21 -21
  181. package/parser/string/shift/operand/unenclosed.ts +54 -68
  182. package/parser/string/shift/operator/bounds.ts +99 -64
  183. package/parser/string/shift/operator/operator.ts +31 -31
  184. package/parser/string/shift/scanner.ts +7 -7
  185. package/parser/string/string.ts +29 -32
  186. package/parser/tuple.ts +87 -88
  187. package/scope.ts +81 -80
  188. package/type.ts +76 -132
  189. package/util.ts +36 -0
  190. package/dist/main.d.ts +0 -11
  191. package/dist/main.d.ts.map +0 -1
  192. package/dist/main.js +0 -8
  193. package/dist/main.js.map +0 -1
  194. package/dist/match.d.ts +0 -46
  195. package/dist/match.d.ts.map +0 -1
  196. package/dist/match.js.map +0 -1
  197. package/dist/parser/definition.d.ts.map +0 -1
  198. package/dist/parser/definition.js.map +0 -1
  199. package/dist/parser/generic.d.ts +0 -44
  200. package/dist/parser/generic.d.ts.map +0 -1
  201. package/dist/parser/generic.js +0 -46
  202. package/dist/parser/generic.js.map +0 -1
  203. package/dist/parser/objectLiteral.d.ts.map +0 -1
  204. package/dist/parser/objectLiteral.js +0 -38
  205. package/dist/parser/objectLiteral.js.map +0 -1
  206. package/dist/parser/semantic/bounds.d.ts +0 -13
  207. package/dist/parser/semantic/bounds.d.ts.map +0 -1
  208. package/dist/parser/semantic/intersections.d.ts.map +0 -1
  209. package/dist/parser/semantic/semantic.d.ts +0 -22
  210. package/dist/parser/semantic/semantic.d.ts.map +0 -1
  211. package/dist/parser/semantic/validate.d.ts.map +0 -1
  212. package/dist/parser/shared.d.ts.map +0 -1
  213. package/dist/parser/shared.js.map +0 -1
  214. package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
  215. package/dist/parser/string/reduce/dynamic.js.map +0 -1
  216. package/dist/parser/string/reduce/shared.js.map +0 -1
  217. package/dist/parser/string/reduce/static.d.ts.map +0 -1
  218. package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
  219. package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
  220. package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  221. package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
  222. package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
  223. package/dist/parser/string/shift/operator/bounds.js +0 -95
  224. package/dist/parser/string/shift/operator/bounds.js.map +0 -1
  225. package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
  226. package/dist/parser/string/shift/scanner.js.map +0 -1
  227. package/dist/parser/string/string.d.ts.map +0 -1
  228. package/dist/parser/string/string.js +0 -31
  229. package/dist/parser/string/string.js.map +0 -1
  230. package/dist/parser/tuple.d.ts.map +0 -1
  231. package/dist/parser/tuple.js.map +0 -1
  232. package/dist/scope.d.ts.map +0 -1
  233. package/dist/scope.js.map +0 -1
  234. package/dist/scopes/ark.d.ts +0 -38
  235. package/dist/scopes/ark.d.ts.map +0 -1
  236. package/dist/scopes/ark.js +0 -24
  237. package/dist/scopes/ark.js.map +0 -1
  238. package/dist/scopes/jsObjects.d.ts +0 -19
  239. package/dist/scopes/jsObjects.d.ts.map +0 -1
  240. package/dist/scopes/jsObjects.js +0 -15
  241. package/dist/scopes/jsObjects.js.map +0 -1
  242. package/dist/scopes/parsing.d.ts +0 -18
  243. package/dist/scopes/parsing.d.ts.map +0 -1
  244. package/dist/scopes/parsing.js +0 -61
  245. package/dist/scopes/parsing.js.map +0 -1
  246. package/dist/scopes/tsGenerics.d.ts +0 -17
  247. package/dist/scopes/tsGenerics.d.ts.map +0 -1
  248. package/dist/scopes/tsGenerics.js +0 -13
  249. package/dist/scopes/tsGenerics.js.map +0 -1
  250. package/dist/scopes/tsKeywords.d.ts +0 -24
  251. package/dist/scopes/tsKeywords.d.ts.map +0 -1
  252. package/dist/scopes/tsKeywords.js +0 -20
  253. package/dist/scopes/tsKeywords.js.map +0 -1
  254. package/dist/scopes/utils/creditCard.d.ts +0 -3
  255. package/dist/scopes/utils/creditCard.d.ts.map +0 -1
  256. package/dist/scopes/utils/creditCard.js +0 -36
  257. package/dist/scopes/utils/creditCard.js.map +0 -1
  258. package/dist/scopes/utils/date.d.ts +0 -24
  259. package/dist/scopes/utils/date.d.ts.map +0 -1
  260. package/dist/scopes/utils/date.js +0 -53
  261. package/dist/scopes/utils/date.js.map +0 -1
  262. package/dist/scopes/validation.d.ts +0 -20
  263. package/dist/scopes/validation.d.ts.map +0 -1
  264. package/dist/scopes/validation.js +0 -57
  265. package/dist/scopes/validation.js.map +0 -1
  266. package/dist/type.d.ts.map +0 -1
  267. package/dist/type.js.map +0 -1
  268. package/scopes/ark.ts +0 -64
  269. package/scopes/jsObjects.ts +0 -29
  270. package/scopes/parsing.ts +0 -81
  271. package/scopes/tsGenerics.ts +0 -28
  272. package/scopes/tsKeywords.ts +0 -39
  273. package/scopes/utils/creditCard.ts +0 -37
  274. package/scopes/utils/date.ts +0 -108
  275. package/scopes/validation.ts +0 -81
  276. /package/{dist → out}/parser/semantic/bounds.js +0 -0
  277. /package/{dist → out}/parser/semantic/divisor.js +0 -0
  278. /package/{dist → out}/parser/semantic/divisor.js.map +0 -0
  279. /package/{dist → out}/parser/semantic/intersections.js +0 -0
  280. /package/{dist → out}/parser/semantic/intersections.js.map +0 -0
  281. /package/{dist → out}/parser/semantic/semantic.js +0 -0
  282. /package/{dist → out}/parser/semantic/semantic.js.map +0 -0
  283. /package/{dist → out}/parser/semantic/utils.js +0 -0
  284. /package/{dist → out}/parser/semantic/utils.js.map +0 -0
  285. /package/{dist → out}/parser/semantic/validate.js +0 -0
  286. /package/{dist → out}/parser/string/reduce/static.js +0 -0
  287. /package/{dist → out}/parser/string/reduce/static.js.map +0 -0
  288. /package/{dist → out}/parser/string/shift/operand/date.js +0 -0
  289. /package/{dist → out}/parser/string/shift/operand/operand.d.ts +0 -0
  290. /package/{dist → out}/parser/string/shift/operand/operand.js +0 -0
  291. /package/{dist → out}/parser/string/shift/operator/divisor.d.ts +0 -0
  292. /package/{dist → out}/parser/string/shift/operator/divisor.d.ts.map +0 -0
  293. /package/{dist → out}/parser/string/shift/operator/divisor.js +0 -0
  294. /package/{dist → out}/parser/string/shift/operator/operator.d.ts +0 -0
  295. /package/{dist → out}/parser/string/shift/operator/operator.js +0 -0
  296. /package/{dist → out}/parser/string/shift/scanner.js +0 -0
@@ -0,0 +1,140 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { scope, type } from "arktype"
3
+ import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
4
+ import { writeUnboundableMessage } from "../parser/string/shift/operator/bounds.js"
5
+ import { writeUnexpectedCharacterMessage } from "../parser/string/shift/operator/operator.js"
6
+
7
+ describe("scope", () => {
8
+ it("base definition", () => {
9
+ const types = scope({ a: "string" }).export()
10
+ attest<string>(types.a.infer)
11
+ attest(() =>
12
+ // @ts-expect-error
13
+ scope({ a: "strong" }).export()
14
+ ).throwsAndHasTypeError(writeUnresolvableMessage("strong"))
15
+ })
16
+ it("type definition", () => {
17
+ const types = scope({ a: type("string") }).export()
18
+ attest<string>(types.a.infer)
19
+ attest(() =>
20
+ // @ts-expect-error
21
+ scope({ a: type("strong") })
22
+ ).throwsAndHasTypeError(writeUnresolvableMessage("strong"))
23
+ })
24
+ it("interdependent", () => {
25
+ const types = scope({
26
+ a: "string>5",
27
+ b: "email<=10",
28
+ c: "a&b"
29
+ }).export()
30
+ attest<string>(types.c.infer)
31
+ })
32
+ it("object array", () => {
33
+ const types = scope({ a: "string", b: [{ c: "a" }] }).export()
34
+ attest<
35
+ [
36
+ {
37
+ c: string
38
+ }
39
+ ]
40
+ >(types.b.infer)
41
+ })
42
+ it("doesn't try to validate any in scope", () => {
43
+ // const $ = scope({ a: {} as any })
44
+ // attest<{ a: never }>($.infer)
45
+ // attest<[number, never]>($.type(["number", "a"]).infer)
46
+ })
47
+ it("infers input and output", () => {
48
+ const $ = scope({
49
+ a: ["string", "=>", (s) => s.length]
50
+ })
51
+ attest<{ a: number }>($.infer)
52
+ // TODO: API?
53
+ // attest<{ a: string }>($.in.infer)
54
+ })
55
+ it("scope.scope", () => {
56
+ const $ = scope({
57
+ a: "string"
58
+ })
59
+ const importer = $.scope({ b: "a[]" })
60
+ attest<{ b: string[] }>(importer.infer)
61
+ const t = importer.type("b")
62
+ attest(t.json).is(type("string[]").json)
63
+ })
64
+ it("infers its own helpers", () => {
65
+ const $ = scope({
66
+ a: () => $.type("string"),
67
+ b: () => $.type("number")
68
+ })
69
+ const types = $.export()
70
+ attest<string>(types.a.infer)
71
+ attest<number>(types.b.infer)
72
+ })
73
+ it("allows semantically valid helpers", () => {
74
+ const $ = scope({
75
+ n: () => $.type("number"),
76
+ lessThan10: () => $.type("n<10")
77
+ })
78
+ const types = $.export()
79
+ attest<number>(types.n.infer)
80
+ attest<number>(types.lessThan10.infer)
81
+ })
82
+ it("errors on helper parse error", () => {
83
+ attest(() => {
84
+ const $ = scope({
85
+ // @ts-expect-error
86
+ a: () => $.type("kung|foo")
87
+ })
88
+ $.export()
89
+ }).throwsAndHasTypeError(writeUnresolvableMessage("kung"))
90
+ })
91
+ it("errors on semantically invalid helper", () => {
92
+ attest(() => {
93
+ const $ = scope({
94
+ b: () => $.type("boolean"),
95
+ // @ts-expect-error
96
+ lessThan10: () => $.type("b<10")
97
+ })
98
+ $.export()
99
+ }).throwsAndHasTypeError(writeUnboundableMessage("'b'"))
100
+ })
101
+ it("errors on ridiculous unexpected alias scenario", () => {
102
+ attest(() =>
103
+ scope({
104
+ Unexpected: {},
105
+ User: {
106
+ // Previously, using the alias `Unexpected` allowed creating
107
+ // this type string which matched its own error message.
108
+ // @ts-expect-error
109
+ name: "Unexpected character 'c'"
110
+ }
111
+ }).export()
112
+ ).throwsAndHasTypeError(writeUnexpectedCharacterMessage("c"))
113
+ })
114
+ it("autocompletion", () => {
115
+ attest(() => {
116
+ scope({
117
+ foobar: "string",
118
+ // @ts-expect-error
119
+ baz: "fo"
120
+ }).export()
121
+ }).type.errors(`Type '"fo"' is not assignable to type '"foobar"'`)
122
+ })
123
+ it("cross-scope reference", () => {
124
+ const { Apple } = scope({
125
+ Apple: {
126
+ pear: "Pear"
127
+ },
128
+ Pear: {
129
+ tasty: "true"
130
+ }
131
+ }).export()
132
+
133
+ const { X } = scope({
134
+ X: Apple
135
+ }).export()
136
+
137
+ const { out: data } = X({ pear: { tasty: true } })
138
+ attest(data).snap({ pear: { tasty: true } })
139
+ })
140
+ })
@@ -0,0 +1,48 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { type } from "arktype"
3
+ import { writeUnterminatedEnclosedMessage } from "../parser/string/shift/operand/enclosed.js"
4
+ import {
5
+ writeExpressionExpectedMessage,
6
+ writeUnresolvableMessage
7
+ } from "../parser/string/shift/operand/unenclosed.js"
8
+
9
+ describe("string", () => {
10
+ it("errors on empty string", () => {
11
+ // @ts-expect-error
12
+ attest(() => type("")).throws(writeExpressionExpectedMessage(""))
13
+ })
14
+ it("ignores whitespace between identifiers/operators", () => {
15
+ const t = type(` \n string |
16
+ boolean [] `)
17
+ attest<string | boolean[]>(t.infer)
18
+ attest(t.json).equals(type("string|boolean[]").json)
19
+ })
20
+ it("errors on bad whitespace", () => {
21
+ attest(() =>
22
+ // @ts-expect-error
23
+ type("string | boo lean[]")
24
+ )
25
+ .throws(writeUnresolvableMessage("boo"))
26
+ .type.errors("string | boolean")
27
+ })
28
+ it("unterminated string", () => {
29
+ // @ts-expect-error
30
+ attest(() => type("'bob")).throwsAndHasTypeError(
31
+ writeUnterminatedEnclosedMessage("bob", "'")
32
+ )
33
+ })
34
+ it("shallow single autocomplete", () => {
35
+ // @ts-expect-error
36
+ attest(() => type("str")).type.errors(
37
+ `Argument of type '"str"' is not assignable to parameter of type '"string"'`
38
+ )
39
+ })
40
+ it("shallow multi autocomplete", () => {
41
+ // @ts-expect-error
42
+ attest(() => type("s")).type.errors(`"string" | "symbol" | "semver"`)
43
+ })
44
+ it("post-operator autocomplete", () => {
45
+ // @ts-expect-error
46
+ attest(() => type("string|num")).type.errors(`"string|number"`)
47
+ })
48
+ })
@@ -0,0 +1,99 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { lazily } from "@arktype/util"
3
+ import { scope, type, type Ark, type Module, type Scope } from "arktype"
4
+ import {
5
+ writeMissingSubmoduleAccessMessage,
6
+ writeNonSubmoduleDotMessage,
7
+ writeUnresolvableMessage
8
+ } from "../parser/string/shift/operand/unenclosed.js"
9
+
10
+ const $ = lazily(() =>
11
+ scope({
12
+ a: "string",
13
+ b: "sub.alias",
14
+ sub: scope({ alias: "number" }).export()
15
+ })
16
+ )
17
+
18
+ describe("submodules", () => {
19
+ it("base", () => {
20
+ const types = $.export()
21
+ attest<
22
+ Module<{
23
+ exports: {
24
+ a: string
25
+ b: number
26
+ sub: Module<{
27
+ exports: {
28
+ alias: number
29
+ }
30
+ locals: {}
31
+ ambient: Ark
32
+ }>
33
+ }
34
+ locals: {}
35
+ ambient: Ark
36
+ }>
37
+ >(types)
38
+
39
+ attest<number>(types.sub.alias.infer)
40
+ const expected = type("number").json
41
+ attest(types.sub.alias.json).is(expected)
42
+ attest(types.b.json).is(expected)
43
+ })
44
+ it("non-submodule dot access", () => {
45
+ // @ts-expect-error
46
+ attest(() => $.type("b.foo")).throwsAndHasTypeError(
47
+ writeNonSubmoduleDotMessage("b")
48
+ )
49
+ })
50
+ it("thunk submodule", () => {
51
+ const $ = scope({
52
+ a: "string",
53
+ c: "a",
54
+ sub: () =>
55
+ $.scope({
56
+ foo: "a",
57
+ bar: "foo"
58
+ }).export()
59
+ })
60
+ attest<
61
+ Scope<{
62
+ exports: {
63
+ a: string
64
+ c: string
65
+ sub: Module<{
66
+ exports: {
67
+ foo: string
68
+ bar: string
69
+ }
70
+ locals: {}
71
+ ambient: Ark
72
+ }>
73
+ }
74
+ locals: {}
75
+ ambient: Ark
76
+ }>
77
+ >($)
78
+ })
79
+ it("no alias reference", () => {
80
+ // @ts-expect-error
81
+ attest(() => $.type("sub")).throwsAndHasTypeError(
82
+ writeMissingSubmoduleAccessMessage("sub")
83
+ )
84
+ })
85
+ it("bad alias reference", () => {
86
+ // @ts-expect-error
87
+ attest(() => $.type("sub.marine")).throwsAndHasTypeError(
88
+ writeUnresolvableMessage("sub.marine")
89
+ )
90
+ })
91
+ it("autocompletion", () => {
92
+ const base = scope({ foo: "true" })
93
+ // @ts-expect-error
94
+ attest(() => scope({ base, reference: "base." }).export())
95
+ .throws(writeUnresolvableMessage("base."))
96
+ .type.errors("base.foo")
97
+ })
98
+ // TODO: private aliases
99
+ })
@@ -0,0 +1,57 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { scope, type } from "arktype"
3
+ import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
4
+
5
+ describe("this reference", () => {
6
+ it("resolves from type", () => {
7
+ const disappointingGift = type({
8
+ label: "string",
9
+ "box?": "this"
10
+ })
11
+
12
+ type ExpectedDisappointingGift = {
13
+ label: string
14
+ box?: ExpectedDisappointingGift
15
+ }
16
+ attest<ExpectedDisappointingGift>(disappointingGift.infer)
17
+ })
18
+
19
+ it("doesn't change when rereferenced", () => {
20
+ const initial = type({
21
+ initial: "this"
22
+ })
23
+
24
+ const reference = type({
25
+ reference: initial
26
+ })
27
+ type Initial = {
28
+ initial: Initial
29
+ }
30
+ type Expected = {
31
+ reference: Initial
32
+ }
33
+
34
+ attest<Expected>(reference.infer)
35
+ const types = scope({
36
+ initial: {
37
+ initial: "initial"
38
+ },
39
+ reference: {
40
+ reference: "initial"
41
+ }
42
+ }).export()
43
+ attest(reference.json).equals(types.reference.json)
44
+ })
45
+
46
+ it("unresolvable in scope", () => {
47
+ attest(() =>
48
+ scope({
49
+ disappointingGift: {
50
+ label: "string",
51
+ // @ts-expect-error
52
+ "box?": "this"
53
+ }
54
+ }).export()
55
+ ).throwsAndHasTypeError(writeUnresolvableMessage("this"))
56
+ })
57
+ })
@@ -0,0 +1,80 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { scope, type } from "arktype"
3
+ import { writeBadDefinitionTypeMessage } from "../parser/definition.js"
4
+ import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
5
+
6
+ describe("thunk", () => {
7
+ it("thunk", () => {
8
+ const t = type(() => type("boolean"))
9
+ attest<boolean>(t.infer)
10
+ attest(() => {
11
+ // @ts-expect-error
12
+ type(() => type("moolean"))
13
+ }).throwsAndHasTypeError(writeUnresolvableMessage("moolean"))
14
+ })
15
+ it("thunks in scope", () => {
16
+ const $ = scope({
17
+ a: () => $.type({ b: "b" }),
18
+ b: () => $.type({ a: "string" })
19
+ })
20
+ attest<{
21
+ a: {
22
+ b: {
23
+ a: string
24
+ }
25
+ }
26
+ b: {
27
+ a: string
28
+ }
29
+ }>($.infer)
30
+
31
+ const types = $.export()
32
+ attest<{
33
+ b: {
34
+ a: string
35
+ }
36
+ }>(types.a.infer)
37
+
38
+ // attest(types.a.node).snap({ object: { props: { b: "b" } } })
39
+ attest<{ a: string }>(types.b.infer)
40
+
41
+ // attest(types.b.node).snap({
42
+ // object: { props: { a: { object: { props: { a: "string" } } } } }
43
+ // })
44
+ })
45
+ it("expression from thunk", () => {
46
+ const $ = scope({
47
+ a: () => $.type({ a: "string" }),
48
+ b: { b: "boolean" },
49
+ aAndB: () => $.type("a&b")
50
+ })
51
+ const types = $.export()
52
+ attest<{ a: string; b: boolean }>(types.aAndB.infer)
53
+ // attest(types.aAndB.node).snap({
54
+ // object: { props: { a: "string", b: "boolean" } }
55
+ // })
56
+ })
57
+ it("shallow thunk in type", () => {
58
+ const t = type(() => type("string"))
59
+ attest(t.json).equals(type("string").json)
60
+ attest<string>(t.infer)
61
+ })
62
+ it("deep thunk in type", () => {
63
+ const t = type({ a: () => type("string") })
64
+ attest(t.json).equals(type({ a: "string" }).json)
65
+ attest<{ a: string }>(t.infer)
66
+ })
67
+ it("non-type thunk in scope", () => {
68
+ const $ = scope({
69
+ a: () => 42
70
+ })
71
+ attest(() => $.export()).throws(writeBadDefinitionTypeMessage("number"))
72
+ })
73
+ it("parse error in thunk in scope", () => {
74
+ const $ = scope({
75
+ // @ts-expect-error
76
+ a: () => $.type("bad")
77
+ })
78
+ attest(() => $.export()).throws(writeUnresolvableMessage("bad"))
79
+ })
80
+ })
@@ -0,0 +1,119 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { scope, type } from "arktype"
3
+
4
+ describe("traverse", () => {
5
+ it("divisible", () => {
6
+ const t = type("number%2")
7
+ attest(t(4).out).snap(4)
8
+ attest(t(5).errors?.summary).snap("Must be a multiple of 2 (was 5)")
9
+ })
10
+ it("range", () => {
11
+ const t = type("number>2")
12
+ attest(t(3).out).snap(3)
13
+ attest(t(2).errors?.summary).snap("Must be more than 2 (was 2)")
14
+ })
15
+ it("domain", () => {
16
+ const t = type("number")
17
+ attest(t(5).out).snap(5)
18
+ attest(t("foo").errors?.summary).snap("Must be a number (was string)")
19
+ })
20
+ it("regex", () => {
21
+ const t = type("/.*@arktype.io/")
22
+ attest(t("shawn@arktype.io").out).snap("shawn@arktype.io")
23
+ attest(t("shawn@hotmail.com").errors?.summary).snap(
24
+ "Must be a string matching /.*@arktype.io/ (was 'shawn@hotmail.com')"
25
+ )
26
+ })
27
+ it("required keys", () => {
28
+ const t = type({
29
+ name: "string",
30
+ age: "number",
31
+ "title?": "string"
32
+ })
33
+ attest(t({ name: "Shawn", age: 99 }).out).snap({
34
+ name: "Shawn",
35
+ age: 99
36
+ })
37
+ attest(t({ name: "Shawn" }).errors?.summary).snap("age must be defined")
38
+ })
39
+ it("customized builtin problem", () => {
40
+ const types = scope(
41
+ { isEven: "number%2" }
42
+ //{
43
+ // TODO: Fix
44
+ // codes: {
45
+ // divisor: {
46
+ // mustBe: (divisor) => `a multiple of ${divisor}`,
47
+ // writeReason: (mustBe, was) => `${was} is not ${mustBe}!`
48
+ // }
49
+ // }
50
+ //}
51
+ ).export()
52
+ attest(types.isEven(3).errors?.summary).snap("3 is not a multiple of 2!")
53
+ })
54
+ it("domains", () => {
55
+ const t = type("string|number[]")
56
+ attest(t([1]).out).snap([1])
57
+ attest(t("hello").out).snap("hello")
58
+ attest(t(2).errors?.summary).snap("Must be a string or an object (was 2)")
59
+ })
60
+ it("tuple length", () => {
61
+ const t = type(["string", "number", "string", "string[]"])
62
+ const data: typeof t.infer = ["foo", 5, "boo", []]
63
+ attest(t(data).out).equals(data)
64
+ attest(t(["hello"]).errors?.summary).snap("length must be 4 (was 1)")
65
+ })
66
+ it("branches", () => {
67
+ const t = type([{ a: "string" }, "|", { b: "boolean" }])
68
+ attest(t({ a: "ok" }).out).snap({ a: "ok" })
69
+ attest(t({ b: true }).out).snap({ b: true })
70
+ attest(t({}).errors?.summary).snap(
71
+ "a must be defined or b must be defined (was {})"
72
+ )
73
+ })
74
+ it("branches at path", () => {
75
+ const t = type({ key: [{ a: "string" }, "|", { b: "boolean" }] })
76
+ attest(t({ key: { a: "ok" } }).out).snap({ key: { a: "ok" } })
77
+ attest(t({ key: { b: true } }).out).snap({ key: { b: true } })
78
+ attest(t({ key: {} }).errors?.summary).snap(
79
+ "At key, a must be defined or b must be defined (was {})"
80
+ )
81
+ })
82
+ it("switch", () => {
83
+ const t = type([{ a: "string" }, "|", { a: "boolean" }])
84
+ attest(t({ a: "ok" }).out).snap({ a: "ok" })
85
+ attest(t({ a: true }).out).snap({ a: true })
86
+ // value isn't present
87
+ attest(t({}).errors?.summary).snap(
88
+ "a must be a string or boolean (was undefined)"
89
+ )
90
+ // unsatisfying value
91
+ attest(t({ a: 5 }).errors?.summary).snap(
92
+ "a must be a string or boolean (was 5)"
93
+ )
94
+ })
95
+ it("multiple switch", () => {
96
+ const types = scope({
97
+ a: { a: "string" },
98
+ b: { a: "number" },
99
+ c: { a: "Function" },
100
+ d: "a|b|c"
101
+ }).export()
102
+ attest(types.d({}).errors?.summary).snap(
103
+ "a must be a string, a number or an object (was undefined)"
104
+ )
105
+ })
106
+
107
+ it("multi", () => {
108
+ const naturalNumber = type("integer>0")
109
+ attest(naturalNumber(-1.2).errors?.summary).snap(
110
+ "-1.2 must be...\n• an integer\n• more than 0"
111
+ )
112
+ const naturalAtPath = type({
113
+ natural: naturalNumber
114
+ })
115
+ attest(naturalAtPath({ natural: -0.1 }).errors?.summary).snap(
116
+ "At natural, -0.1 must be...\n• an integer\n• more than 0"
117
+ )
118
+ })
119
+ })
@@ -0,0 +1,12 @@
1
+ describe("tsGenerics", () => {
2
+ // it("in scope", () => {
3
+ // const types = scope({
4
+ // t: "Record<string, number>"
5
+ // }).export()
6
+ // attest<Record<string, number>>(types.t.infer)
7
+ // })
8
+ // it("in module", () => {
9
+ // const t = arktypes.Record("string", "number")
10
+ // attest<Record<string, number>>(t.infer)
11
+ // })
12
+ })
@@ -0,0 +1,69 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { ArkTypeError } from "@arktype/schema"
3
+ import { define, scope, type } from "arktype"
4
+ import { AssertionError } from "node:assert"
5
+ import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
6
+
7
+ describe("type methods", () => {
8
+ it("root discriminates", () => {
9
+ const t = type("string")
10
+ const { out, errors: errors } = t("")
11
+ if (errors) {
12
+ errors.throw()
13
+ } else {
14
+ attest<string>(out)
15
+ }
16
+ })
17
+ it("allows", () => {
18
+ const t = type("number%2")
19
+ const data: unknown = 4
20
+ if (t.allows(data)) {
21
+ // narrows correctly
22
+ attest<number>(data)
23
+ } else {
24
+ throw new Error()
25
+ }
26
+ attest(t.allows(5)).equals(false)
27
+ })
28
+ it("errors can be thrown", () => {
29
+ const t = type("number")
30
+ try {
31
+ attest(t("invalid").errors?.throw())
32
+ } catch (e) {
33
+ attest(e instanceof ArkTypeError).equals(true)
34
+ return
35
+ }
36
+ throw new AssertionError({ message: "Expected to throw" })
37
+ })
38
+ })
39
+
40
+ describe("scope utilities", () => {
41
+ describe("define", () => {
42
+ it("ark", () => {
43
+ const def = define({
44
+ a: "string|number",
45
+ b: ["boolean"],
46
+ c: "this"
47
+ })
48
+ attest<{ a: "string|number"; b: ["boolean"]; c: "this" }>(def)
49
+ })
50
+ it("ark error", () => {
51
+ // currently is a no-op, so only has type error
52
+ // @ts-expect-error
53
+ attest(define({ a: "boolean|foo" })).type.errors(
54
+ writeUnresolvableMessage("foo")
55
+ )
56
+ })
57
+ it("custom scope", () => {
58
+ const $ = scope({
59
+ a: "string[]"
60
+ })
61
+ const ok = $.define(["a[]|boolean"])
62
+ attest<["a[]|boolean"]>(ok)
63
+ // @ts-expect-error
64
+ attest($.define({ not: "ok" })).type.errors(
65
+ writeUnresolvableMessage("ok")
66
+ )
67
+ })
68
+ })
69
+ })
@@ -0,0 +1,39 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { ark, scope, type } from "arktype"
3
+ import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
4
+
5
+ describe("type references", () => {
6
+ it("shallow type reference", () => {
7
+ const t = type(type("boolean"))
8
+ attest<boolean>(t.infer)
9
+ })
10
+
11
+ it("bad shallow type reference", () => {
12
+ attest(() => {
13
+ // @ts-expect-error
14
+ type(type("foolean"))
15
+ }).throwsAndHasTypeError(writeUnresolvableMessage("foolean"))
16
+ })
17
+
18
+ it("deep type reference", () => {
19
+ const t = type({ a: type("boolean") })
20
+ attest<{ a: boolean }>(t.infer)
21
+ })
22
+
23
+ it("type reference in scope", () => {
24
+ const a = type({ a: "string" })
25
+ const $ = scope({ a })
26
+ const types = $.export()
27
+ attest(types.a.json).equals(type({ a: "string" }).json)
28
+ attest(a.scope).is(ark)
29
+ attest(types.a.scope).is($)
30
+ attest<{ a: string }>(types.a.infer)
31
+ })
32
+
33
+ it("bad deep type reference", () => {
34
+ attest(() => {
35
+ // @ts-expect-error
36
+ type({ a: type("goolean") })
37
+ }).throwsAndHasTypeError(writeUnresolvableMessage("goolean"))
38
+ })
39
+ })