arktype 2.0.0-dev.0 → 2.0.0-dev.10

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