arktype 2.0.0-dev.2 → 2.0.0-dev.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/LICENSE +7 -0
  2. package/out/api.d.ts +6 -0
  3. package/out/api.js +5 -0
  4. package/out/ark.d.ts +15 -39
  5. package/out/ark.js +9 -18
  6. package/out/config.d.ts +1 -0
  7. package/out/config.js +2 -0
  8. package/out/generic.d.ts +21 -0
  9. package/out/generic.js +24 -0
  10. package/out/match.d.ts +18 -24
  11. package/out/match.js +50 -60
  12. package/out/module.d.ts +11 -0
  13. package/out/module.js +2 -0
  14. package/out/parser/definition.d.ts +13 -15
  15. package/out/parser/definition.js +10 -13
  16. package/out/parser/generic.d.ts +5 -7
  17. package/out/parser/generic.js +7 -11
  18. package/out/parser/objectLiteral.d.ts +57 -18
  19. package/out/parser/objectLiteral.js +93 -52
  20. package/out/parser/semantic/bounds.d.ts +10 -8
  21. package/out/parser/semantic/bounds.js +0 -1
  22. package/out/parser/semantic/divisor.d.ts +5 -7
  23. package/out/parser/semantic/divisor.js +0 -1
  24. package/out/parser/semantic/infer.d.ts +24 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +3 -7
  27. package/out/parser/semantic/utils.js +0 -1
  28. package/out/parser/semantic/validate.d.ts +16 -13
  29. package/out/parser/semantic/validate.js +1 -1
  30. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  31. package/out/parser/string/reduce/dynamic.js +31 -26
  32. package/out/parser/string/reduce/shared.d.ts +14 -28
  33. package/out/parser/string/reduce/shared.js +4 -3
  34. package/out/parser/string/reduce/static.d.ts +26 -23
  35. package/out/parser/string/reduce/static.js +0 -1
  36. package/out/parser/string/shift/operand/date.d.ts +2 -2
  37. package/out/parser/string/shift/operand/date.js +4 -7
  38. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  39. package/out/parser/string/shift/operand/enclosed.js +14 -14
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  41. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  42. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  43. package/out/parser/string/shift/operand/operand.js +9 -13
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -20
  45. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  46. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  47. package/out/parser/string/shift/operator/bounds.js +45 -56
  48. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  49. package/out/parser/string/shift/operator/divisor.js +1 -3
  50. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  51. package/out/parser/string/shift/operator/operator.js +12 -19
  52. package/out/parser/string/shift/scanner.d.ts +8 -7
  53. package/out/parser/string/shift/scanner.js +17 -16
  54. package/out/parser/string/string.d.ts +9 -7
  55. package/out/parser/string/string.js +1 -3
  56. package/out/parser/tuple.d.ts +101 -42
  57. package/out/parser/tuple.js +107 -86
  58. package/out/scope.d.ts +44 -115
  59. package/out/scope.js +39 -188
  60. package/out/type.d.ts +63 -61
  61. package/out/type.js +30 -135
  62. package/package.json +33 -39
  63. package/__tests__/array.test.ts +0 -310
  64. package/__tests__/astUtils.test.ts +0 -17
  65. package/__tests__/badDefinitionType.test.ts +0 -51
  66. package/__tests__/basis.test.ts +0 -46
  67. package/__tests__/bounds.test.ts +0 -388
  68. package/__tests__/cast.test.ts +0 -32
  69. package/__tests__/completions.test.ts +0 -46
  70. package/__tests__/config.test.ts +0 -77
  71. package/__tests__/cyclic.test.ts +0 -144
  72. package/__tests__/dateLiteral.test.ts +0 -32
  73. package/__tests__/declared.test.ts +0 -124
  74. package/__tests__/discrimination.test.ts +0 -74
  75. package/__tests__/divisor.test.ts +0 -90
  76. package/__tests__/enclosed.test.ts +0 -93
  77. package/__tests__/examples.test.ts +0 -56
  78. package/__tests__/expressions.test.ts +0 -138
  79. package/__tests__/generateBenchData.ts +0 -89
  80. package/__tests__/generated/cyclic.ts +0 -3058
  81. package/__tests__/generic.test.ts +0 -294
  82. package/__tests__/group.test.ts +0 -72
  83. package/__tests__/hkt.test.ts +0 -22
  84. package/__tests__/imports.test.ts +0 -113
  85. package/__tests__/instanceof.test.ts +0 -102
  86. package/__tests__/intersection.test.ts +0 -104
  87. package/__tests__/jsdoc.test.ts +0 -64
  88. package/__tests__/keyTraversal.test.ts +0 -74
  89. package/__tests__/keyof.test.ts +0 -134
  90. package/__tests__/keywords.test.ts +0 -188
  91. package/__tests__/literal.test.ts +0 -51
  92. package/__tests__/match.bench.ts +0 -31
  93. package/__tests__/match.test.ts +0 -224
  94. package/__tests__/methods.bench.ts +0 -22
  95. package/__tests__/morph.test.ts +0 -409
  96. package/__tests__/narrow.test.ts +0 -102
  97. package/__tests__/nodeDefinitions.test.ts +0 -190
  98. package/__tests__/object.bench.ts +0 -28
  99. package/__tests__/objectLiteral.test.ts +0 -301
  100. package/__tests__/operand.bench.ts +0 -45
  101. package/__tests__/operator.bench.ts +0 -130
  102. package/__tests__/regex.test.ts +0 -58
  103. package/__tests__/scope.bench.ts +0 -39
  104. package/__tests__/scope.test.ts +0 -140
  105. package/__tests__/string.test.ts +0 -48
  106. package/__tests__/submodule.test.ts +0 -99
  107. package/__tests__/this.test.ts +0 -57
  108. package/__tests__/thunk.test.ts +0 -80
  109. package/__tests__/traverse.test.ts +0 -119
  110. package/__tests__/tsGenerics.test.ts +0 -12
  111. package/__tests__/typeMethods.test.ts +0 -69
  112. package/__tests__/typeReference.test.ts +0 -39
  113. package/__tests__/unenclosed.test.ts +0 -120
  114. package/__tests__/union.test.ts +0 -147
  115. package/ark.ts +0 -91
  116. package/main.ts +0 -18
  117. package/match.ts +0 -243
  118. package/out/ark.d.ts.map +0 -1
  119. package/out/ark.js.map +0 -1
  120. package/out/arkConfig.d.ts +0 -2
  121. package/out/arkConfig.d.ts.map +0 -1
  122. package/out/arkConfig.js +0 -10
  123. package/out/arkConfig.js.map +0 -1
  124. package/out/main.d.ts +0 -7
  125. package/out/main.d.ts.map +0 -1
  126. package/out/main.js +0 -4
  127. package/out/main.js.map +0 -1
  128. package/out/match.d.ts.map +0 -1
  129. package/out/match.js.map +0 -1
  130. package/out/parser/definition.d.ts.map +0 -1
  131. package/out/parser/definition.js.map +0 -1
  132. package/out/parser/generic.d.ts.map +0 -1
  133. package/out/parser/generic.js.map +0 -1
  134. package/out/parser/objectLiteral.d.ts.map +0 -1
  135. package/out/parser/objectLiteral.js.map +0 -1
  136. package/out/parser/semantic/bounds.d.ts.map +0 -1
  137. package/out/parser/semantic/bounds.js.map +0 -1
  138. package/out/parser/semantic/divisor.d.ts.map +0 -1
  139. package/out/parser/semantic/divisor.js.map +0 -1
  140. package/out/parser/semantic/intersections.d.ts +0 -14
  141. package/out/parser/semantic/intersections.d.ts.map +0 -1
  142. package/out/parser/semantic/intersections.js +0 -2
  143. package/out/parser/semantic/intersections.js.map +0 -1
  144. package/out/parser/semantic/semantic.d.ts +0 -33
  145. package/out/parser/semantic/semantic.d.ts.map +0 -1
  146. package/out/parser/semantic/semantic.js +0 -2
  147. package/out/parser/semantic/semantic.js.map +0 -1
  148. package/out/parser/semantic/utils.d.ts.map +0 -1
  149. package/out/parser/semantic/utils.js.map +0 -1
  150. package/out/parser/semantic/validate.d.ts.map +0 -1
  151. package/out/parser/semantic/validate.js.map +0 -1
  152. package/out/parser/shared.d.ts +0 -56
  153. package/out/parser/shared.d.ts.map +0 -1
  154. package/out/parser/shared.js +0 -55
  155. package/out/parser/shared.js.map +0 -1
  156. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  157. package/out/parser/string/reduce/dynamic.js.map +0 -1
  158. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  159. package/out/parser/string/reduce/shared.js.map +0 -1
  160. package/out/parser/string/reduce/static.d.ts.map +0 -1
  161. package/out/parser/string/reduce/static.js.map +0 -1
  162. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/date.js.map +0 -1
  164. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  166. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  168. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/operand.js.map +0 -1
  170. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  171. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  172. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  174. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  176. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  177. package/out/parser/string/shift/operator/operator.js.map +0 -1
  178. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  179. package/out/parser/string/shift/scanner.js.map +0 -1
  180. package/out/parser/string/string.d.ts.map +0 -1
  181. package/out/parser/string/string.js.map +0 -1
  182. package/out/parser/tuple.d.ts.map +0 -1
  183. package/out/parser/tuple.js.map +0 -1
  184. package/out/scope.d.ts.map +0 -1
  185. package/out/scope.js.map +0 -1
  186. package/out/test.d.ts +0 -2
  187. package/out/test.d.ts.map +0 -1
  188. package/out/test.js +0 -7
  189. package/out/test.js.map +0 -1
  190. package/out/tsconfig.build.tsbuildinfo +0 -1
  191. package/out/type.d.ts.map +0 -1
  192. package/out/type.js.map +0 -1
  193. package/out/util.d.ts +0 -19
  194. package/out/util.d.ts.map +0 -1
  195. package/out/util.js +0 -7
  196. package/out/util.js.map +0 -1
  197. package/parser/definition.ts +0 -159
  198. package/parser/generic.ts +0 -68
  199. package/parser/objectLiteral.ts +0 -197
  200. package/parser/semantic/bounds.ts +0 -61
  201. package/parser/semantic/divisor.ts +0 -13
  202. package/parser/semantic/intersections.ts +0 -46
  203. package/parser/semantic/semantic.ts +0 -143
  204. package/parser/semantic/utils.ts +0 -26
  205. package/parser/semantic/validate.ts +0 -109
  206. package/parser/shared.ts +0 -167
  207. package/parser/string/reduce/dynamic.ts +0 -196
  208. package/parser/string/reduce/shared.ts +0 -101
  209. package/parser/string/reduce/static.ts +0 -298
  210. package/parser/string/shift/operand/date.ts +0 -54
  211. package/parser/string/shift/operand/enclosed.ts +0 -118
  212. package/parser/string/shift/operand/genericArgs.ts +0 -127
  213. package/parser/string/shift/operand/operand.ts +0 -48
  214. package/parser/string/shift/operand/unenclosed.ts +0 -268
  215. package/parser/string/shift/operator/bounds.ts +0 -264
  216. package/parser/string/shift/operator/divisor.ts +0 -36
  217. package/parser/string/shift/operator/operator.ts +0 -82
  218. package/parser/string/shift/scanner.ts +0 -209
  219. package/parser/string/string.ts +0 -90
  220. package/parser/tuple.ts +0 -475
  221. package/scope.ts +0 -548
  222. package/type.ts +0 -349
  223. package/util.ts +0 -36
@@ -1,119 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { scope, type } from "arktype"
3
-
4
- describe("traverse", () => {
5
- it("divisible", () => {
6
- const t = type("number%2")
7
- attest(t(4).out).snap(4)
8
- attest(t(5).errors?.summary).snap("Must be a multiple of 2 (was 5)")
9
- })
10
- it("range", () => {
11
- const t = type("number>2")
12
- attest(t(3).out).snap(3)
13
- attest(t(2).errors?.summary).snap("Must be more than 2 (was 2)")
14
- })
15
- it("domain", () => {
16
- const t = type("number")
17
- attest(t(5).out).snap(5)
18
- attest(t("foo").errors?.summary).snap("Must be a number (was string)")
19
- })
20
- it("regex", () => {
21
- const t = type("/.*@arktype.io/")
22
- attest(t("shawn@arktype.io").out).snap("shawn@arktype.io")
23
- attest(t("shawn@hotmail.com").errors?.summary).snap(
24
- "Must be a string matching /.*@arktype.io/ (was 'shawn@hotmail.com')"
25
- )
26
- })
27
- it("required keys", () => {
28
- const t = type({
29
- name: "string",
30
- age: "number",
31
- "title?": "string"
32
- })
33
- attest(t({ name: "Shawn", age: 99 }).out).snap({
34
- name: "Shawn",
35
- age: 99
36
- })
37
- attest(t({ name: "Shawn" }).errors?.summary).snap("age must be defined")
38
- })
39
- it("customized builtin problem", () => {
40
- const types = scope(
41
- { isEven: "number%2" }
42
- //{
43
- // TODO: Fix
44
- // codes: {
45
- // divisor: {
46
- // mustBe: (divisor) => `a multiple of ${divisor}`,
47
- // writeReason: (mustBe, was) => `${was} is not ${mustBe}!`
48
- // }
49
- // }
50
- //}
51
- ).export()
52
- attest(types.isEven(3).errors?.summary).snap("3 is not a multiple of 2!")
53
- })
54
- it("domains", () => {
55
- const t = type("string|number[]")
56
- attest(t([1]).out).snap([1])
57
- attest(t("hello").out).snap("hello")
58
- attest(t(2).errors?.summary).snap("Must be a string or an object (was 2)")
59
- })
60
- it("tuple length", () => {
61
- const t = type(["string", "number", "string", "string[]"])
62
- const data: typeof t.infer = ["foo", 5, "boo", []]
63
- attest(t(data).out).equals(data)
64
- attest(t(["hello"]).errors?.summary).snap("length must be 4 (was 1)")
65
- })
66
- it("branches", () => {
67
- const t = type([{ a: "string" }, "|", { b: "boolean" }])
68
- attest(t({ a: "ok" }).out).snap({ a: "ok" })
69
- attest(t({ b: true }).out).snap({ b: true })
70
- attest(t({}).errors?.summary).snap(
71
- "a must be defined or b must be defined (was {})"
72
- )
73
- })
74
- it("branches at path", () => {
75
- const t = type({ key: [{ a: "string" }, "|", { b: "boolean" }] })
76
- attest(t({ key: { a: "ok" } }).out).snap({ key: { a: "ok" } })
77
- attest(t({ key: { b: true } }).out).snap({ key: { b: true } })
78
- attest(t({ key: {} }).errors?.summary).snap(
79
- "At key, a must be defined or b must be defined (was {})"
80
- )
81
- })
82
- it("switch", () => {
83
- const t = type([{ a: "string" }, "|", { a: "boolean" }])
84
- attest(t({ a: "ok" }).out).snap({ a: "ok" })
85
- attest(t({ a: true }).out).snap({ a: true })
86
- // value isn't present
87
- attest(t({}).errors?.summary).snap(
88
- "a must be a string or boolean (was undefined)"
89
- )
90
- // unsatisfying value
91
- attest(t({ a: 5 }).errors?.summary).snap(
92
- "a must be a string or boolean (was 5)"
93
- )
94
- })
95
- it("multiple switch", () => {
96
- const types = scope({
97
- a: { a: "string" },
98
- b: { a: "number" },
99
- c: { a: "Function" },
100
- d: "a|b|c"
101
- }).export()
102
- attest(types.d({}).errors?.summary).snap(
103
- "a must be a string, a number or an object (was undefined)"
104
- )
105
- })
106
-
107
- it("multi", () => {
108
- const naturalNumber = type("integer>0")
109
- attest(naturalNumber(-1.2).errors?.summary).snap(
110
- "-1.2 must be...\n• an integer\n• more than 0"
111
- )
112
- const naturalAtPath = type({
113
- natural: naturalNumber
114
- })
115
- attest(naturalAtPath({ natural: -0.1 }).errors?.summary).snap(
116
- "At natural, -0.1 must be...\n• an integer\n• more than 0"
117
- )
118
- })
119
- })
@@ -1,12 +0,0 @@
1
- describe("tsGenerics", () => {
2
- // it("in scope", () => {
3
- // const types = scope({
4
- // t: "Record<string, number>"
5
- // }).export()
6
- // attest<Record<string, number>>(types.t.infer)
7
- // })
8
- // it("in module", () => {
9
- // const t = arktypes.Record("string", "number")
10
- // attest<Record<string, number>>(t.infer)
11
- // })
12
- })
@@ -1,69 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { ArkTypeError } from "@arktype/schema"
3
- import { define, scope, type } from "arktype"
4
- import { AssertionError } from "node:assert"
5
- import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
6
-
7
- describe("type methods", () => {
8
- it("root discriminates", () => {
9
- const t = type("string")
10
- const { out, errors: errors } = t("")
11
- if (errors) {
12
- errors.throw()
13
- } else {
14
- attest<string>(out)
15
- }
16
- })
17
- it("allows", () => {
18
- const t = type("number%2")
19
- const data: unknown = 4
20
- if (t.allows(data)) {
21
- // narrows correctly
22
- attest<number>(data)
23
- } else {
24
- throw new Error()
25
- }
26
- attest(t.allows(5)).equals(false)
27
- })
28
- it("errors can be thrown", () => {
29
- const t = type("number")
30
- try {
31
- attest(t("invalid").errors?.throw())
32
- } catch (e) {
33
- attest(e instanceof ArkTypeError).equals(true)
34
- return
35
- }
36
- throw new AssertionError({ message: "Expected to throw" })
37
- })
38
- })
39
-
40
- describe("scope utilities", () => {
41
- describe("define", () => {
42
- it("ark", () => {
43
- const def = define({
44
- a: "string|number",
45
- b: ["boolean"],
46
- c: "this"
47
- })
48
- attest<{ a: "string|number"; b: ["boolean"]; c: "this" }>(def)
49
- })
50
- it("ark error", () => {
51
- // currently is a no-op, so only has type error
52
- // @ts-expect-error
53
- attest(define({ a: "boolean|foo" })).type.errors(
54
- writeUnresolvableMessage("foo")
55
- )
56
- })
57
- it("custom scope", () => {
58
- const $ = scope({
59
- a: "string[]"
60
- })
61
- const ok = $.define(["a[]|boolean"])
62
- attest<["a[]|boolean"]>(ok)
63
- // @ts-expect-error
64
- attest($.define({ not: "ok" })).type.errors(
65
- writeUnresolvableMessage("ok")
66
- )
67
- })
68
- })
69
- })
@@ -1,39 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { ark, scope, type } from "arktype"
3
- import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
4
-
5
- describe("type references", () => {
6
- it("shallow type reference", () => {
7
- const t = type(type("boolean"))
8
- attest<boolean>(t.infer)
9
- })
10
-
11
- it("bad shallow type reference", () => {
12
- attest(() => {
13
- // @ts-expect-error
14
- type(type("foolean"))
15
- }).throwsAndHasTypeError(writeUnresolvableMessage("foolean"))
16
- })
17
-
18
- it("deep type reference", () => {
19
- const t = type({ a: type("boolean") })
20
- attest<{ a: boolean }>(t.infer)
21
- })
22
-
23
- it("type reference in scope", () => {
24
- const a = type({ a: "string" })
25
- const $ = scope({ a })
26
- const types = $.export()
27
- attest(types.a.json).equals(type({ a: "string" }).json)
28
- attest(a.scope).is(ark)
29
- attest(types.a.scope).is($)
30
- attest<{ a: string }>(types.a.infer)
31
- })
32
-
33
- it("bad deep type reference", () => {
34
- attest(() => {
35
- // @ts-expect-error
36
- type({ a: type("goolean") })
37
- }).throwsAndHasTypeError(writeUnresolvableMessage("goolean"))
38
- })
39
- })
@@ -1,120 +0,0 @@
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
- })
@@ -1,147 +0,0 @@
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 DELETED
@@ -1,91 +0,0 @@
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 DELETED
@@ -1,18 +0,0 @@
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"