arktype 1.1.0-dev-1 → 2.0.0-dev.0

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 (213) 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 +18 -0
  54. package/match.ts +116 -0
  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/out/match.js +18 -0
  70. package/out/match.js.map +1 -0
  71. package/out/parser/definition.d.ts +32 -0
  72. package/out/parser/definition.d.ts.map +1 -0
  73. package/out/parser/definition.js +32 -0
  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/out/parser/objectLiteral.d.ts +33 -0
  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/out/parser/semantic/bounds.js +2 -0
  86. package/out/parser/semantic/bounds.js.map +1 -0
  87. package/out/parser/semantic/divisor.d.ts +9 -0
  88. package/out/parser/semantic/divisor.d.ts.map +1 -0
  89. package/out/parser/semantic/divisor.js +2 -0
  90. package/out/parser/semantic/divisor.js.map +1 -0
  91. package/out/parser/semantic/intersections.d.ts +14 -0
  92. package/out/parser/semantic/intersections.d.ts.map +1 -0
  93. package/out/parser/semantic/intersections.js +2 -0
  94. package/out/parser/semantic/intersections.js.map +1 -0
  95. package/out/parser/semantic/semantic.d.ts +33 -0
  96. package/out/parser/semantic/semantic.d.ts.map +1 -0
  97. package/out/parser/semantic/semantic.js +2 -0
  98. package/out/parser/semantic/semantic.js.map +1 -0
  99. package/out/parser/semantic/utils.d.ts +8 -0
  100. package/out/parser/semantic/utils.d.ts.map +1 -0
  101. package/out/parser/semantic/utils.js +2 -0
  102. package/out/parser/semantic/utils.js.map +1 -0
  103. package/out/parser/semantic/validate.d.ts +24 -0
  104. package/out/parser/semantic/validate.d.ts.map +1 -0
  105. package/out/parser/semantic/validate.js +2 -0
  106. package/out/parser/semantic/validate.js.map +1 -0
  107. package/out/parser/shared.d.ts +54 -0
  108. package/out/parser/shared.d.ts.map +1 -0
  109. package/out/parser/shared.js +47 -0
  110. package/out/parser/shared.js.map +1 -0
  111. package/out/parser/string/reduce/dynamic.d.ts +42 -0
  112. package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
  113. package/out/parser/string/reduce/dynamic.js +133 -0
  114. package/out/parser/string/reduce/dynamic.js.map +1 -0
  115. package/out/parser/string/reduce/shared.d.ts +56 -0
  116. package/out/parser/string/reduce/shared.d.ts.map +1 -0
  117. package/out/parser/string/reduce/shared.js +26 -0
  118. package/out/parser/string/reduce/shared.js.map +1 -0
  119. package/out/parser/string/reduce/static.d.ts +176 -0
  120. package/out/parser/string/reduce/static.d.ts.map +1 -0
  121. package/out/parser/string/reduce/static.js +2 -0
  122. package/out/parser/string/reduce/static.js.map +1 -0
  123. package/out/parser/string/shift/operand/date.d.ts +11 -0
  124. package/out/parser/string/shift/operand/date.d.ts.map +1 -0
  125. package/out/parser/string/shift/operand/date.js +26 -0
  126. package/out/parser/string/shift/operand/date.js.map +1 -0
  127. package/out/parser/string/shift/operand/enclosed.d.ts +49 -0
  128. package/out/parser/string/shift/operand/enclosed.d.ts.map +1 -0
  129. package/out/parser/string/shift/operand/enclosed.js +50 -0
  130. package/out/parser/string/shift/operand/enclosed.js.map +1 -0
  131. package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
  132. package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
  133. package/out/parser/string/shift/operand/genericArgs.js +25 -0
  134. package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
  135. package/out/parser/string/shift/operand/operand.d.ts +9 -0
  136. package/out/parser/string/shift/operand/operand.d.ts.map +1 -0
  137. package/out/parser/string/shift/operand/operand.js +17 -0
  138. package/out/parser/string/shift/operand/operand.js.map +1 -0
  139. package/out/parser/string/shift/operand/unenclosed.d.ts +34 -0
  140. package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
  141. package/out/parser/string/shift/operand/unenclosed.js +68 -0
  142. package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
  143. package/out/parser/string/shift/operator/bounds.d.ts +33 -0
  144. package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
  145. package/out/parser/string/shift/operator/bounds.js +106 -0
  146. package/out/parser/string/shift/operator/bounds.js.map +1 -0
  147. package/out/parser/string/shift/operator/divisor.d.ts +9 -0
  148. package/out/parser/string/shift/operator/divisor.d.ts.map +1 -0
  149. package/out/parser/string/shift/operator/divisor.js +13 -0
  150. package/out/parser/string/shift/operator/divisor.js.map +1 -0
  151. package/out/parser/string/shift/operator/operator.d.ts +13 -0
  152. package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
  153. package/out/parser/string/shift/operator/operator.js +29 -0
  154. package/out/parser/string/shift/operator/operator.js.map +1 -0
  155. package/out/parser/string/shift/scanner.d.ts +71 -0
  156. package/out/parser/string/shift/scanner.d.ts.map +1 -0
  157. package/out/parser/string/shift/scanner.js +99 -0
  158. package/out/parser/string/shift/scanner.js.map +1 -0
  159. package/out/parser/string/string.d.ts +24 -0
  160. package/out/parser/string/string.d.ts.map +1 -0
  161. package/out/parser/string/string.js +23 -0
  162. package/out/parser/string/string.js.map +1 -0
  163. package/out/parser/tuple.d.ts +71 -0
  164. package/out/parser/tuple.d.ts.map +1 -0
  165. package/out/parser/tuple.js +137 -0
  166. package/out/parser/tuple.js.map +1 -0
  167. package/out/scope.d.ts +159 -0
  168. package/out/scope.d.ts.map +1 -0
  169. package/out/scope.js +253 -0
  170. package/out/scope.js.map +1 -0
  171. package/out/test.d.ts +2 -0
  172. package/out/test.d.ts.map +1 -0
  173. package/out/test.js +7 -0
  174. package/out/test.js.map +1 -0
  175. package/out/tsconfig.build.tsbuildinfo +1 -0
  176. package/out/type.d.ts +77 -0
  177. package/out/type.d.ts.map +1 -0
  178. package/out/type.js +134 -0
  179. package/out/type.js.map +1 -0
  180. package/out/util.d.ts +19 -0
  181. package/out/util.d.ts.map +1 -0
  182. package/out/util.js +7 -0
  183. package/out/util.js.map +1 -0
  184. package/package.json +13 -10
  185. package/parser/definition.ts +159 -0
  186. package/parser/generic.ts +68 -0
  187. package/parser/objectLiteral.ts +197 -0
  188. package/parser/semantic/bounds.ts +61 -0
  189. package/parser/semantic/divisor.ts +13 -0
  190. package/parser/semantic/intersections.ts +46 -0
  191. package/parser/semantic/semantic.ts +144 -0
  192. package/parser/semantic/utils.ts +26 -0
  193. package/parser/semantic/validate.ts +109 -0
  194. package/parser/shared.ts +152 -0
  195. package/parser/string/reduce/dynamic.ts +196 -0
  196. package/parser/string/reduce/shared.ts +101 -0
  197. package/parser/string/reduce/static.ts +298 -0
  198. package/parser/string/shift/operand/date.ts +54 -0
  199. package/parser/string/shift/operand/enclosed.ts +118 -0
  200. package/parser/string/shift/operand/genericArgs.ts +127 -0
  201. package/parser/string/shift/operand/operand.ts +48 -0
  202. package/parser/string/shift/operand/unenclosed.ts +268 -0
  203. package/parser/string/shift/operator/bounds.ts +264 -0
  204. package/parser/string/shift/operator/divisor.ts +36 -0
  205. package/parser/string/shift/operator/operator.ts +82 -0
  206. package/parser/string/shift/scanner.ts +209 -0
  207. package/parser/string/string.ts +90 -0
  208. package/parser/tuple.ts +474 -0
  209. package/scope.ts +574 -0
  210. package/type.ts +347 -0
  211. package/util.ts +36 -0
  212. package/dist/main.d.ts +0 -1018
  213. package/dist/main.js +0 -1450
@@ -0,0 +1,120 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { writeMalformedNumericLiteralMessage } from "@arktype/util"
3
+ import { scope, type } from "arktype"
4
+ import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
5
+
6
+ describe("parse unenclosed", () => {
7
+ describe("identifier", () => {
8
+ it("keyword", () => {
9
+ attest<string>(type("string").infer)
10
+ })
11
+ it("alias", () => {
12
+ const a = scope({ a: "string" }).type("a")
13
+ attest<string>(a.infer)
14
+ })
15
+ describe("errors", () => {
16
+ it("unresolvable", () => {
17
+ // @ts-expect-error
18
+ attest(() => type("HUH")).throwsAndHasTypeError(
19
+ writeUnresolvableMessage("HUH")
20
+ )
21
+ })
22
+ })
23
+ })
24
+ describe("number", () => {
25
+ describe("positive", () => {
26
+ it("whole", () => {
27
+ const four = type("4")
28
+ attest<4>(four.infer)
29
+ // attest(four.node).snap({ number: { value: 4 } })
30
+ })
31
+ it("decimal", () => {
32
+ attest<3.14159>(type("3.14159").infer)
33
+ })
34
+ it("decimal with zero whole portion", () => {
35
+ attest<0.5>(type("0.5").infer)
36
+ })
37
+ })
38
+ describe("negative", () => {
39
+ it("whole", () => {
40
+ attest<-12>(type("-12").infer)
41
+ })
42
+ it("decimal", () => {
43
+ attest<-1.618>(type("-1.618").infer)
44
+ })
45
+ it("decimal with zero whole portion", () => {
46
+ attest<-0.001>(type("-0.001").infer)
47
+ })
48
+ })
49
+ it("zero", () => {
50
+ attest<0>(type("0").infer)
51
+ })
52
+ describe("errors", () => {
53
+ it("multiple decimals", () => {
54
+ // @ts-expect-error
55
+ attest(() => type("127.0.0.1")).throwsAndHasTypeError(
56
+ writeUnresolvableMessage("127.0.0.1")
57
+ )
58
+ })
59
+ it("with alpha", () => {
60
+ // @ts-expect-error
61
+ attest(() => type("13three7")).throwsAndHasTypeError(
62
+ writeUnresolvableMessage("13three7")
63
+ )
64
+ })
65
+ it("leading zeroes", () => {
66
+ // @ts-expect-error
67
+ attest(() => type("010")).throwsAndHasTypeError(
68
+ writeMalformedNumericLiteralMessage("010", "number")
69
+ )
70
+ })
71
+ it("trailing zeroes", () => {
72
+ // @ts-expect-error
73
+ attest(() => type("4.0")).throws(
74
+ writeMalformedNumericLiteralMessage("4.0", "number")
75
+ )
76
+ })
77
+ it("negative zero", () => {
78
+ // @ts-expect-error
79
+ attest(() => type("-0")).throws(
80
+ writeMalformedNumericLiteralMessage("-0", "number")
81
+ )
82
+ })
83
+ })
84
+ })
85
+ describe("bigint", () => {
86
+ it("positive", () => {
87
+ // Is prime :D
88
+ attest<12345678910987654321n>(type("12345678910987654321n").infer)
89
+ })
90
+ it("negative", () => {
91
+ attest<-9801n>(type("-9801n").infer)
92
+ })
93
+ it("zero", () => {
94
+ attest<0n>(type("0n").infer)
95
+ })
96
+ describe("errors", () => {
97
+ it("decimal", () => {
98
+ // @ts-expect-error
99
+ attest(() => type("999.1n")).throwsAndHasTypeError(
100
+ writeUnresolvableMessage("999.1n")
101
+ )
102
+ })
103
+
104
+ it("leading zeroes", () => {
105
+ // TS currently doesn't try to infer this as bigint even
106
+ // though it matches our rules for a "malformed" integer.
107
+ // @ts-expect-error
108
+ attest(() => type("007n"))
109
+ .throws(writeMalformedNumericLiteralMessage("007n", "bigint"))
110
+ .type.errors(writeUnresolvableMessage("007n"))
111
+ })
112
+ it("negative zero", () => {
113
+ // @ts-expect-error
114
+ attest(() => type("-0n")).throwsAndHasTypeError(
115
+ writeMalformedNumericLiteralMessage("-0n", "bigint")
116
+ )
117
+ })
118
+ })
119
+ })
120
+ })
@@ -0,0 +1,147 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { schema, writeIndivisibleMessage } from "@arktype/schema"
3
+ import { type } from "arktype"
4
+ import {
5
+ writeMissingRightOperandMessage,
6
+ writeUnresolvableMessage
7
+ } from "../parser/string/shift/operand/unenclosed.js"
8
+
9
+ describe("union", () => {
10
+ it("binary", () => {
11
+ const binary = type("number|string")
12
+ attest<number | string>(binary.infer)
13
+ // attest(binary.node).snap({ number: true, string: true })
14
+ })
15
+ it("nary", () => {
16
+ const nary = type("false|null|undefined|0|''")
17
+ attest<false | "" | 0 | null | undefined>(nary.infer)
18
+ const expected = schema.units(false, null, undefined, 0, "")
19
+ attest(nary.json).equals(expected.json)
20
+ })
21
+ it("subtype pruning", () => {
22
+ type([{ a: "string" }, "|", { a: "'foo'" }])
23
+ })
24
+ it("multiple subtypes pruned", () => {
25
+ const t = type("'foo'|'bar'|string|'baz'|/.*/")
26
+ attest<string>(t.infer)
27
+ attest(t.json).is(type("string").json)
28
+ })
29
+ it("union of true and false reduces to boolean", () => {
30
+ const t = type("true|false")
31
+ attest(t.infer).type.toString("boolean")
32
+ attest(t.json).equals(type("boolean").json)
33
+ })
34
+ it("nested tuple union", () => {
35
+ const t = type(["string|bigint", "|", ["number", "|", "boolean"]])
36
+ attest<string | number | bigint | boolean>(t.infer)
37
+ attest(t.json).equals(type("string|bigint|number|boolean").json)
38
+ })
39
+ it("length stress", () => {
40
+ // as of TS 5.1, can handle a max of 46 branches before an inifinitely
41
+ // deep error not the end of the world if this changes slightly, but
42
+ // wanted to make those changes explicit if something reduces it it's
43
+ // also still very responsive up until it hits the limit, so it is
44
+ // likely a safeguard rather than a limitation of the parser
45
+ const t = type(
46
+ "0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45"
47
+ )
48
+ // prettier-ignore
49
+ attest<0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45>(t.infer)
50
+ })
51
+ describe("expressions", () => {
52
+ const expected = () =>
53
+ schema(
54
+ {
55
+ basis: "object",
56
+ required: {
57
+ key: "a",
58
+ value: { basis: "string" }
59
+ }
60
+ },
61
+ {
62
+ basis: "object",
63
+ required: {
64
+ key: "b",
65
+ value: { basis: "number" }
66
+ }
67
+ }
68
+ ).json
69
+
70
+ it("tuple", () => {
71
+ const t = type([{ a: "string" }, "|", { b: "number" }])
72
+ attest<{ a: string } | { b: number }>(t.infer)
73
+ attest(t.json).equals(expected())
74
+ })
75
+ it("root", () => {
76
+ const t = type({ a: "string" }, "|", { b: "number" })
77
+ attest<{ a: string } | { b: number }>(t.infer)
78
+ attest(t.json).equals(expected())
79
+ })
80
+ it("chained", () => {
81
+ const t = type({ a: "string" }).or({ b: "number" })
82
+ attest<
83
+ | {
84
+ a: string
85
+ }
86
+ | {
87
+ b: number
88
+ }
89
+ >(t.infer)
90
+ attest(t.json).equals(expected())
91
+ })
92
+ it("root autocompletions", () => {
93
+ // @ts-expect-error
94
+ attest(() => type({ a: "s" }, "|", { b: "boolean" })).type.errors(
95
+ `Type '"s"' is not assignable to type '"string" | "symbol" | "semver"'`
96
+ )
97
+ // @ts-expect-error
98
+ attest(() => type({ a: "string" }, "|", { b: "b" })).type.errors(
99
+ `Type '"b"' is not assignable to type '"bigint" | "boolean"'`
100
+ )
101
+ })
102
+ })
103
+ describe("errors", () => {
104
+ it("bad reference", () => {
105
+ // @ts-expect-error
106
+ attest(() => type("number|strng")).throwsAndHasTypeError(
107
+ writeUnresolvableMessage("strng")
108
+ )
109
+ })
110
+ it("consecutive tokens", () => {
111
+ // @ts-expect-error
112
+ attest(() => type("boolean||null")).throws(
113
+ writeMissingRightOperandMessage("|", "|null")
114
+ )
115
+ })
116
+ it("ends with |", () => {
117
+ // @ts-expect-error
118
+ attest(() => type("boolean|")).throws(
119
+ writeMissingRightOperandMessage("|", "")
120
+ )
121
+ })
122
+ it("long missing union member", () => {
123
+ attest(() =>
124
+ // @ts-expect-error
125
+ type("boolean[]|(string|number|)|object")
126
+ ).throws(writeMissingRightOperandMessage("|", ")|object"))
127
+ })
128
+ it("left semantic error", () => {
129
+ // @ts-expect-error
130
+ attest(() => type("symbol%2|string")).throwsAndHasTypeError(
131
+ writeIndivisibleMessage("symbol")
132
+ )
133
+ })
134
+ it("right semantic error", () => {
135
+ // @ts-expect-error
136
+ attest(() => type("string|symbol%2")).throwsAndHasTypeError(
137
+ writeIndivisibleMessage("symbol")
138
+ )
139
+ })
140
+ it("chained bad reference", () => {
141
+ // @ts-expect-error
142
+ attest(() => type("string").or("nummer")).throwsAndHasTypeError(
143
+ writeUnresolvableMessage("nummer")
144
+ )
145
+ })
146
+ })
147
+ })
package/ark.ts ADDED
@@ -0,0 +1,91 @@
1
+ import {
2
+ JsObjects,
3
+ Parsing,
4
+ TsKeywords,
5
+ Validation,
6
+ type inferred
7
+ } from "@arktype/schema"
8
+ import type { MatchParser } from "./match.js"
9
+ import { Scope, type Module, type ScopeParser } from "./scope.js"
10
+ import type {
11
+ DeclarationParser,
12
+ DefinitionParser,
13
+ Generic,
14
+ TypeParser
15
+ } from "./type.js"
16
+
17
+ /** Root scopes can be inferred automatically from node definitions, but
18
+ * explicitly typing them can improve responsiveness */
19
+ export type RootScope<exports> = Scope<{
20
+ exports: exports
21
+ locals: {}
22
+ ambient: {}
23
+ }>
24
+
25
+ export type ArkResolutions = { exports: Ark; locals: {}; ambient: Ark }
26
+
27
+ // For some reason if we try to inline this, it gets evaluated and the module
28
+ // can't be inferred
29
+ export type ParsingResolutions = {
30
+ exports: Parsing.infer
31
+ locals: {}
32
+ ambient: {}
33
+ }
34
+
35
+ export type TsGenericsResolutions<$ = Ark> = {
36
+ exports: TsGenericsExports<$>
37
+ locals: {}
38
+ ambient: {}
39
+ }
40
+
41
+ type TsGenericsExports<$ = Ark> = {
42
+ Record: Generic<
43
+ ["K", "V"],
44
+ {
45
+ "[K]": "V"
46
+ },
47
+ // as long as the generics in the root scope don't reference one
48
+ // another, they shouldn't need a bound local scope
49
+ $
50
+ >
51
+ }
52
+
53
+ export const tsGenerics = {} as Module<TsGenericsResolutions>
54
+
55
+ export const ark: Scope<ArkResolutions> = Scope.root({
56
+ ...TsKeywords.resolutions,
57
+ ...JsObjects.resolutions,
58
+ ...Validation.resolutions,
59
+ // TODO: fix
60
+ ...tsGenerics,
61
+ parse: Parsing.resolutions as {} as Module<ParsingResolutions>
62
+ }).toAmbient() as never
63
+
64
+ export const arktypes: Module<ArkResolutions> = ark.export()
65
+
66
+ // this type is redundant with the inferred definition of ark but allow types
67
+ // derived from the default scope to be calulated more efficiently
68
+ export interface Ark
69
+ extends TsKeywords.infer,
70
+ JsObjects.infer,
71
+ Validation.infer,
72
+ TsGenericsExports {
73
+ parse: Module<ParsingResolutions>
74
+ }
75
+
76
+ export const scope: ScopeParser<{}, Ark> = ark.scope as never
77
+
78
+ export const type: TypeParser<Ark> = ark.type
79
+
80
+ // TODO: cast needed?
81
+ export const match: MatchParser<Ark> = ark.match as never
82
+
83
+ export namespace type {
84
+ export type cast<to> = {
85
+ [inferred]?: to
86
+ }
87
+ }
88
+
89
+ export const define: DefinitionParser<Ark> = ark.define
90
+
91
+ export const declare: DeclarationParser<Ark> = ark.declare
package/main.ts ADDED
@@ -0,0 +1,18 @@
1
+ export {
2
+ ArkErrors as ArkErrors,
3
+ type ArkTypeError as ArkTypeError
4
+ } from "@arktype/schema"
5
+ export type { Out, is } from "@arktype/schema"
6
+ export {
7
+ ark,
8
+ arktypes,
9
+ declare,
10
+ define,
11
+ match,
12
+ scope,
13
+ type,
14
+ type Ark
15
+ } from "./ark.js"
16
+ export type { Module, Scope } from "./scope.js"
17
+ export { Type } from "./type.js"
18
+ export type { inferTypeRoot, validateTypeRoot } from "./type.js"
package/match.ts ADDED
@@ -0,0 +1,116 @@
1
+ import type { Morph } from "@arktype/schema"
2
+ import type {
3
+ Fn,
4
+ isDisjoint,
5
+ replaceKey,
6
+ returnOf,
7
+ unionToTuple,
8
+ valueOf
9
+ } from "@arktype/util"
10
+ import type { Scope } from "./scope.js"
11
+ import { Type, type inferTypeRoot, type validateTypeRoot } from "./type.js"
12
+
13
+ type MatchContext = {
14
+ inConstraint: unknown
15
+ outConstraint: unknown
16
+ thens: readonly Fn[]
17
+ $: unknown
18
+ }
19
+
20
+ type validateCases<cases, ctx extends MatchContext> = {
21
+ [k in keyof cases | keyof ctx["$"] | "default"]?: k extends "default"
22
+ ? (In: ctx["inConstraint"]) => ctx["outConstraint"]
23
+ : k extends validateTypeRoot<k, ctx["$"]>
24
+ ? (
25
+ In: ctx["inConstraint"] & inferTypeRoot<k, ctx["$"]>
26
+ ) => ctx["outConstraint"]
27
+ : validateTypeRoot<k, ctx["$"]>
28
+ }
29
+
30
+ type errorCases<cases, ctx extends MatchContext> = {
31
+ [k in keyof cases]?: k extends "default"
32
+ ? (In: ctx["inConstraint"]) => ctx["outConstraint"]
33
+ : k extends validateTypeRoot<k, ctx["$"]>
34
+ ? (
35
+ In: ctx["inConstraint"] & inferTypeRoot<k, ctx["$"]>
36
+ ) => ctx["outConstraint"]
37
+ : validateTypeRoot<k, ctx["$"]>
38
+ } & {
39
+ [k in Exclude<keyof ctx["$"], keyof cases>]?: (
40
+ In: ctx["inConstraint"] & ctx["$"][k]
41
+ ) => ctx["outConstraint"]
42
+ } & {
43
+ default?: (In: ctx["inConstraint"]) => ctx["outConstraint"]
44
+ }
45
+
46
+ export type CaseMatchParser<ctx extends MatchContext> = {
47
+ <cases>(
48
+ def: cases extends validateCases<cases, ctx>
49
+ ? cases
50
+ : errorCases<cases, ctx>
51
+ ): ChainableMatchParser<
52
+ replaceKey<ctx, "thens", [...ctx["thens"], ...unionToTuple<valueOf<cases>>]>
53
+ >
54
+ }
55
+
56
+ export type MatchParser<$> = CaseMatchParser<{
57
+ inConstraint: unknown
58
+ outConstraint: unknown
59
+ thens: []
60
+ $: $
61
+ }> & {
62
+ <In = unknown, Out = unknown>(): ChainableMatchParser<{
63
+ inConstraint: In
64
+ outConstraint: Out
65
+ thens: []
66
+ $: $
67
+ }>
68
+ }
69
+
70
+ export type WhenMatchParser<ctx extends MatchContext> = <
71
+ def,
72
+ then extends (
73
+ In: ctx["inConstraint"] & inferTypeRoot<def, ctx["$"]>
74
+ ) => ctx["outConstraint"]
75
+ >(
76
+ when: validateTypeRoot<def, ctx["$"]>,
77
+ then: then
78
+ ) => ChainableMatchParser<replaceKey<ctx, "thens", [...ctx["thens"], then]>>
79
+
80
+ export type MatchInvocation<ctx extends MatchContext> = <
81
+ data extends ctx["inConstraint"]
82
+ >(
83
+ data: data
84
+ ) => {
85
+ [i in Extract<keyof ctx["thens"], `${number}`>]: ctx["thens"][i] extends Fn<
86
+ [infer In],
87
+ infer Out
88
+ >
89
+ ? isDisjoint<data, In> extends true
90
+ ? never
91
+ : Out
92
+ : returnOf<ctx["thens"][i]>
93
+ }[Extract<keyof ctx["thens"], `${number}`>]
94
+
95
+ export type ChainableMatchParser<ctx extends MatchContext> =
96
+ MatchInvocation<ctx> & {
97
+ cases: CaseMatchParser<ctx>
98
+ when: WhenMatchParser<ctx>
99
+ }
100
+
101
+ export const createMatchParser = <$>(scope: Scope): MatchParser<$> => {
102
+ const parser = (cases: Record<string, Morph>) => {
103
+ const caseArray = Object.entries(cases).map(([def, morph]) => ({
104
+ when: new Type(def, scope).allows,
105
+ then: morph
106
+ }))
107
+ return (data: unknown) => {
108
+ for (const c of caseArray) {
109
+ if (c.when(data)) {
110
+ return c.then(data, {} as never)
111
+ }
112
+ }
113
+ }
114
+ }
115
+ return parser as never
116
+ }
package/out/ark.d.ts ADDED
@@ -0,0 +1,52 @@
1
+ import { JsObjects, Parsing, TsKeywords, Validation, type inferred } from "@arktype/schema";
2
+ import type { MatchParser } from "./match.js";
3
+ import { Scope, type Module, type ScopeParser } from "./scope.js";
4
+ import type { DeclarationParser, DefinitionParser, Generic, TypeParser } from "./type.js";
5
+ /** Root scopes can be inferred automatically from node definitions, but
6
+ * explicitly typing them can improve responsiveness */
7
+ export type RootScope<exports> = Scope<{
8
+ exports: exports;
9
+ locals: {};
10
+ ambient: {};
11
+ }>;
12
+ export type ArkResolutions = {
13
+ exports: Ark;
14
+ locals: {};
15
+ ambient: Ark;
16
+ };
17
+ export type ParsingResolutions = {
18
+ exports: Parsing.infer;
19
+ locals: {};
20
+ ambient: {};
21
+ };
22
+ export type TsGenericsResolutions<$ = Ark> = {
23
+ exports: TsGenericsExports<$>;
24
+ locals: {};
25
+ ambient: {};
26
+ };
27
+ type TsGenericsExports<$ = Ark> = {
28
+ Record: Generic<[
29
+ "K",
30
+ "V"
31
+ ], {
32
+ "[K]": "V";
33
+ }, $>;
34
+ };
35
+ export declare const tsGenerics: Module<TsGenericsResolutions<Ark>>;
36
+ export declare const ark: Scope<ArkResolutions>;
37
+ export declare const arktypes: Module<ArkResolutions>;
38
+ export interface Ark extends TsKeywords.infer, JsObjects.infer, Validation.infer, TsGenericsExports {
39
+ parse: Module<ParsingResolutions>;
40
+ }
41
+ export declare const scope: ScopeParser<{}, Ark>;
42
+ export declare const type: TypeParser<Ark>;
43
+ export declare const match: MatchParser<Ark>;
44
+ export declare namespace type {
45
+ type cast<to> = {
46
+ [inferred]?: to;
47
+ };
48
+ }
49
+ export declare const define: DefinitionParser<Ark>;
50
+ export declare const declare: DeclarationParser<Ark>;
51
+ export {};
52
+ //# sourceMappingURL=ark.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ark.d.ts","sourceRoot":"","sources":["../ark.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EACT,OAAO,EACP,UAAU,EACV,UAAU,EACV,KAAK,QAAQ,EACb,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,KAAK,EAAE,KAAK,MAAM,EAAE,KAAK,WAAW,EAAE,MAAM,YAAY,CAAA;AACjE,OAAO,KAAK,EACX,iBAAiB,EACjB,gBAAgB,EAChB,OAAO,EACP,UAAU,EACV,MAAM,WAAW,CAAA;AAElB;uDACuD;AACvD,MAAM,MAAM,SAAS,CAAC,OAAO,IAAI,KAAK,CAAC;IACtC,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,EAAE,CAAA;IACV,OAAO,EAAE,EAAE,CAAA;CACX,CAAC,CAAA;AAEF,MAAM,MAAM,cAAc,GAAG;IAAE,OAAO,EAAE,GAAG,CAAC;IAAC,MAAM,EAAE,EAAE,CAAC;IAAC,OAAO,EAAE,GAAG,CAAA;CAAE,CAAA;AAIvE,MAAM,MAAM,kBAAkB,GAAG;IAChC,OAAO,EAAE,OAAO,CAAC,KAAK,CAAA;IACtB,MAAM,EAAE,EAAE,CAAA;IACV,OAAO,EAAE,EAAE,CAAA;CACX,CAAA;AAED,MAAM,MAAM,qBAAqB,CAAC,CAAC,GAAG,GAAG,IAAI;IAC5C,OAAO,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAA;IAC7B,MAAM,EAAE,EAAE,CAAA;IACV,OAAO,EAAE,EAAE,CAAA;CACX,CAAA;AAED,KAAK,iBAAiB,CAAC,CAAC,GAAG,GAAG,IAAI;IACjC,MAAM,EAAE,OAAO,CACd;QAAC,GAAG;QAAE,GAAG;KAAC,EACV;QACC,KAAK,EAAE,GAAG,CAAA;KACV,EAGD,CAAC,CACD,CAAA;CACD,CAAA;AAED,eAAO,MAAM,UAAU,oCAAsC,CAAA;AAE7D,eAAO,MAAM,GAAG,EAAE,KAAK,CAAC,cAAc,CAOf,CAAA;AAEvB,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,cAAc,CAAgB,CAAA;AAI5D,MAAM,WAAW,GAChB,SAAQ,UAAU,CAAC,KAAK,EACvB,SAAS,CAAC,KAAK,EACf,UAAU,CAAC,KAAK,EAChB,iBAAiB;IAClB,KAAK,EAAE,MAAM,CAAC,kBAAkB,CAAC,CAAA;CACjC;AAED,eAAO,MAAM,KAAK,EAAE,WAAW,CAAC,EAAE,EAAE,GAAG,CAAsB,CAAA;AAE7D,eAAO,MAAM,IAAI,EAAE,UAAU,CAAC,GAAG,CAAY,CAAA;AAG7C,eAAO,MAAM,KAAK,EAAE,WAAW,CAAC,GAAG,CAAsB,CAAA;AAEzD,yBAAiB,IAAI,CAAC;IACrB,KAAY,IAAI,CAAC,EAAE,IAAI;QACtB,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAA;KACf,CAAA;CACD;AAED,eAAO,MAAM,MAAM,EAAE,gBAAgB,CAAC,GAAG,CAAc,CAAA;AAEvD,eAAO,MAAM,OAAO,EAAE,iBAAiB,CAAC,GAAG,CAAe,CAAA"}
package/out/ark.js ADDED
@@ -0,0 +1,19 @@
1
+ import { JsObjects, Parsing, TsKeywords, Validation } from "@arktype/schema";
2
+ import { Scope } from "./scope.js";
3
+ export const tsGenerics = {};
4
+ export const ark = Scope.root({
5
+ ...TsKeywords.resolutions,
6
+ ...JsObjects.resolutions,
7
+ ...Validation.resolutions,
8
+ // TODO: fix
9
+ ...tsGenerics,
10
+ parse: Parsing.resolutions
11
+ }).toAmbient();
12
+ export const arktypes = ark.export();
13
+ export const scope = ark.scope;
14
+ export const type = ark.type;
15
+ // TODO: cast needed?
16
+ export const match = ark.match;
17
+ export const define = ark.define;
18
+ export const declare = ark.declare;
19
+ //# sourceMappingURL=ark.js.map
package/out/ark.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ark.js","sourceRoot":"","sources":["../ark.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EACT,OAAO,EACP,UAAU,EACV,UAAU,EAEV,MAAM,iBAAiB,CAAA;AAExB,OAAO,EAAE,KAAK,EAAiC,MAAM,YAAY,CAAA;AA4CjE,MAAM,CAAC,MAAM,UAAU,GAAG,EAAmC,CAAA;AAE7D,MAAM,CAAC,MAAM,GAAG,GAA0B,KAAK,CAAC,IAAI,CAAC;IACpD,GAAG,UAAU,CAAC,WAAW;IACzB,GAAG,SAAS,CAAC,WAAW;IACxB,GAAG,UAAU,CAAC,WAAW;IACzB,YAAY;IACZ,GAAG,UAAU;IACb,KAAK,EAAE,OAAO,CAAC,WAA+C;CAC9D,CAAC,CAAC,SAAS,EAAW,CAAA;AAEvB,MAAM,CAAC,MAAM,QAAQ,GAA2B,GAAG,CAAC,MAAM,EAAE,CAAA;AAY5D,MAAM,CAAC,MAAM,KAAK,GAAyB,GAAG,CAAC,KAAc,CAAA;AAE7D,MAAM,CAAC,MAAM,IAAI,GAAoB,GAAG,CAAC,IAAI,CAAA;AAE7C,qBAAqB;AACrB,MAAM,CAAC,MAAM,KAAK,GAAqB,GAAG,CAAC,KAAc,CAAA;AAQzD,MAAM,CAAC,MAAM,MAAM,GAA0B,GAAG,CAAC,MAAM,CAAA;AAEvD,MAAM,CAAC,MAAM,OAAO,GAA2B,GAAG,CAAC,OAAO,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=arkConfig.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arkConfig.d.ts","sourceRoot":"","sources":["../arkConfig.ts"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ /* eslint-disable @typescript-eslint/no-restricted-imports */
2
+ import { configure } from "@arktype/schema/config";
3
+ // import { type } from "arktype"
4
+ // const user = type("string")
5
+ configure({
6
+ domain: {
7
+ description: (inner) => `my special ${inner.domain}`
8
+ }
9
+ });
10
+ //# sourceMappingURL=arkConfig.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arkConfig.js","sourceRoot":"","sources":["../arkConfig.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAClD,iCAAiC;AAEjC,8BAA8B;AAE9B,SAAS,CAAC;IACT,MAAM,EAAE;QACP,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,cAAc,KAAK,CAAC,MAAM,EAAE;KACpD;CACD,CAAC,CAAA"}
package/out/main.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export { ArkErrors as ArkErrors, type ArkTypeError as ArkTypeError } from "@arktype/schema";
2
+ export type { Out, is } from "@arktype/schema";
3
+ export { ark, arktypes, declare, define, match, scope, type, type Ark } from "./ark.js";
4
+ export type { Module, Scope } from "./scope.js";
5
+ export { Type } from "./type.js";
6
+ export type { inferTypeRoot, validateTypeRoot } from "./type.js";
7
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,IAAI,SAAS,EACtB,KAAK,YAAY,IAAI,YAAY,EACjC,MAAM,iBAAiB,CAAA;AACxB,YAAY,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EACN,GAAG,EACH,QAAQ,EACR,OAAO,EACP,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EACJ,KAAK,GAAG,EACR,MAAM,UAAU,CAAA;AACjB,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAC/C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,YAAY,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAA"}
package/out/main.js ADDED
@@ -0,0 +1,4 @@
1
+ export { ArkErrors as ArkErrors } from "@arktype/schema";
2
+ export { ark, arktypes, declare, define, match, scope, type } from "./ark.js";
3
+ export { Type } from "./type.js";
4
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.js","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,IAAI,SAAS,EAEtB,MAAM,iBAAiB,CAAA;AAExB,OAAO,EACN,GAAG,EACH,QAAQ,EACR,OAAO,EACP,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EAEJ,MAAM,UAAU,CAAA;AAEjB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA"}
package/out/match.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ import type { Fn, isDisjoint, replaceKey, returnOf, unionToTuple, valueOf } from "@arktype/util";
2
+ import type { Scope } from "./scope.js";
3
+ import { type inferTypeRoot, type validateTypeRoot } from "./type.js";
4
+ type MatchContext = {
5
+ inConstraint: unknown;
6
+ outConstraint: unknown;
7
+ thens: readonly Fn[];
8
+ $: unknown;
9
+ };
10
+ type validateCases<cases, ctx extends MatchContext> = {
11
+ [k in keyof cases | keyof ctx["$"] | "default"]?: k extends "default" ? (In: ctx["inConstraint"]) => ctx["outConstraint"] : k extends validateTypeRoot<k, ctx["$"]> ? (In: ctx["inConstraint"] & inferTypeRoot<k, ctx["$"]>) => ctx["outConstraint"] : validateTypeRoot<k, ctx["$"]>;
12
+ };
13
+ type errorCases<cases, ctx extends MatchContext> = {
14
+ [k in keyof cases]?: k extends "default" ? (In: ctx["inConstraint"]) => ctx["outConstraint"] : k extends validateTypeRoot<k, ctx["$"]> ? (In: ctx["inConstraint"] & inferTypeRoot<k, ctx["$"]>) => ctx["outConstraint"] : validateTypeRoot<k, ctx["$"]>;
15
+ } & {
16
+ [k in Exclude<keyof ctx["$"], keyof cases>]?: (In: ctx["inConstraint"] & ctx["$"][k]) => ctx["outConstraint"];
17
+ } & {
18
+ default?: (In: ctx["inConstraint"]) => ctx["outConstraint"];
19
+ };
20
+ export type CaseMatchParser<ctx extends MatchContext> = {
21
+ <cases>(def: cases extends validateCases<cases, ctx> ? cases : errorCases<cases, ctx>): ChainableMatchParser<replaceKey<ctx, "thens", [...ctx["thens"], ...unionToTuple<valueOf<cases>>]>>;
22
+ };
23
+ export type MatchParser<$> = CaseMatchParser<{
24
+ inConstraint: unknown;
25
+ outConstraint: unknown;
26
+ thens: [];
27
+ $: $;
28
+ }> & {
29
+ <In = unknown, Out = unknown>(): ChainableMatchParser<{
30
+ inConstraint: In;
31
+ outConstraint: Out;
32
+ thens: [];
33
+ $: $;
34
+ }>;
35
+ };
36
+ export type WhenMatchParser<ctx extends MatchContext> = <def, then extends (In: ctx["inConstraint"] & inferTypeRoot<def, ctx["$"]>) => ctx["outConstraint"]>(when: validateTypeRoot<def, ctx["$"]>, then: then) => ChainableMatchParser<replaceKey<ctx, "thens", [...ctx["thens"], then]>>;
37
+ export type MatchInvocation<ctx extends MatchContext> = <data extends ctx["inConstraint"]>(data: data) => {
38
+ [i in Extract<keyof ctx["thens"], `${number}`>]: ctx["thens"][i] extends Fn<[
39
+ infer In
40
+ ], infer Out> ? isDisjoint<data, In> extends true ? never : Out : returnOf<ctx["thens"][i]>;
41
+ }[Extract<keyof ctx["thens"], `${number}`>];
42
+ export type ChainableMatchParser<ctx extends MatchContext> = MatchInvocation<ctx> & {
43
+ cases: CaseMatchParser<ctx>;
44
+ when: WhenMatchParser<ctx>;
45
+ };
46
+ export declare const createMatchParser: <$>(scope: Scope) => MatchParser<$>;
47
+ export {};
48
+ //# sourceMappingURL=match.d.ts.map