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,32 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { type } from "arktype"
3
- import { writeInvalidDateMessage } from "../parser/string/shift/operand/date.js"
4
-
5
- describe("date literal", () => {
6
- it("base", () => {
7
- const t = type("d'2000/05/05'")
8
- attest<Date>(t.infer)
9
- attest(t.allows(new Date("2000/05/05"))).equals(true)
10
- attest(t.allows(new Date("2000/06/05"))).equals(false)
11
- attest(t.allows(new Date("2000/05/05T09:00:00.00Z"))).equals(false)
12
- })
13
- it("with punctuation", () => {
14
- const ISO = type("d'2000-05-05T04:00:00.000Z'")
15
- attest<Date>(ISO.infer)
16
- attest(ISO.allows(new Date("2000/05/05"))).equals(true)
17
- attest(ISO.allows(new Date("2000/07/05"))).equals(false)
18
- })
19
- it("allows spaces", () => {
20
- const t = type("d' 2021 / 05 / 01 '")
21
- attest(t.allows(new Date("2021/05/01"))).equals(true)
22
- })
23
- it("epoch", () => {
24
- const now = new Date()
25
- const t = type(`d'${now.valueOf()}'`)
26
- attest(t.allows(now)).equals(true)
27
- attest(t.allows(new Date(now.valueOf() + 1)))
28
- })
29
- it("invalid date", () => {
30
- attest(() => type("d'tuesday'")).throws(writeInvalidDateMessage("tuesday"))
31
- })
32
- })
@@ -1,124 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { declare, type } from "arktype"
3
-
4
- describe("declared", () => {
5
- it("shallow", () => {
6
- const shallow = declare<number>().type("number")
7
- attest<number>(shallow.infer)
8
- attest(shallow.json).equals(type("number").json)
9
- })
10
-
11
- it("obj", () => {
12
- type Expected = { a: string; b?: number }
13
- const t = declare<Expected>().type({
14
- a: "string",
15
- "b?": "number"
16
- })
17
- attest<Expected>(t.infer)
18
- })
19
-
20
- it("tuple", () => {
21
- type Expected = [string, number]
22
- const t = declare<Expected>().type(["string", "number"])
23
- attest<Expected>(t.infer)
24
- })
25
-
26
- it("bad element", () => {
27
- attest(
28
- // @ts-expect-error
29
- declare<[string, number]>().type(["string", "boolean"])
30
- ).type.errors(
31
- `Type 'string' is not assignable to type '{ declared: number; inferred: boolean; }'`
32
- )
33
- })
34
-
35
- it("too short", () => {
36
- // @ts-expect-error
37
- attest(declare<[string, number]>().type(["string"])).type.errors(
38
- `Source has 1 element(s) but target requires 2`
39
- )
40
- })
41
-
42
- it("too long", () => {
43
- attest(
44
- // @ts-expect-error
45
- declare<[string, number]>().type(["string", "number", "number"])
46
- ).type.errors(`Source has 3 element(s) but target requires 2`)
47
- })
48
-
49
- it("tuple expression", () => {
50
- const t = declare<0 | 1>().type(["0", "|", "1"])
51
- attest<0 | 1>(t.infer)
52
- })
53
-
54
- // TODO: figure out narrowing
55
- // it("regexp", () => {
56
- // const t = declare<string>().type(/.*/)
57
- // attest<string>(t.infer)
58
- // })
59
-
60
- it("Inferred<t>", () => {
61
- const foo = type("'foo'")
62
- const t = declare<"foo">().type(foo)
63
- attest<"foo">(t.infer)
64
- })
65
-
66
- it("bad tuple expression", () => {
67
- attest(
68
- // @ts-expect-error
69
- declare<"foo" | "bar">().type(["'foo'", "|", "'baz'"])
70
- ).type.errors(`{ declared: "foo" | "bar"; inferred: "foo" | "baz"; }`)
71
- })
72
-
73
- it("narrower", () => {
74
- // @ts-expect-error
75
- attest(() => declare<string>().type("'foo'")).type.errors(
76
- `Argument of type 'string' is not assignable to parameter of type '{ declared: string; inferred: "foo"; }'`
77
- )
78
- })
79
-
80
- it("wider", () => {
81
- attest(() =>
82
- declare<{ a: string }>().type({
83
- // @ts-expect-error
84
- a: "unknown"
85
- })
86
- ).type.errors(
87
- `Type 'string' is not assignable to type '{ declared: string; inferred: unknown; }'`
88
- )
89
- })
90
-
91
- it("missing key", () => {
92
- attest(() =>
93
- // @ts-expect-error
94
- declare<{ a: string; b: number }>().type({
95
- a: "string"
96
- })
97
- ).type.errors(
98
- `Property 'b' is missing in type '{ a: "string"; }' but required in type '{ a: "string"; "b?": unknown; }'.`
99
- )
100
- })
101
-
102
- it("missing optional key", () => {
103
- attest(() =>
104
- // @ts-expect-error
105
- declare<{ a: string; b?: number }>().type({
106
- a: "string"
107
- })
108
- ).type.errors(
109
- `Property '"b?"' is missing in type '{ a: "string"; }' but required in type '{ a: "string"; "b?": unknown; }'.`
110
- )
111
- })
112
-
113
- it("extraneous key", () => {
114
- attest(() =>
115
- declare<{ a: string }>().type({
116
- a: "string",
117
- // @ts-expect-error
118
- b: "boolean"
119
- })
120
- ).type.errors(
121
- `Object literal may only specify known properties, and 'b' does not exist in type '{ a: "string"; }'.`
122
- )
123
- })
124
- })
@@ -1,74 +0,0 @@
1
- // import { attest } from "@arktype/attest"
2
- // import { scope, type } from "arktype"
3
-
4
- describe("discrimination", () => {
5
- // it("2 literal branches", () => {
6
- // // should not use a switch with <=2 branches to avoid visual clutter
7
- // const t = type("'a'|'b'")
8
- // attest(t.json).snap({ unit: "a" })
9
- // attest(t.allows("a")).equals(true)
10
- // attest(t.allows("b")).equals(true)
11
- // attest(t.allows("c")).equals(false)
12
- // })
13
- // it(">2 literal branches", () => {
14
- // const t = type("'a'|'b'|'c'")
15
- // attest(t.json).snap({ unit: "a" })
16
- // attest(t.allows("a")).equals(true)
17
- // attest(t.allows("b")).equals(true)
18
- // attest(t.allows("c")).equals(true)
19
- // attest(t.allows("d")).equals(false)
20
- // })
21
- // const getPlaces = () =>
22
- // scope({
23
- // rainForest: {
24
- // climate: "'wet'",
25
- // color: "'green'",
26
- // isRainForest: "true"
27
- // },
28
- // desert: { climate: "'dry'", color: "'brown'", isDesert: "true" },
29
- // sky: { climate: "'dry'", color: "'blue'", isSky: "true" },
30
- // ocean: { climate: "'wet'", color: "'blue'", isOcean: "true" }
31
- // })
32
- // it("nested", () => {
33
- // const t = getPlaces().type("ocean|sky|rainForest|desert")
34
- // attest(t.json).snap()
35
- // })
36
- // it("undiscriminable", () => {
37
- // const t = getPlaces().type([
38
- // "ocean",
39
- // "|",
40
- // {
41
- // climate: "'wet'",
42
- // color: "'blue'",
43
- // indistinguishableFrom: "ocean"
44
- // }
45
- // ])
46
- // })
47
- // it("doesn't discriminate optional key", () => {
48
- // const t = type({
49
- // direction: "'forward' | 'backward'",
50
- // "operator?": "'by'"
51
- // }).or({
52
- // duration: "'s' | 'min' | 'h'",
53
- // operator: "'to'"
54
- // })
55
- // attest(t.root.hasKind("union") && t.root.discriminant).equals(null)
56
- // })
57
- // it("default case", () => {
58
- // const t = getPlaces().type([
59
- // "ocean|rainForest",
60
- // "|",
61
- // { temperature: "'hot'" }
62
- // ])
63
- // })
64
- // it("discriminable default", () => {
65
- // const t = getPlaces().type([
66
- // { temperature: "'cold'" },
67
- // "|",
68
- // ["ocean|rainForest", "|", { temperature: "'hot'" }]
69
- // ])
70
- // })
71
- // it("won't discriminate between possibly empty arrays", () => {
72
- // const t = type("string[]|boolean[]")
73
- // })
74
- })
@@ -1,90 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { writeIndivisibleMessage } from "@arktype/schema"
3
- import { type } from "arktype"
4
- import { writeInvalidDivisorMessage } from "../parser/string/shift/operator/divisor.js"
5
-
6
- describe("divisibility", () => {
7
- describe("parse", () => {
8
- it("integer literal", () => {
9
- const divisibleByTwo = type("number%2")
10
- attest<number>(divisibleByTwo.infer)
11
- attest(divisibleByTwo.json).snap({ basis: "number", divisor: 2 })
12
- })
13
- it("whitespace after %", () => {
14
- const t = type("number % 5")
15
- attest<number>(t.infer)
16
- attest(t.json).snap({ basis: "number", divisor: 5 })
17
- })
18
- it("with bound", () => {
19
- const t = type("number%8<3")
20
- attest(t.json).equals(type("number%8").and("number<3").json)
21
- attest(t.root.description).snap(
22
- "a number and a multiple of 8 and less than 3"
23
- )
24
- })
25
- it("allows non-narrowed divisor", () => {
26
- const d = 5 as number
27
- attest<number>(type(`number%${d}`).infer)
28
- })
29
- it("fails at runtime on non-integer divisor", () => {
30
- attest(() => type("number%2.3")).throws(writeInvalidDivisorMessage("2.3"))
31
- })
32
- it("non-numeric divisor", () => {
33
- // @ts-expect-error
34
- attest(() => type("number%foobar")).throwsAndHasTypeError(
35
- writeInvalidDivisorMessage("foobar")
36
- )
37
- })
38
- it("zero divisor", () => {
39
- // @ts-expect-error
40
- attest(() => type("number%0")).throwsAndHasTypeError(
41
- writeInvalidDivisorMessage(0)
42
- )
43
- })
44
- it("unknown", () => {
45
- // @ts-expect-error
46
- attest(() => type("unknown%2")).throwsAndHasTypeError(
47
- writeIndivisibleMessage("unknown")
48
- )
49
- })
50
- it("indivisible", () => {
51
- // @ts-expect-error
52
- attest(() => type("string%1")).throwsAndHasTypeError(
53
- writeIndivisibleMessage("string")
54
- )
55
- })
56
- it("overlapping", () => {
57
- // @ts-expect-error
58
- attest(() => type("(number|string)%10"))
59
- .throws("Divisibility operand string must be a number")
60
- .type.errors("Divisibility operand number | string must be a number")
61
- })
62
- })
63
- describe("intersection", () => {
64
- it("identical", () => {
65
- const t = type("number%2&number%2")
66
- attest(t.json).equals(type("number%2").json)
67
- })
68
- it("purely divisible", () => {
69
- const t = type("number%4&number%2")
70
- attest(t.json).equals(type("number%4").json)
71
- })
72
- it("common divisor", () => {
73
- const t = type("number%6&number%4")
74
- attest(t.json).equals(type("number%12").json)
75
- })
76
- it("relatively prime", () => {
77
- const t = type("number%2&number%3")
78
- attest(t.json).equals(type("number%6").json)
79
- })
80
- it("valid literal", () => {
81
- const t = type("number%5&0")
82
- attest(t.json).equals(type("0").json)
83
- })
84
- it("invalid literal", () => {
85
- attest(() => type("number%3&8")).throws(
86
- "Intersection of (a multiple of 3) and 8 results in an unsatisfiable type"
87
- )
88
- })
89
- })
90
- })
@@ -1,93 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { type } from "arktype"
3
- import { writeUnterminatedEnclosedMessage } from "../parser/string/shift/operand/enclosed.js"
4
-
5
- describe("parse enclosed", () => {
6
- it("with spaces", () => {
7
- const t = type("'this has spaces'")
8
- attest<"this has spaces">(t.infer)
9
- attest(t.json).snap({ unit: "this has spaces" })
10
- })
11
- it("with neighbors", () => {
12
- const t = type("'foo'|/.*/[]")
13
- attest<"foo" | string[]>(t.infer)
14
- attest(t.json).snap([{ unit: "foo" }, { basis: "Array" }])
15
- })
16
- describe("errors", () => {
17
- describe("unterminated", () => {
18
- it("regex", () => {
19
- // @ts-expect-error
20
- attest(() => type("/.*")).throwsAndHasTypeError(
21
- writeUnterminatedEnclosedMessage(".*", "/")
22
- )
23
- })
24
- it("single-quote", () => {
25
- // @ts-expect-error
26
- attest(() => type("'.*")).throwsAndHasTypeError(
27
- writeUnterminatedEnclosedMessage(".*", "'")
28
- )
29
- })
30
- it("double-quote", () => {
31
- // @ts-expect-error
32
- attest(() => type('".*')).throwsAndHasTypeError(
33
- writeUnterminatedEnclosedMessage(".*", '"')
34
- )
35
- })
36
- })
37
- })
38
- it("single-quoted", () => {
39
- attest<"hello">(type("'hello'").infer)
40
- })
41
- it("double-quoted", () => {
42
- attest<"goodbye">(type('"goodbye"').infer)
43
- })
44
- it("regex literal", () => {
45
- attest<string>(type("/.*/").infer)
46
- })
47
- it("invalid regex", () => {
48
- attest(() => type("/[/")).throws(
49
- "Invalid regular expression: /[/: Unterminated character class"
50
- )
51
- })
52
- it("mixed quote types", () => {
53
- attest<"'single-quoted'">(type(`"'single-quoted'"`).infer)
54
- attest<'"double-quoted"'>(type(`'"double-quoted"'`).infer)
55
- })
56
- it("ignores enclosed operators", () => {
57
- attest<"yes|no|maybe">(type("'yes|no|maybe'").infer)
58
- })
59
- it("mix of enclosed and unenclosed operators", () => {
60
- attest<"yes|no" | "true|false">(type("'yes|no'|'true|false'").infer)
61
- })
62
- it("escaped enclosing", () => {
63
- const t = type("'don\\'t'")
64
- attest<"don't">(t.infer)
65
- })
66
- it("string literal stress", () => {
67
- const s = `"3.
68
- 14159265358979323846264338327950288419716939937510
69
- 58209749445923078164062862089986280348253421170679
70
- 82148086513282306647093844609550582231725359408128
71
- 48111745028410270193852110555964462294895493038196
72
- 44288109756659334461284756482337867831652712019091
73
- 45648566923460348610454326648213393607260249141273
74
- 72458700660631558817488152092096282925409171536436
75
- 78925903600113305305488204665213841469519415116094
76
- 33057270365759591953092186117381932611793105118548
77
- 07446237996274956735188575272489122793818301194912
78
- 98336733624406566430860213949463952247371907021798
79
- 60943702770539217176293176752384674818467669405132
80
- 00056812714526356082778577134275778960917363717872
81
- 14684409012249534301465495853710507922796892589235
82
- 42019956112129021960864034418159813629774771309960
83
- 51870721134999999837297804995105973173281609631859
84
- 50244594553469083026425223082533446850352619311881
85
- 71010003137838752886587533208381420617177669147303
86
- 59825349042875546873115956286388235378759375195778
87
- 185778053217122680661300192"`
88
- // parses exactly 1001 characters before hitting a recursion limit
89
- const t = type(s)
90
- type Expected = typeof s extends `"${infer enclosed}"` ? enclosed : never
91
- attest<Expected>(t.infer)
92
- })
93
- })
@@ -1,56 +0,0 @@
1
- describe("snippets", () => {
2
- // it("demo", async () => {
3
- // const typeSnippet = await import("../../docs/examples/demo.js")
4
- // attest<{>(typeSnippet.pkg.infer)
5
- // name: string
6
- // version: string
7
- // contributors?: string[]
8
- // }
9
- // attest(typeSnippet.errors?.summary).snap(
10
- // "contributors must be more than 1 items long (was 1)"
11
- // )
12
- // })
13
- // it("type", async () => {
14
- // const typeSnippet = await import("./benches/type.js")
15
- // attest<{>(typeSnippet.user.infer)
16
- // name: string
17
- // device: {
18
- // platform: "android" | "ios"
19
- // version?: number
20
- // }
21
- // }
22
- // attest(typeSnippet.errors?.summary).snap(
23
- // "device/platform must be 'android' or 'ios' (was 'enigma')"
24
- // )
25
- // })
26
- // // it("scope", async () => {
27
- // // const scopeSnippet = await import("../examples/scope.js")
28
- // // attest<{>(scopeSnippet.types.package.infer)
29
- // // name: string
30
- // // dependencies?: any[]
31
- // // devDependencies?: any[]
32
- // // contributors?: {
33
- // // email: string
34
- // // packages?: any[]
35
- // // }[]
36
- // // }
37
- // // attest(scopeSnippet.errors?.summary).snap(
38
- // // "dependencies/0/dependencies/0/contributors/0/email must be a valid email (was 'david@sharktypeio')\ncontributors/0/email must be a valid email (was 'david@sharktypeio')"
39
- // // )
40
- // // })
41
- // it("optimized", async () => {
42
- // const example = await import("../../docs/examples/optimized.js")
43
- // attest(example.deepLeftOrRight.infer).typed as
44
- // | {
45
- // auto: {
46
- // discriminated: "left"
47
- // }
48
- // }
49
- // | {
50
- // auto: {
51
- // discriminated: "right"
52
- // }
53
- // }
54
- // attest<number>(example.numericIntersection.infer)
55
- // })
56
- })
@@ -1,138 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { schema } from "@arktype/schema"
3
- import { type, type Out } from "arktype"
4
- import { scope, type Ark } from "../ark.js"
5
- import {
6
- writeMissingRightOperandMessage,
7
- writeUnresolvableMessage
8
- } from "../parser/string/shift/operand/unenclosed.js"
9
- import type { Type } from "../type.js"
10
-
11
- describe("tuple expressions", () => {
12
- it("nested", () => {
13
- const t = type(["string|bigint", "|", ["number", "|", "boolean"]])
14
- attest<string | number | bigint | boolean>(t.infer)
15
- })
16
- it("autocompletion", () => {
17
- // @ts-expect-error
18
- attest(() => type([""])).type.errors(
19
- `IndexZeroOperator | keyof Ark | "this"`
20
- )
21
- // @ts-expect-error
22
- attest(() => type(["string", ""])).type.errors(
23
- `"keyof" | keyof Ark | "this" | IndexOneOperator'`
24
- )
25
- })
26
- describe("errors", () => {
27
- it("missing right operand", () => {
28
- // @ts-expect-error
29
- attest(() => type(["string", "|"])).throwsAndHasTypeError(
30
- writeMissingRightOperandMessage("|", "")
31
- )
32
- // @ts-expect-error
33
- attest(() => type(["string", "&"])).throwsAndHasTypeError(
34
- writeMissingRightOperandMessage("&", "")
35
- )
36
- })
37
- it("nested parse error", () => {
38
- attest(() => {
39
- // @ts-expect-error
40
- type(["string", "|", "numbr"])
41
- }).throwsAndHasTypeError(writeUnresolvableMessage("numbr"))
42
- })
43
- it("nested object parse error", () => {
44
- attest(() => {
45
- // @ts-expect-error
46
- type([{ s: "strng" }, "|", "number"])
47
- }).throwsAndHasTypeError(writeUnresolvableMessage("strng"))
48
- })
49
- // TODO: reenable
50
- it("this", () => {
51
- const t = type([{ a: "string" }, "|", { b: "this" }])
52
- attest(t.infer).type.toString.snap(
53
- "{ a: string; } | { b: { a: string; } | any; }"
54
- )
55
- const types = scope({
56
- a: {
57
- a: "string"
58
- },
59
- b: {
60
- b: "expected"
61
- },
62
- expected: "a|b"
63
- }).export()
64
- attest(t.json).equals(types.expected.json)
65
- })
66
- })
67
- })
68
-
69
- describe("root expression", () => {
70
- it("=== single", () => {
71
- const t = type("===", 5)
72
- attest<5>(t.infer)
73
- attest(t.json).equals(type("5").json)
74
- })
75
- it("=== branches", () => {
76
- const t = type("===", "foo", "bar", "baz")
77
- attest<"foo" | "bar" | "baz">(t.infer)
78
- attest(t.json).equals(schema.units("foo", "bar", "baz").json)
79
- })
80
- it("instanceof single", () => {
81
- const t = type("instanceof", RegExp)
82
- attest<RegExp>(t.infer)
83
- attest(t.json).equals(schema(RegExp).json)
84
- })
85
- it("instanceof branches", () => {
86
- const t = type("instanceof", Array, Date)
87
- attest<unknown[] | Date>(t.infer)
88
- attest(t.json).equals(schema(Array, Date).json)
89
- })
90
- it("postfix", () => {
91
- const t = type({ a: "string" }, "[]")
92
- attest<{ a: string }[]>(t.infer)
93
- attest(t.json).equals(type({ a: "string" }).array().json)
94
- })
95
- it("infix", () => {
96
- const t = type({ a: "string" }, "|", { b: "boolean" })
97
- attest<
98
- | {
99
- a: string
100
- }
101
- | {
102
- b: boolean
103
- }
104
- >(t.infer)
105
-
106
- attest(t.json).equals(type({ a: "string" }).or({ b: "boolean" }).json)
107
- })
108
- it("morph", () => {
109
- const t = type({ a: "string" }, "=>", (In) => ({ b: In.a }))
110
- attest<Type<(In: { a: string }) => Out<{ b: string }>, Ark>>(t)
111
- })
112
- it("narrow", () => {
113
- const t = type(
114
- { a: "string" },
115
- ":",
116
- (In): In is { a: "foo" } => In.a === "foo"
117
- )
118
- attest<{ a: "foo" }>(t.infer)
119
- })
120
- it("this", () => {
121
- const t = type({ a: "string" }, "|", { b: "this" })
122
- attest(t.infer).type.toString.snap(
123
- "{ a: string; } | { b: { a: string; } | any; }"
124
- )
125
- attest(t.json).equals(type([{ a: "string" }, "|", { b: "this" }]).json)
126
- })
127
- it("tuple as second arg", () => {
128
- // this case is not fundamentally unique but TS has a hard time
129
- // narrowing tuples in contexts like this
130
- const t = type("keyof", [
131
- { a: "string" },
132
- "&",
133
- { b: "boolean" }
134
- // as const is required for TS <=5.0
135
- ] as const)
136
- attest<"a" | "b">(t.infer)
137
- })
138
- })