arktype 2.0.0-dev.1 → 2.0.0-dev.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/__tests__/array.test.ts +270 -151
  2. package/__tests__/badDefinitionType.test.ts +10 -3
  3. package/__tests__/basis.test.ts +6 -2
  4. package/__tests__/bounds.test.ts +282 -352
  5. package/__tests__/cast.test.ts +7 -2
  6. package/__tests__/completions.test.ts +8 -3
  7. package/__tests__/config.test.ts +95 -167
  8. package/__tests__/dateLiteral.test.ts +8 -4
  9. package/__tests__/declared.test.ts +7 -6
  10. package/__tests__/discrimination.test.ts +1 -1
  11. package/__tests__/divisor.test.ts +46 -22
  12. package/__tests__/enclosed.test.ts +39 -25
  13. package/__tests__/examples.test.ts +56 -56
  14. package/__tests__/expressions.test.ts +184 -112
  15. package/__tests__/generic.test.ts +307 -288
  16. package/__tests__/group.test.ts +67 -54
  17. package/__tests__/imports.test.ts +62 -49
  18. package/__tests__/instanceof.test.ts +25 -23
  19. package/__tests__/intersection.test.ts +52 -48
  20. package/__tests__/keyTraversal.test.ts +77 -73
  21. package/__tests__/keyof.test.ts +68 -52
  22. package/__tests__/keywords.test.ts +164 -138
  23. package/__tests__/literal.test.ts +60 -21
  24. package/__tests__/match.bench.ts +31 -0
  25. package/__tests__/match.test.ts +222 -22
  26. package/__tests__/narrow.test.ts +52 -54
  27. package/__tests__/objectLiteral.test.ts +347 -205
  28. package/__tests__/operator.bench.ts +4 -4
  29. package/__tests__/pipe.test.ts +501 -0
  30. package/__tests__/realWorld.test.ts +63 -0
  31. package/__tests__/regex.test.ts +17 -7
  32. package/__tests__/scope.bench.ts +1 -1
  33. package/__tests__/scope.test.ts +257 -23
  34. package/__tests__/serialization.test.ts +19 -0
  35. package/__tests__/string.test.ts +11 -7
  36. package/__tests__/submodule.test.ts +26 -39
  37. package/__tests__/this.test.ts +83 -57
  38. package/__tests__/thunk.test.ts +13 -7
  39. package/__tests__/traverse.test.ts +76 -54
  40. package/__tests__/tsGenerics.test.ts +12 -12
  41. package/__tests__/type.test.ts +36 -0
  42. package/__tests__/typeReference.test.ts +7 -7
  43. package/__tests__/unenclosed.test.ts +116 -100
  44. package/__tests__/union.test.ts +171 -104
  45. package/ark.ts +21 -65
  46. package/generic.ts +69 -0
  47. package/main.ts +6 -18
  48. package/match.ts +195 -93
  49. package/module.ts +15 -0
  50. package/out/ark.d.ts +15 -39
  51. package/out/ark.js +9 -18
  52. package/out/ark.js.map +1 -1
  53. package/out/generic.d.ts +21 -0
  54. package/out/generic.js +24 -0
  55. package/out/generic.js.map +1 -0
  56. package/out/main.d.ts +6 -7
  57. package/out/main.js +4 -3
  58. package/out/main.js.map +1 -1
  59. package/out/match.d.ts +57 -34
  60. package/out/match.js +51 -17
  61. package/out/match.js.map +1 -1
  62. package/out/module.d.ts +10 -0
  63. package/out/module.js +3 -0
  64. package/out/module.js.map +1 -0
  65. package/out/parser/definition.d.ts +11 -13
  66. package/out/parser/definition.js +10 -13
  67. package/out/parser/definition.js.map +1 -1
  68. package/out/parser/generic.d.ts +5 -7
  69. package/out/parser/generic.js +7 -11
  70. package/out/parser/generic.js.map +1 -1
  71. package/out/parser/objectLiteral.d.ts +45 -14
  72. package/out/parser/objectLiteral.js +61 -52
  73. package/out/parser/objectLiteral.js.map +1 -1
  74. package/out/parser/semantic/bounds.d.ts +10 -8
  75. package/out/parser/semantic/bounds.js +0 -1
  76. package/out/parser/semantic/bounds.js.map +1 -1
  77. package/out/parser/semantic/divisor.d.ts +5 -7
  78. package/out/parser/semantic/divisor.js +0 -1
  79. package/out/parser/semantic/infer.d.ts +24 -0
  80. package/out/parser/semantic/infer.js +1 -0
  81. package/out/parser/semantic/infer.js.map +1 -0
  82. package/out/parser/semantic/utils.d.ts +3 -7
  83. package/out/parser/semantic/utils.js +0 -1
  84. package/out/parser/semantic/validate.d.ts +15 -14
  85. package/out/parser/semantic/validate.js +1 -1
  86. package/out/parser/semantic/validate.js.map +1 -1
  87. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  88. package/out/parser/string/reduce/dynamic.js +31 -26
  89. package/out/parser/string/reduce/dynamic.js.map +1 -1
  90. package/out/parser/string/reduce/shared.d.ts +6 -20
  91. package/out/parser/string/reduce/shared.js +0 -1
  92. package/out/parser/string/reduce/shared.js.map +1 -1
  93. package/out/parser/string/reduce/static.d.ts +27 -24
  94. package/out/parser/string/reduce/static.js +0 -1
  95. package/out/parser/string/shift/operand/date.d.ts +3 -3
  96. package/out/parser/string/shift/operand/date.js +4 -7
  97. package/out/parser/string/shift/operand/date.js.map +1 -1
  98. package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
  99. package/out/parser/string/shift/operand/enclosed.js +12 -13
  100. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  101. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  102. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  103. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  104. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  105. package/out/parser/string/shift/operand/operand.js +8 -12
  106. package/out/parser/string/shift/operand/operand.js.map +1 -1
  107. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
  108. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  109. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  110. package/out/parser/string/shift/operator/bounds.d.ts +9 -13
  111. package/out/parser/string/shift/operator/bounds.js +43 -54
  112. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  113. package/out/parser/string/shift/operator/divisor.d.ts +2 -4
  114. package/out/parser/string/shift/operator/divisor.js +1 -3
  115. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  116. package/out/parser/string/shift/operator/operator.d.ts +2 -3
  117. package/out/parser/string/shift/operator/operator.js +12 -19
  118. package/out/parser/string/shift/operator/operator.js.map +1 -1
  119. package/out/parser/string/shift/scanner.d.ts +2 -2
  120. package/out/parser/string/shift/scanner.js +14 -16
  121. package/out/parser/string/shift/scanner.js.map +1 -1
  122. package/out/parser/string/string.d.ts +8 -6
  123. package/out/parser/string/string.js +1 -3
  124. package/out/parser/string/string.js.map +1 -1
  125. package/out/parser/tuple.d.ts +102 -43
  126. package/out/parser/tuple.js +119 -87
  127. package/out/parser/tuple.js.map +1 -1
  128. package/out/scope.d.ts +43 -120
  129. package/out/scope.js +39 -209
  130. package/out/scope.js.map +1 -1
  131. package/out/type.d.ts +43 -64
  132. package/out/type.js +30 -131
  133. package/out/type.js.map +1 -1
  134. package/package.json +7 -3
  135. package/parser/definition.ts +77 -93
  136. package/parser/generic.ts +38 -40
  137. package/parser/objectLiteral.ts +172 -136
  138. package/parser/semantic/bounds.ts +33 -32
  139. package/parser/semantic/divisor.ts +8 -10
  140. package/parser/semantic/infer.ts +141 -0
  141. package/parser/semantic/utils.ts +12 -23
  142. package/parser/semantic/validate.ts +81 -73
  143. package/parser/string/reduce/dynamic.ts +67 -55
  144. package/parser/string/reduce/shared.ts +1 -3
  145. package/parser/string/reduce/static.ts +67 -69
  146. package/parser/string/shift/operand/date.ts +15 -14
  147. package/parser/string/shift/operand/enclosed.ts +30 -25
  148. package/parser/string/shift/operand/genericArgs.ts +46 -45
  149. package/parser/string/shift/operand/operand.ts +32 -38
  150. package/parser/string/shift/operand/unenclosed.ts +107 -158
  151. package/parser/string/shift/operator/bounds.ts +118 -112
  152. package/parser/string/shift/operator/divisor.ts +14 -17
  153. package/parser/string/shift/operator/operator.ts +35 -46
  154. package/parser/string/shift/scanner.ts +51 -50
  155. package/parser/string/string.ts +30 -35
  156. package/parser/tuple.ts +375 -307
  157. package/scope.ts +170 -453
  158. package/type.ts +193 -274
  159. package/__tests__/astUtils.test.ts +0 -17
  160. package/__tests__/cyclic.test.ts +0 -144
  161. package/__tests__/jsdoc.test.ts +0 -64
  162. package/__tests__/morph.test.ts +0 -409
  163. package/__tests__/nodeDefinitions.test.ts +0 -190
  164. package/__tests__/typeMethods.test.ts +0 -69
  165. package/out/ark.d.ts.map +0 -1
  166. package/out/arkConfig.d.ts +0 -2
  167. package/out/arkConfig.d.ts.map +0 -1
  168. package/out/arkConfig.js +0 -10
  169. package/out/arkConfig.js.map +0 -1
  170. package/out/main.d.ts.map +0 -1
  171. package/out/match.d.ts.map +0 -1
  172. package/out/parser/definition.d.ts.map +0 -1
  173. package/out/parser/generic.d.ts.map +0 -1
  174. package/out/parser/objectLiteral.d.ts.map +0 -1
  175. package/out/parser/semantic/bounds.d.ts.map +0 -1
  176. package/out/parser/semantic/divisor.d.ts.map +0 -1
  177. package/out/parser/semantic/intersections.d.ts +0 -14
  178. package/out/parser/semantic/intersections.d.ts.map +0 -1
  179. package/out/parser/semantic/intersections.js +0 -2
  180. package/out/parser/semantic/intersections.js.map +0 -1
  181. package/out/parser/semantic/semantic.d.ts +0 -33
  182. package/out/parser/semantic/semantic.d.ts.map +0 -1
  183. package/out/parser/semantic/semantic.js +0 -2
  184. package/out/parser/semantic/semantic.js.map +0 -1
  185. package/out/parser/semantic/utils.d.ts.map +0 -1
  186. package/out/parser/semantic/validate.d.ts.map +0 -1
  187. package/out/parser/shared.d.ts +0 -54
  188. package/out/parser/shared.d.ts.map +0 -1
  189. package/out/parser/shared.js +0 -47
  190. package/out/parser/shared.js.map +0 -1
  191. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  192. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  193. package/out/parser/string/reduce/static.d.ts.map +0 -1
  194. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  195. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  196. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  197. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  198. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  199. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  200. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  201. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  202. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  203. package/out/parser/string/string.d.ts.map +0 -1
  204. package/out/parser/tuple.d.ts.map +0 -1
  205. package/out/scope.d.ts.map +0 -1
  206. package/out/test.d.ts +0 -2
  207. package/out/test.d.ts.map +0 -1
  208. package/out/test.js +0 -7
  209. package/out/test.js.map +0 -1
  210. package/out/tsconfig.build.tsbuildinfo +0 -1
  211. package/out/type.d.ts.map +0 -1
  212. package/out/util.d.ts +0 -19
  213. package/out/util.d.ts.map +0 -1
  214. package/out/util.js +0 -7
  215. package/out/util.js.map +0 -1
  216. package/parser/semantic/intersections.ts +0 -46
  217. package/parser/semantic/semantic.ts +0 -144
  218. package/parser/shared.ts +0 -152
  219. package/util.ts +0 -36
@@ -1,4 +1,4 @@
1
- import { attest } from "@arktype/attest"
1
+ import { attest, contextualize } from "@arktype/attest"
2
2
  import { type } from "arktype"
3
3
  import {
4
4
  writeUnclosedGroupMessage,
@@ -6,67 +6,80 @@ import {
6
6
  } from "../parser/string/reduce/shared.js"
7
7
  import { writeExpressionExpectedMessage } from "../parser/string/shift/operand/unenclosed.js"
8
8
 
9
- describe("group", () => {
9
+ contextualize(() => {
10
10
  it("entire expression", () => {
11
- attest<string>(type("(string)").infer)
11
+ const t = type("(string)")
12
+ const expected = type("string")
13
+ attest<typeof expected>(t)
14
+ attest(t.json).equals(expected.json)
12
15
  })
16
+
13
17
  it("overrides default precedence", () => {
14
- attest<boolean | number[]>(type("boolean|number[]").infer)
15
- attest<(boolean | number)[]>(type("(boolean|number)[]").infer)
18
+ const t = type("(boolean|number)[]")
19
+ const expected = type("boolean|number").array()
20
+ attest<typeof expected>(t)
21
+ attest(t.json).equals(expected.json)
16
22
  })
23
+
17
24
  it("nested", () => {
18
25
  attest<((number | boolean)[] | (string | undefined)[])[]>(
19
26
  type("((boolean|number)[]|(string|undefined)[])[]").infer
20
27
  )
21
28
  })
22
- describe("errors", () => {
23
- it("empty", () => {
24
- attest(() => {
25
- // @ts-expect-error
26
- type("()")
27
- }).throws(writeExpressionExpectedMessage(")"))
28
- })
29
- it("unmatched (", () => {
30
- attest(() => {
31
- // @ts-expect-error
32
- type("string|(boolean|number[]")
33
- }).throwsAndHasTypeError(writeUnclosedGroupMessage(")"))
34
- })
35
- it("unmatched )", () => {
36
- attest(() => {
37
- // @ts-expect-error
38
- type("string|number[]|boolean)")
39
- }).throwsAndHasTypeError(writeUnmatchedGroupCloseMessage(""))
40
- })
41
- it("lone )", () => {
42
- attest(() => {
43
- // @ts-expect-error
44
- type(")")
45
- }).throws(writeExpressionExpectedMessage(")"))
46
- })
47
- it("lone (", () => {
48
- attest(() => {
49
- // @ts-expect-error
50
- type("(")
51
- }).throws(writeExpressionExpectedMessage(""))
52
- })
53
- it("deep unmatched (", () => {
54
- attest(() => {
55
- // @ts-expect-error
56
- type("(null|(undefined|(1))|2")
57
- }).throwsAndHasTypeError(writeUnclosedGroupMessage(")"))
58
- })
59
- it("deep unmatched )", () => {
60
- attest(() => {
61
- // @ts-expect-error
62
- type("((string|number)[]|boolean))[]")
63
- }).throwsAndHasTypeError(writeUnmatchedGroupCloseMessage("[]"))
64
- })
65
- it("starting )", () => {
66
- attest(() => {
67
- // @ts-expect-error
68
- type(")number(")
69
- }).throws(writeExpressionExpectedMessage(")number("))
70
- })
29
+
30
+ it("empty", () => {
31
+ attest(() => {
32
+ // @ts-expect-error
33
+ type("()")
34
+ }).throws(writeExpressionExpectedMessage(")"))
35
+ })
36
+
37
+ it("unmatched (", () => {
38
+ attest(() => {
39
+ // @ts-expect-error
40
+ type("string|(boolean|number[]")
41
+ }).throwsAndHasTypeError(writeUnclosedGroupMessage(")"))
42
+ })
43
+
44
+ it("unmatched )", () => {
45
+ attest(() => {
46
+ // @ts-expect-error
47
+ type("string|number[]|boolean)")
48
+ }).throwsAndHasTypeError(writeUnmatchedGroupCloseMessage(""))
49
+ })
50
+
51
+ it("lone )", () => {
52
+ attest(() => {
53
+ // @ts-expect-error
54
+ type(")")
55
+ }).throws(writeExpressionExpectedMessage(")"))
56
+ })
57
+
58
+ it("lone (", () => {
59
+ attest(() => {
60
+ // @ts-expect-error
61
+ type("(")
62
+ }).throws(writeExpressionExpectedMessage(""))
63
+ })
64
+
65
+ it("deep unmatched (", () => {
66
+ attest(() => {
67
+ // @ts-expect-error
68
+ type("(null|(undefined|(1))|2")
69
+ }).throwsAndHasTypeError(writeUnclosedGroupMessage(")"))
70
+ })
71
+
72
+ it("deep unmatched )", () => {
73
+ attest(() => {
74
+ // @ts-expect-error
75
+ type("((string|number)[]|boolean))[]")
76
+ }).throwsAndHasTypeError(writeUnmatchedGroupCloseMessage("[]"))
77
+ })
78
+
79
+ it("starting )", () => {
80
+ attest(() => {
81
+ // @ts-expect-error
82
+ type(")number(")
83
+ }).throws(writeExpressionExpectedMessage(")number("))
71
84
  })
72
85
  })
@@ -1,24 +1,27 @@
1
- import { attest } from "@arktype/attest"
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import { writeDuplicateAliasError } from "@arktype/schema"
2
3
  import { lazily } from "@arktype/util"
3
- import { scope, type, type Ark, type Module } from "arktype"
4
+ import { type Module, scope, type } from "arktype"
5
+ import { writePrefixedPrivateReferenceMessage } from "../parser/semantic/validate.js"
4
6
 
5
- describe("scope imports", () => {
6
- const threeSixtyNoScope = lazily(() =>
7
- scope({
8
- three: "3",
9
- sixty: "60",
10
- no: "'no'"
11
- })
12
- )
13
- const yesScope = lazily(() => scope({ yes: "'yes'" }))
7
+ const threeSixtyNoScope = lazily(() =>
8
+ scope({
9
+ three: "3",
10
+ sixty: "60",
11
+ no: "'no'"
12
+ })
13
+ )
14
+ const yesScope = lazily(() => scope({ yes: "'yes'" }))
14
15
 
15
- const threeSixtyNoModule = lazily(() => threeSixtyNoScope.export())
16
- const yesModule = lazily(() => yesScope.export())
16
+ const threeSixtyNoModule = lazily(() => threeSixtyNoScope.export())
17
+ const yesModule = lazily(() => yesScope.export())
17
18
 
19
+ contextualize(() => {
18
20
  it("single", () => {
19
21
  const $ = scope({
20
- ...threeSixtyNoModule
21
- }).scope({ threeSixtyNo: "three|sixty|no" })
22
+ ...threeSixtyNoModule,
23
+ threeSixtyNo: "three|sixty|no"
24
+ })
22
25
  attest<{ threeSixtyNo: 3 | 60 | "no" }>($.infer)
23
26
  })
24
27
 
@@ -29,25 +32,14 @@ describe("scope imports", () => {
29
32
  extra: "true"
30
33
  })
31
34
 
32
- const imported = base.scope({
35
+ const imported = scope({
36
+ ...base.import(),
33
37
  a: "three|sixty|no|yes|extra"
34
38
  })
35
39
 
36
40
  attest<{ a: 3 | 60 | "no" | "yes" | true }>(imported.infer)
37
41
  })
38
42
 
39
- // TODO: fix, tests for more duplicate scenarios
40
- // it("duplicate alias", () => {
41
- // attest(() =>
42
- // scope({ a: "boolean" })
43
- // .scope(
44
- // // @ts-expect-error
45
- // { a: "string" }
46
- // )
47
- // .export()
48
- // ).throwsAndHasTypeError(writeDuplicateAliasesMessage("a"))
49
- // })
50
-
51
43
  it("import & export", () => {
52
44
  const threeSixtyNoScope = scope({
53
45
  three: "3",
@@ -72,22 +64,15 @@ describe("scope imports", () => {
72
64
 
73
65
  attest<
74
66
  Module<{
75
- exports: {
76
- hasCrept: true
77
- public: string | true | 3
78
- }
79
- locals: {
80
- three: 3
81
- no: "no"
82
- private: string
83
- }
84
- ambient: Ark
67
+ hasCrept: true
68
+ public: string | true | 3
69
+ "#three": 3
70
+ "#no": "no"
71
+ "#private": string
85
72
  }>
86
73
  >(types)
87
74
  })
88
- })
89
75
 
90
- describe("private aliases", () => {
91
76
  it("non-generic", () => {
92
77
  const types = scope({
93
78
  foo: "bar[]",
@@ -97,17 +82,45 @@ describe("private aliases", () => {
97
82
  attest(types.foo.json).equals(type("boolean[]").json)
98
83
  attest<
99
84
  Module<{
100
- exports: { foo: boolean[] }
101
- locals: { bar: boolean }
102
- ambient: Ark
85
+ foo: boolean[]
86
+ "#bar": boolean
103
87
  }>
104
88
  >(types)
105
89
  })
106
- it("generic", () => {
107
- const types = scope({
108
- foo: "bar<string>[]",
109
- "#bar<t>": ["t"]
110
- }).export()
111
- attest<[string][]>(types.foo.infer)
90
+
91
+ it("errors on private reference with #", () => {
92
+ attest(() =>
93
+ scope({
94
+ // @ts-expect-error
95
+ xdd: "#kekw",
96
+ "#kekw": "true"
97
+ }).export()
98
+ ).throwsAndHasTypeError(writePrefixedPrivateReferenceMessage("#kekw"))
112
99
  })
100
+
101
+ it("errors on public and private refrence with same name", () => {
102
+ attest(() =>
103
+ scope({
104
+ kekw: "1",
105
+ // @ts-expect-error
106
+ "#kekw": "1"
107
+ }).export()
108
+ ).throwsAndHasTypeError(writeDuplicateAliasError("kekw"))
109
+ attest(() =>
110
+ scope({
111
+ // @ts-expect-error
112
+ "#kekw": "1",
113
+ kekw: "1"
114
+ }).export()
115
+ ).throwsAndHasTypeError(writeDuplicateAliasError("kekw"))
116
+ })
117
+
118
+ // TODO: reenable
119
+ // it("generic", () => {
120
+ // const types = scope({
121
+ // foo: "bar<string>[]",
122
+ // "#bar<t>": ["t"]
123
+ // }).export()
124
+ // attest<[string][]>(types.foo.infer)
125
+ // })
113
126
  })
@@ -1,19 +1,19 @@
1
- import { attest } from "@arktype/attest"
2
- import { schema } from "@arktype/schema"
3
- import { type } from "arktype"
4
- import type { Ark } from "../ark.js"
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import { rawSchema } from "@arktype/schema"
3
+ import { type Type, type } from "arktype"
5
4
  import { writeInvalidConstructorMessage } from "../parser/tuple.js"
6
- import type { Type } from "../type.js"
7
5
 
8
- describe("instanceof", () => {
9
- describe("tuple expression", () => {
6
+ contextualize(
7
+ "tuple expression",
8
+ () => {
10
9
  it("base", () => {
11
10
  const t = type(["instanceof", Error])
12
11
  attest<Error>(t.infer)
13
- attest(t.json).equals(schema(Error).json)
12
+ const expected = rawSchema(Error)
13
+ attest(t.json).equals(expected.json)
14
14
  const e = new Error()
15
- attest(t(e).out).equals(e)
16
- attest(t({}).errors?.summary).snap("Must be an Error (was Object)")
15
+ attest(t(e)).equals(e)
16
+ attest(t({}).toString()).snap("must be an Error (was object)")
17
17
  })
18
18
  it("inherited", () => {
19
19
  const t = type(["instanceof", TypeError])
@@ -21,9 +21,9 @@ describe("instanceof", () => {
21
21
  // for some reason the return of TypeError's constructor is actually
22
22
  // inferred as Error? Disabling this check for now, seems like an anomaly.
23
23
  // attest<TypeError>(t.infer)
24
- attest(t(e).out).equals(e)
25
- attest(t(new Error()).errors?.summary).snap(
26
- "Must be an instance of TypeError (was Error)"
24
+ attest(t(e)).equals(e)
25
+ attest(t(new Error()).toString()).snap(
26
+ "must be an instance of TypeError (was Error)"
27
27
  )
28
28
  })
29
29
  it("abstract", () => {
@@ -36,7 +36,7 @@ describe("instanceof", () => {
36
36
  const t = type(["instanceof", Base])
37
37
  attest<Base>(t.infer)
38
38
  const sub = new Sub()
39
- attest(t(sub).out).equals(sub)
39
+ attest(t(sub)).equals(sub)
40
40
  })
41
41
  it("multiple branches", () => {
42
42
  const t = type(["instanceof", Date, Array])
@@ -53,14 +53,14 @@ describe("instanceof", () => {
53
53
  private isArk = true
54
54
  }
55
55
  const ark = type(["instanceof", ArkClass])
56
- attest<Type<ArkClass, Ark>>(ark)
56
+ attest<Type<ArkClass, {}>>(ark)
57
57
  // not expanded since there are no morphs
58
58
  attest(ark.infer).type.toString("ArkClass")
59
59
  attest(ark.in.infer).type.toString("ArkClass")
60
60
  const a = new ArkClass()
61
- attest(ark(a).out).equals(a)
62
- attest(ark({}).errors?.summary).snap(
63
- "Must be an instance of ArkClass (was Object)"
61
+ attest(ark(a)).equals(a)
62
+ attest(ark({}).toString()).snap(
63
+ "must be an instance of ArkClass (was object)"
64
64
  )
65
65
  })
66
66
  // TODO: Fix- Investigate bidirectional check impact on perf to narrow private props without breaking this case:
@@ -75,13 +75,15 @@ describe("instanceof", () => {
75
75
  private isArk = true
76
76
  }
77
77
  const ark = type(["instanceof", ArkClass])
78
- attest<Type<ArkClass, Ark>>(ark)
78
+
79
+ attest<Type<ArkClass, {}>>(ark)
79
80
  // not expanded since there are no morphs
80
81
  attest(ark.infer).type.toString("ArkClass")
81
82
  attest(ark.in.infer).type.toString("ArkClass")
82
83
  })
83
- })
84
- describe("root expression", () => {
84
+ },
85
+ "root expression",
86
+ () => {
85
87
  it("class", () => {
86
88
  const t = type("instanceof", Error)
87
89
  attest<Error>(t.infer)
@@ -98,5 +100,5 @@ describe("instanceof", () => {
98
100
  writeInvalidConstructorMessage("Error")
99
101
  )
100
102
  })
101
- })
102
- })
103
+ }
104
+ )
@@ -1,17 +1,19 @@
1
- import { attest } from "@arktype/attest"
2
- import { writeIndivisibleMessage } from "@arktype/schema"
3
- import { type } from "arktype"
1
+ import { attest, contextualize } from "@arktype/attest"
4
2
  import {
5
- writeMissingRightOperandMessage,
3
+ keywordNodes,
4
+ writeIndivisibleMessage,
6
5
  writeUnresolvableMessage
7
- } from "../parser/string/shift/operand/unenclosed.js"
6
+ } from "@arktype/schema"
7
+ import { type } from "arktype"
8
+ import { writeMissingRightOperandMessage } from "../parser/string/shift/operand/unenclosed.js"
8
9
 
9
- describe("intersection", () => {
10
+ contextualize(() => {
10
11
  it("two types", () => {
11
12
  const t = type("boolean&true")
12
13
  attest<true>(t.infer)
13
14
  attest(t.json).is(type("true").json)
14
15
  })
16
+
15
17
  it("intersection parsed before union", () => {
16
18
  // Should be parsed as:
17
19
  // 1. "0" | ("1"&"string") | "2"
@@ -20,25 +22,29 @@ describe("intersection", () => {
20
22
  attest<"0" | "1" | "2">(t.infer)
21
23
  attest(t.json).equals(type("===", "0", "1", "2").json)
22
24
  })
25
+
23
26
  it("tuple expression", () => {
24
27
  const t = type([{ a: "string" }, "&", { b: "number" }])
25
28
  attest<{ a: string; b: number }>(t.infer)
26
29
  attest(t.json).equals(type({ a: "string", b: "number" }).json)
27
30
  })
31
+
28
32
  it("several types", () => {
29
33
  const t = type("unknown&boolean&false")
30
34
  attest<false>(t.infer)
31
35
  attest(t.json).equals(type("false").json)
32
36
  })
37
+
33
38
  it("method", () => {
34
39
  const t = type({ a: "string" }).and({ b: "boolean" })
35
40
  attest<{ a: string; b: boolean }>(t.infer)
36
41
  attest(t.json).equals(type({ a: "string", b: "boolean" }).json)
37
42
  })
43
+
38
44
  it("chained deep intersections", () => {
39
- const b = type({ b: "boolean" }, "=>", (o) => [o.b])
45
+ const b = type({ b: "boolean" }, "=>", o => [o.b])
40
46
  const t = type({
41
- a: ["string", "=>", (s) => s.length]
47
+ a: ["string", "=>", s => s.length]
42
48
  })
43
49
  .and({
44
50
  // unable to inline this due to:
@@ -59,46 +65,44 @@ describe("intersection", () => {
59
65
 
60
66
  attest<{ a: number; b: boolean[]; c: "hello" }>(t.infer)
61
67
  })
62
- describe("errors", () => {
63
- it("bad reference", () => {
64
- // @ts-expect-error
65
- attest(() => type("boolean&tru"))
66
- .throws(writeUnresolvableMessage("tru"))
67
- .type.errors("boolean&true")
68
- })
69
- it("double and", () => {
70
- // @ts-expect-error
71
- attest(() => type("boolean&&true")).throws(
72
- writeMissingRightOperandMessage("&", "&true")
73
- )
74
- })
75
- it("implicit never", () => {
76
- attest(() => type("string&number")).throws(
77
- "Intersection of a string and a number results in an unsatisfiable type"
78
- )
79
- })
80
- it("left semantic error", () => {
81
- // @ts-expect-error
82
- attest(() => type("string%2&'foo'")).throwsAndHasTypeError(
83
- writeIndivisibleMessage("string")
84
- )
85
- })
86
- it("right semantic error", () => {
68
+ it("bad reference", () => {
69
+ // @ts-expect-error
70
+ attest(() => type("boolean&tru"))
71
+ .throws(writeUnresolvableMessage("tru"))
72
+ .type.errors("boolean&true")
73
+ })
74
+ it("double and", () => {
75
+ // @ts-expect-error
76
+ attest(() => type("boolean&&true")).throws(
77
+ writeMissingRightOperandMessage("&", "&true")
78
+ )
79
+ })
80
+ it("implicit never", () => {
81
+ attest(() => type("string&number")).throws.snap(
82
+ "ParseError: Intersection of string and number results in an unsatisfiable type"
83
+ )
84
+ })
85
+ it("left semantic error", () => {
86
+ // @ts-expect-error
87
+ attest(() => type("string%2&'foo'")).throwsAndHasTypeError(
88
+ writeIndivisibleMessage(keywordNodes.string)
89
+ )
90
+ })
91
+ it("right semantic error", () => {
92
+ // @ts-expect-error
93
+ attest(() => type("'foo'&string%2")).throwsAndHasTypeError(
94
+ writeIndivisibleMessage(keywordNodes.string)
95
+ )
96
+ })
97
+ it("chained validation error", () => {
98
+ attest(() =>
87
99
  // @ts-expect-error
88
- attest(() => type("'foo'&string%2")).throwsAndHasTypeError(
89
- writeIndivisibleMessage("string")
90
- )
91
- })
92
- it("chained validation", () => {
93
- attest(() =>
94
- // @ts-expect-error
95
- type({ a: "string" }).and({ b: "what" })
96
- ).throwsAndHasTypeError(writeUnresolvableMessage("what"))
97
- })
98
- it("at path", () => {
99
- attest(() => type({ a: "string" }).and({ a: "number" })).throws(
100
- "Intersection at a of a string and a number results in an unsatisfiable type"
101
- )
102
- })
100
+ type({ a: "string" }).and({ b: "what" })
101
+ ).throwsAndHasTypeError(writeUnresolvableMessage("what"))
102
+ })
103
+ it("error at path", () => {
104
+ attest(() => type({ a: "string" }).and({ a: "number" })).throws.snap(
105
+ "ParseError: Intersection at a of string and number results in an unsatisfiable type"
106
+ )
103
107
  })
104
108
  })