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,442 +0,0 @@
1
- import { attest, contextualize } from "@arktype/attest"
2
- import {
3
- writeInvalidPropertyKeyMessage,
4
- writeUnboundableMessage,
5
- writeUnresolvableMessage
6
- } from "@arktype/schema"
7
- import { printable, registeredReference } from "@arktype/util"
8
- import { scope, type } from "arktype"
9
- import { writeInvalidSpreadTypeMessage } from "../parser/objectLiteral.js"
10
-
11
- contextualize(
12
- "named",
13
- () => {
14
- it("empty", () => {
15
- const o = type({})
16
- attest(o.json).equals(type("object").json)
17
- })
18
-
19
- it("required", () => {
20
- const o = type({ a: "string", b: "number" })
21
- attest<{ a: string; b: number }>(o.infer)
22
- attest(o.json).snap({
23
- domain: "object",
24
- prop: [
25
- { key: "a", value: "string" },
26
- { key: "b", value: "number" }
27
- ]
28
- })
29
- })
30
-
31
- it("optional keys", () => {
32
- const o = type({ "a?": "string", b: "number" })
33
- attest<{ a?: string; b: number }>(o.infer)
34
- attest(o.json).snap({
35
- domain: "object",
36
- prop: [
37
- { key: "a", optional: true, value: "string" },
38
- { key: "b", value: "number" }
39
- ]
40
- })
41
- })
42
-
43
- it("symbol key", () => {
44
- const s = Symbol()
45
- const name = registeredReference(s)
46
- const t = type({
47
- [s]: "string"
48
- })
49
- attest<{ [s]: string }>(t.infer)
50
- attest(t.json).snap({
51
- domain: "object",
52
- prop: [{ key: name, value: "string" }]
53
- })
54
- })
55
-
56
- it("error in obj that has tuple that writes error at proper path", () => {
57
- // @ts-expect-error
58
- attest(() => type({ "a?": ["string", ["stringx", "?"]] }))
59
- .throws(writeUnresolvableMessage("stringx"))
60
- .type.errors.snap(
61
- "Type '\"stringx\"' is not assignable to type '\"'stringx' is unresolvable \"'."
62
- )
63
- })
64
-
65
- it("nested", () => {
66
- const t = type({ "a?": { b: "boolean" } })
67
- attest<{ a?: { b: boolean } }>(t.infer)
68
- })
69
-
70
- it("intersections", () => {
71
- const a = { "a?": "string" } as const
72
- const b = { b: "string" } as const
73
- const c = { "c?": "string" } as const
74
- const abc = type(a).and(b).and(c)
75
- attest<{ a?: string; b: string; c?: string }>(abc.infer)
76
- attest(abc.json).equals(type({ ...a, ...b, ...c }).json)
77
- attest(abc.json).equals(type([[a, "&", b], "&", c]).json)
78
- })
79
-
80
- it("intersection", () => {
81
- const t = type({ a: "number" }).and({ b: "boolean" })
82
- // Should be simplified from {a: number} & {b: boolean} to {a: number, b: boolean}
83
- attest(t.infer).type.toString.snap("{ a: number; b: boolean; }")
84
- attest(t.json).equals(type({ a: "number", b: "boolean" }).json)
85
- })
86
-
87
- it("escaped optional token", () => {
88
- const t = type({ "a\\?": "string" })
89
- attest<{ "a?": string }>(t.infer)
90
- })
91
-
92
- it("traverse optional", () => {
93
- const o = type({ "a?": "string" })
94
- attest(o({ a: "a" })).snap({ a: "a" })
95
- attest(o({})).snap({})
96
- attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
97
- })
98
-
99
- // it("traverse strict optional", () => {
100
- // // TODO: strict
101
- // const o = type({ "a?": "string" })
102
- // attest(o({ a: "a" })).snap({ a: "a" })
103
- // attest(o({})).snap({})
104
- // attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
105
- // })
106
-
107
- // it("multiple bad strict", () => {
108
- // const t = type({ a: "string", b: "boolean" }).configure({
109
- // keys: "strict"
110
- // })
111
- // attest(t({ a: 1, b: 2 }).toString()).snap(
112
- // "a must be a string (was number)\nb must be boolean (was number)"
113
- // )
114
- // })
115
-
116
- // it("optional symbol", () => {
117
- // const s = Symbol()
118
- // const name = reference(s)
119
- // const t = type({
120
- // [optional(s)]: "number"
121
- // })
122
- // attest<{ [s]?: number }>(t.infer)
123
- // attest(t.json).equals({
124
- // domain: "object",
125
- // optional: [{ key: name, value: "number" }]
126
- // })
127
- // })
128
- },
129
- "spread syntax",
130
- () => {
131
- it("within scope", () => {
132
- const s = scope({
133
- user: { isAdmin: "false", name: "string" },
134
- admin: { "...": "user", isAdmin: "true" }
135
- }).export()
136
-
137
- attest<{ isAdmin: true; name: string }>(s.admin.infer)
138
- attest(s.admin.json).equals({
139
- domain: "object",
140
- prop: [
141
- { key: "isAdmin", value: { unit: true } },
142
- { key: "name", value: "string" }
143
- ]
144
- })
145
- })
146
-
147
- it("from another `type` call", () => {
148
- const user = type({ isAdmin: "false", name: "string" })
149
- const admin = type({ "...": user, isAdmin: "true" })
150
-
151
- attest<{ isAdmin: true; name: string }>(admin.infer)
152
- attest(admin.json).snap({
153
- domain: "object",
154
- prop: [
155
- { key: "isAdmin", value: { unit: true } },
156
- { key: "name", value: "string" }
157
- ]
158
- })
159
- })
160
-
161
- it("from an object literal", () => {
162
- // no idea why you'd want to do this
163
- const t = type({
164
- "...": {
165
- inherited: "boolean",
166
- overridden: "string"
167
- },
168
- overridden: "number"
169
- })
170
-
171
- attest<{
172
- inherited: boolean
173
- overridden: number
174
- }>(t.infer)
175
-
176
- attest(t.json).snap({
177
- domain: "object",
178
- prop: [
179
- {
180
- key: "inherited",
181
- value: [{ unit: false }, { unit: true }]
182
- },
183
- { key: "overridden", value: "number" }
184
- ]
185
- })
186
- })
187
-
188
- it("escaped key", () => {
189
- const t = type({
190
- "\\...": "string"
191
- })
192
-
193
- attest<{ "...": string }>(t.infer)
194
-
195
- attest(t.json).snap({
196
- domain: "object",
197
- prop: [{ key: "...", value: "string" }]
198
- })
199
- })
200
-
201
- it("with non-object", () => {
202
- // @ts-expect-error
203
- attest(() => type({ "...": "string" })).throwsAndHasTypeError(
204
- writeInvalidSpreadTypeMessage(printable("string"))
205
- )
206
- })
207
-
208
- // this is a regression test to ensure nodes are handled even if they aren't just an object
209
- it("with complex type", () => {
210
- const adminUser = type({
211
- "...": [{ name: "string" }, "&", { isAdmin: "false" }],
212
- isAdmin: "true"
213
- })
214
-
215
- attest<{ isAdmin: true; name: string }>(adminUser.infer)
216
- attest(adminUser.json).snap({
217
- domain: "object",
218
- prop: [
219
- { key: "isAdmin", value: { unit: true } },
220
- { key: "name", value: "string" }
221
- ]
222
- })
223
- })
224
- },
225
- "index",
226
- () => {
227
- it("string index", () => {
228
- const o = type({ "[string]": "string" })
229
- attest<{ [x: string]: string }>(o.infer)
230
- attest(o.json).snap({
231
- domain: "object",
232
- index: [{ key: "string", value: "string" }]
233
- })
234
-
235
- attest(o({})).equals({})
236
- attest(o({ a: "a", b: "b" })).equals({ a: "a", b: "b" })
237
-
238
- const validWithSymbol = { a: "a", [Symbol()]: null }
239
- attest(validWithSymbol).equals(validWithSymbol)
240
-
241
- attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
242
- attest(o({ a: true, b: false }).toString())
243
- .snap(`a must be a string (was true)
244
- b must be a string (was false)`)
245
- })
246
-
247
- it("symbol index", () => {
248
- const o = type({ "[symbol]": "1" })
249
- attest<{ [x: symbol]: 1 }>(o.infer)
250
- attest(o.json).snap({
251
- domain: "object",
252
- index: [{ key: "symbol", value: { unit: 1 } }]
253
- })
254
-
255
- attest(o({})).equals({})
256
-
257
- attest(o({ a: 999 })).unknown.snap({ a: 999 })
258
-
259
- const zildjian = Symbol()
260
- const zildjianName = printable(zildjian)
261
-
262
- // I've been dope, suspenseful with a pencil
263
- // Ever since...
264
- const prince = Symbol()
265
- const princeName = printable(prince)
266
-
267
- attest(o({ [zildjian]: 1, [prince]: 1 })).equals({
268
- [zildjian]: 1,
269
- [prince]: 1
270
- })
271
-
272
- attest({ a: 0, [zildjian]: 1 }).equals({ a: 0, [zildjian]: 1 })
273
-
274
- attest(o({ [zildjian]: 0 }).toString()).equals(
275
- `value at [${zildjianName}] must be 1 (was 0)`
276
- )
277
- attest(o({ [prince]: null, [zildjian]: undefined }).toString())
278
- .snap(`value at [${princeName}] must be 1 (was null)
279
- value at [${zildjianName}] must be 1 (was undefined)`)
280
- })
281
-
282
- it("enumerable indexed union", () => {
283
- const o = type({ "['foo' | 'bar']": "string" })
284
- const expected = type({ foo: "string", bar: "string" })
285
- attest<typeof expected>(o)
286
- attest(o.json).equals(expected.json)
287
- })
288
-
289
- it("non-enumerable indexed union", () => {
290
- const o = type({ "[string | symbol]": "string" })
291
- attest<{ [x: string]: string; [x: symbol]: string }>(o.infer)
292
- attest(o.json).snap({
293
- domain: "object",
294
- index: [{ key: ["string", "symbol"], value: "string" }]
295
- })
296
- })
297
-
298
- it("multiple indexed", () => {
299
- const o = type({
300
- "[string]": "string",
301
- "[symbol]": "number"
302
- })
303
- attest<{ [x: string]: string; [x: symbol]: number }>(o.infer)
304
- attest(o.json).snap({
305
- domain: "object",
306
- index: [
307
- { key: "string", value: "string" },
308
- { key: "symbol", value: "number" }
309
- ]
310
- })
311
-
312
- attest(o({})).equals({})
313
- attest(o({ foo: "f" })).equals({ foo: "f" })
314
-
315
- const sym = Symbol()
316
-
317
- const symName = printable(sym)
318
-
319
- const validWithStringsAndSymbols = {
320
- str: "string",
321
- [sym]: 8675309
322
- }
323
-
324
- attest(o(validWithStringsAndSymbols)).equals(validWithStringsAndSymbols)
325
-
326
- attest(
327
- o({
328
- str: 100,
329
- [sym]: "💯"
330
- }).toString()
331
- ).snap(`str must be a string (was number)
332
- value at [${symName}] must be a number (was string)`)
333
- })
334
-
335
- it("all key kinds", () => {
336
- const o = type({
337
- "[string]": "string",
338
- required: "'foo'",
339
- "optional?": "'bar'"
340
- })
341
- attest<{ [x: string]: string; required: "foo"; optional?: "bar" }>(
342
- o.infer
343
- )
344
- attest(o.json).snap({
345
- domain: "object",
346
- prop: [
347
- { key: "optional", optional: true, value: { unit: "bar" } },
348
- { key: "required", value: { unit: "foo" } }
349
- ],
350
- index: [{ key: "string", value: "string" }]
351
- })
352
-
353
- const valid: typeof o.infer = { required: "foo", other: "bar" }
354
- attest(o(valid)).equals(valid)
355
- attest(
356
- o({
357
- optional: "wrongString",
358
- other: 0n
359
- }).toString()
360
- ).snap(`optional must be "bar" (was "wrongString")
361
- required must be "foo" (was missing)
362
- other must be a string (was bigint)`)
363
- })
364
-
365
- it("index key from scope", () => {
366
- const types = scope({
367
- key: "symbol|'foo'|'bar'|'baz'",
368
- obj: {
369
- "[key]": "string"
370
- }
371
- }).export()
372
- type Key = symbol | "foo" | "bar" | "baz"
373
- attest<Key>(types.key.infer)
374
- attest<Record<Key, string>>(types.obj.infer)
375
-
376
- const expected = type({ "[symbol]": "string" }).and({
377
- foo: "string",
378
- bar: "string",
379
- baz: "string"
380
- })
381
-
382
- attest(types.obj.json).snap(expected.json)
383
- })
384
-
385
- it("syntax error in index definition", () => {
386
- attest(() =>
387
- type({
388
- // @ts-expect-error
389
- "[unresolvable]": "string"
390
- })
391
- ).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
392
- })
393
-
394
- it("does not allow syntax error message as value", () => {
395
- attest(() =>
396
- type({
397
- // @ts-expect-error
398
- "[unresolvable]": "'unresolvable' is unresolvable"
399
- })
400
- ).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
401
- })
402
-
403
- it("semantic error in index definition", () => {
404
- attest(() =>
405
- type({
406
- // @ts-expect-error
407
- "[symbol<5]": "string"
408
- })
409
- ).throwsAndHasTypeError(writeUnboundableMessage("symbol"))
410
- })
411
-
412
- it("invalid key type for index definition", () => {
413
- attest(() =>
414
- type({
415
- // @ts-expect-error
416
- "[object]": "string"
417
- })
418
- ).throwsAndHasTypeError(writeInvalidPropertyKeyMessage("object"))
419
- })
420
-
421
- it("does not allow invalid key type error as value", () => {
422
- attest(() =>
423
- type({
424
- // @ts-expect-error
425
- "[object]":
426
- "Indexed key definition 'object' must be a string, number or symbol"
427
- })
428
- )
429
- .throws(writeUnresolvableMessage("Indexed"))
430
- .type.errors(writeInvalidPropertyKeyMessage("object"))
431
- })
432
-
433
- it("escaped index", () => {
434
- const o = type({ "\\[string]": "string" })
435
- attest<{ "[string]": string }>(o.infer)
436
- attest(o.json).snap({
437
- domain: "object",
438
- prop: [{ key: "[string]", value: "string" }]
439
- })
440
- })
441
- }
442
- )
@@ -1,45 +0,0 @@
1
- import { bench } from "@arktype/attest"
2
- import { scope, type } from "arktype"
3
-
4
- bench("single-quoted", () => {
5
- const _ = type("'nineteen characters'")
6
- })
7
- .median([3.05, "us"])
8
- .types([502, "instantiations"])
9
-
10
- bench("double-quoted", () => {
11
- const _ = type('"nineteen characters"')
12
- })
13
- .median([3.13, "us"])
14
- .types([502, "instantiations"])
15
-
16
- bench("regex literal", () => {
17
- const _ = type("/nineteen characters/")
18
- })
19
- .median([4.18, "us"])
20
- .types([502, "instantiations"])
21
-
22
- bench("keyword", () => {
23
- const _ = type("string")
24
- })
25
- .median([1.44, "us"])
26
- .types([84, "instantiations"])
27
-
28
- const $ = scope({ strung: "string" })
29
- bench("alias", () => {
30
- const _ = $.type("strung")
31
- })
32
- .median([1.54, "us"])
33
- .types([725, "instantiations"])
34
-
35
- bench("number", () => {
36
- const _ = type("-98765.4321")
37
- })
38
- .median([4.41, "us"])
39
- .types([415, "instantiations"])
40
-
41
- bench("bigint", () => {
42
- const _ = type("-987654321n")
43
- })
44
- .median()
45
- .types()
@@ -1,130 +0,0 @@
1
- import { bench } from "@arktype/attest"
2
- import { type } from "arktype"
3
-
4
- bench("array-string", () => {
5
- const _ = type("number[]")
6
- })
7
- .median([3.18, "us"])
8
- .types([99, "instantiations"])
9
-
10
- bench("array-tuple", () => {
11
- const _ = type(["number", "[]"])
12
- })
13
- .median([3.47, "us"])
14
- .types([212, "instantiations"])
15
-
16
- bench("array-chain", () => {
17
- const _ = type("number").array()
18
- })
19
- .median([5.15, "us"])
20
- .types([5, "instantiations"])
21
-
22
- bench("union-string", () => {
23
- const _ = type("number|string")
24
- })
25
- .median([7.07, "us"])
26
- .types([777, "instantiations"])
27
-
28
- bench("union-tuple", () => {
29
- const _ = type(["number", "|", "string"])
30
- })
31
- .median([5.59, "us"])
32
- .types([273, "instantiations"])
33
-
34
- bench("union-chain", () => {
35
- const _ = type("number").or("string")
36
- })
37
- .median([7.09, "us"])
38
- .types([488, "instantiations"])
39
-
40
- bench("union-10-ary", () => {
41
- const _ = type("0|1|2|3|4|5|6|7|8|9")
42
- })
43
- .median([119.87, "us"])
44
- .types([4841, "instantiations"])
45
-
46
- bench("intersection-string", () => {
47
- const _ = type("number&0")
48
- })
49
- .median([3.61, "us"])
50
- .types([686, "instantiations"])
51
-
52
- bench("intersection-tuple", () => {
53
- const _ = type(["number", "&", "0"])
54
- })
55
- .median([3.16, "us"])
56
- .types([474, "instantiations"])
57
-
58
- bench("intersection-chain", () => {
59
- const _ = type("number").and("0")
60
- })
61
- .median([4.78, "us"])
62
- .types([687, "instantiations"])
63
-
64
- bench("intersection-10-ary", () => {
65
- const _ = type(
66
- "unknown&unknown&unknown&unknown&unknown&unknown&unknown&unknown&unknown&unknown"
67
- )
68
- })
69
- .median([12.61, "us"])
70
- .types([4351, "instantiations"])
71
-
72
- bench("group-shallow", () => {
73
- const _ = type("string|(number[])")
74
- })
75
- .median([9.58, "us"])
76
- .types([1080, "instantiations"])
77
-
78
- bench("group-nested", () => {
79
- const _ = type("string|(number|(boolean))[][]")
80
- })
81
- .median([17.22, "us"])
82
- .types([3008, "instantiations"])
83
-
84
- bench("group-deep", () => {
85
- const _ = type("(0|(1|(2|(3|(4|5)[])[])[])[])[]")
86
- })
87
- .median([32.5, "us"])
88
- .types([37638, "instantiations"])
89
-
90
- bench("bound-single", () => {
91
- const _ = type("string>5")
92
- })
93
- .median([3.81, "us"])
94
- .types([615, "instantiations"])
95
-
96
- bench("bound-double", () => {
97
- const _ = type("-7<=integer<99")
98
- })
99
- .median([7.2, "us"])
100
- .types([1041, "instantiations"])
101
-
102
- bench("divisor", () => {
103
- const _ = type("number%5")
104
- })
105
- .median([4.53, "us"])
106
- .types([1023, "instantiations"])
107
-
108
- bench("filter-tuple", () => {
109
- const _ = type(["boolean", ":", b => b])
110
- })
111
- .median([8.04, "us"])
112
- .types([583, "instantiations"])
113
-
114
- bench("filter-chain", () => {
115
- const _ = type("boolean").narrow(b => b)
116
- })
117
- .median([12.19, "us"])
118
- .types([42, "instantiations"])
119
-
120
- bench("morph-tuple", () => {
121
- const _ = type(["boolean", "=>", b => b])
122
- })
123
- .median([8.96, "us"])
124
- .types([562, "instantiations"])
125
-
126
- bench("morph-chain", () => {
127
- const _ = type("boolean").pipe(b => b)
128
- })
129
- .median([2.98, "us"])
130
- .types([26, "instantiations"])