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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/__tests__/array.test.ts +270 -151
  2. package/__tests__/badDefinitionType.test.ts +10 -3
  3. package/__tests__/basis.test.ts +6 -2
  4. package/__tests__/bounds.test.ts +282 -352
  5. package/__tests__/cast.test.ts +7 -2
  6. package/__tests__/completions.test.ts +8 -3
  7. package/__tests__/config.test.ts +95 -167
  8. package/__tests__/dateLiteral.test.ts +8 -4
  9. package/__tests__/declared.test.ts +7 -6
  10. package/__tests__/discrimination.test.ts +1 -1
  11. package/__tests__/divisor.test.ts +46 -22
  12. package/__tests__/enclosed.test.ts +39 -25
  13. package/__tests__/examples.test.ts +56 -56
  14. package/__tests__/expressions.test.ts +184 -112
  15. package/__tests__/generic.test.ts +307 -288
  16. package/__tests__/group.test.ts +67 -54
  17. package/__tests__/imports.test.ts +62 -49
  18. package/__tests__/instanceof.test.ts +25 -23
  19. package/__tests__/intersection.test.ts +52 -48
  20. package/__tests__/keyTraversal.test.ts +77 -73
  21. package/__tests__/keyof.test.ts +68 -52
  22. package/__tests__/keywords.test.ts +164 -138
  23. package/__tests__/literal.test.ts +60 -21
  24. package/__tests__/match.bench.ts +31 -0
  25. package/__tests__/match.test.ts +222 -22
  26. package/__tests__/narrow.test.ts +52 -54
  27. package/__tests__/objectLiteral.test.ts +347 -205
  28. package/__tests__/operator.bench.ts +4 -4
  29. package/__tests__/pipe.test.ts +501 -0
  30. package/__tests__/realWorld.test.ts +63 -0
  31. package/__tests__/regex.test.ts +17 -7
  32. package/__tests__/scope.bench.ts +1 -1
  33. package/__tests__/scope.test.ts +257 -23
  34. package/__tests__/serialization.test.ts +19 -0
  35. package/__tests__/string.test.ts +11 -7
  36. package/__tests__/submodule.test.ts +26 -39
  37. package/__tests__/this.test.ts +83 -57
  38. package/__tests__/thunk.test.ts +13 -7
  39. package/__tests__/traverse.test.ts +76 -54
  40. package/__tests__/tsGenerics.test.ts +12 -12
  41. package/__tests__/type.test.ts +36 -0
  42. package/__tests__/typeReference.test.ts +7 -7
  43. package/__tests__/unenclosed.test.ts +116 -100
  44. package/__tests__/union.test.ts +171 -104
  45. package/ark.ts +21 -65
  46. package/generic.ts +69 -0
  47. package/main.ts +6 -18
  48. package/match.ts +195 -93
  49. package/module.ts +15 -0
  50. package/out/ark.d.ts +15 -39
  51. package/out/ark.js +9 -18
  52. package/out/ark.js.map +1 -1
  53. package/out/generic.d.ts +21 -0
  54. package/out/generic.js +24 -0
  55. package/out/generic.js.map +1 -0
  56. package/out/main.d.ts +6 -7
  57. package/out/main.js +4 -3
  58. package/out/main.js.map +1 -1
  59. package/out/match.d.ts +57 -34
  60. package/out/match.js +51 -17
  61. package/out/match.js.map +1 -1
  62. package/out/module.d.ts +10 -0
  63. package/out/module.js +3 -0
  64. package/out/module.js.map +1 -0
  65. package/out/parser/definition.d.ts +11 -13
  66. package/out/parser/definition.js +10 -13
  67. package/out/parser/definition.js.map +1 -1
  68. package/out/parser/generic.d.ts +5 -7
  69. package/out/parser/generic.js +7 -11
  70. package/out/parser/generic.js.map +1 -1
  71. package/out/parser/objectLiteral.d.ts +45 -14
  72. package/out/parser/objectLiteral.js +61 -52
  73. package/out/parser/objectLiteral.js.map +1 -1
  74. package/out/parser/semantic/bounds.d.ts +10 -8
  75. package/out/parser/semantic/bounds.js +0 -1
  76. package/out/parser/semantic/bounds.js.map +1 -1
  77. package/out/parser/semantic/divisor.d.ts +5 -7
  78. package/out/parser/semantic/divisor.js +0 -1
  79. package/out/parser/semantic/infer.d.ts +24 -0
  80. package/out/parser/semantic/infer.js +1 -0
  81. package/out/parser/semantic/infer.js.map +1 -0
  82. package/out/parser/semantic/utils.d.ts +3 -7
  83. package/out/parser/semantic/utils.js +0 -1
  84. package/out/parser/semantic/validate.d.ts +15 -14
  85. package/out/parser/semantic/validate.js +1 -1
  86. package/out/parser/semantic/validate.js.map +1 -1
  87. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  88. package/out/parser/string/reduce/dynamic.js +31 -26
  89. package/out/parser/string/reduce/dynamic.js.map +1 -1
  90. package/out/parser/string/reduce/shared.d.ts +6 -20
  91. package/out/parser/string/reduce/shared.js +0 -1
  92. package/out/parser/string/reduce/shared.js.map +1 -1
  93. package/out/parser/string/reduce/static.d.ts +27 -24
  94. package/out/parser/string/reduce/static.js +0 -1
  95. package/out/parser/string/shift/operand/date.d.ts +3 -3
  96. package/out/parser/string/shift/operand/date.js +4 -7
  97. package/out/parser/string/shift/operand/date.js.map +1 -1
  98. package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
  99. package/out/parser/string/shift/operand/enclosed.js +12 -13
  100. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  101. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  102. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  103. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  104. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  105. package/out/parser/string/shift/operand/operand.js +8 -12
  106. package/out/parser/string/shift/operand/operand.js.map +1 -1
  107. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
  108. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  109. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  110. package/out/parser/string/shift/operator/bounds.d.ts +9 -13
  111. package/out/parser/string/shift/operator/bounds.js +43 -54
  112. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  113. package/out/parser/string/shift/operator/divisor.d.ts +2 -4
  114. package/out/parser/string/shift/operator/divisor.js +1 -3
  115. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  116. package/out/parser/string/shift/operator/operator.d.ts +2 -3
  117. package/out/parser/string/shift/operator/operator.js +12 -19
  118. package/out/parser/string/shift/operator/operator.js.map +1 -1
  119. package/out/parser/string/shift/scanner.d.ts +2 -2
  120. package/out/parser/string/shift/scanner.js +14 -16
  121. package/out/parser/string/shift/scanner.js.map +1 -1
  122. package/out/parser/string/string.d.ts +8 -6
  123. package/out/parser/string/string.js +1 -3
  124. package/out/parser/string/string.js.map +1 -1
  125. package/out/parser/tuple.d.ts +102 -43
  126. package/out/parser/tuple.js +119 -87
  127. package/out/parser/tuple.js.map +1 -1
  128. package/out/scope.d.ts +43 -120
  129. package/out/scope.js +39 -209
  130. package/out/scope.js.map +1 -1
  131. package/out/type.d.ts +43 -64
  132. package/out/type.js +30 -131
  133. package/out/type.js.map +1 -1
  134. package/package.json +7 -3
  135. package/parser/definition.ts +77 -93
  136. package/parser/generic.ts +38 -40
  137. package/parser/objectLiteral.ts +172 -136
  138. package/parser/semantic/bounds.ts +33 -32
  139. package/parser/semantic/divisor.ts +8 -10
  140. package/parser/semantic/infer.ts +141 -0
  141. package/parser/semantic/utils.ts +12 -23
  142. package/parser/semantic/validate.ts +81 -73
  143. package/parser/string/reduce/dynamic.ts +67 -55
  144. package/parser/string/reduce/shared.ts +1 -3
  145. package/parser/string/reduce/static.ts +67 -69
  146. package/parser/string/shift/operand/date.ts +15 -14
  147. package/parser/string/shift/operand/enclosed.ts +30 -25
  148. package/parser/string/shift/operand/genericArgs.ts +46 -45
  149. package/parser/string/shift/operand/operand.ts +32 -38
  150. package/parser/string/shift/operand/unenclosed.ts +107 -158
  151. package/parser/string/shift/operator/bounds.ts +118 -112
  152. package/parser/string/shift/operator/divisor.ts +14 -17
  153. package/parser/string/shift/operator/operator.ts +35 -46
  154. package/parser/string/shift/scanner.ts +51 -50
  155. package/parser/string/string.ts +30 -35
  156. package/parser/tuple.ts +375 -307
  157. package/scope.ts +170 -453
  158. package/type.ts +193 -274
  159. package/__tests__/astUtils.test.ts +0 -17
  160. package/__tests__/cyclic.test.ts +0 -144
  161. package/__tests__/jsdoc.test.ts +0 -64
  162. package/__tests__/morph.test.ts +0 -409
  163. package/__tests__/nodeDefinitions.test.ts +0 -190
  164. package/__tests__/typeMethods.test.ts +0 -69
  165. package/out/ark.d.ts.map +0 -1
  166. package/out/arkConfig.d.ts +0 -2
  167. package/out/arkConfig.d.ts.map +0 -1
  168. package/out/arkConfig.js +0 -10
  169. package/out/arkConfig.js.map +0 -1
  170. package/out/main.d.ts.map +0 -1
  171. package/out/match.d.ts.map +0 -1
  172. package/out/parser/definition.d.ts.map +0 -1
  173. package/out/parser/generic.d.ts.map +0 -1
  174. package/out/parser/objectLiteral.d.ts.map +0 -1
  175. package/out/parser/semantic/bounds.d.ts.map +0 -1
  176. package/out/parser/semantic/divisor.d.ts.map +0 -1
  177. package/out/parser/semantic/intersections.d.ts +0 -14
  178. package/out/parser/semantic/intersections.d.ts.map +0 -1
  179. package/out/parser/semantic/intersections.js +0 -2
  180. package/out/parser/semantic/intersections.js.map +0 -1
  181. package/out/parser/semantic/semantic.d.ts +0 -33
  182. package/out/parser/semantic/semantic.d.ts.map +0 -1
  183. package/out/parser/semantic/semantic.js +0 -2
  184. package/out/parser/semantic/semantic.js.map +0 -1
  185. package/out/parser/semantic/utils.d.ts.map +0 -1
  186. package/out/parser/semantic/validate.d.ts.map +0 -1
  187. package/out/parser/shared.d.ts +0 -54
  188. package/out/parser/shared.d.ts.map +0 -1
  189. package/out/parser/shared.js +0 -47
  190. package/out/parser/shared.js.map +0 -1
  191. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  192. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  193. package/out/parser/string/reduce/static.d.ts.map +0 -1
  194. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  195. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  196. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  197. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  198. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  199. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  200. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  201. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  202. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  203. package/out/parser/string/string.d.ts.map +0 -1
  204. package/out/parser/tuple.d.ts.map +0 -1
  205. package/out/scope.d.ts.map +0 -1
  206. package/out/test.d.ts +0 -2
  207. package/out/test.d.ts.map +0 -1
  208. package/out/test.js +0 -7
  209. package/out/test.js.map +0 -1
  210. package/out/tsconfig.build.tsbuildinfo +0 -1
  211. package/out/type.d.ts.map +0 -1
  212. package/out/util.d.ts +0 -19
  213. package/out/util.d.ts.map +0 -1
  214. package/out/util.js +0 -7
  215. package/out/util.js.map +0 -1
  216. package/parser/semantic/intersections.ts +0 -46
  217. package/parser/semantic/semantic.ts +0 -144
  218. package/parser/shared.ts +0 -152
  219. package/util.ts +0 -36
@@ -0,0 +1,141 @@
1
+ import type {
2
+ Date,
3
+ DateLiteral,
4
+ GenericProps,
5
+ LimitLiteral,
6
+ RegexLiteral,
7
+ constrain,
8
+ distillIn,
9
+ inferIntersection,
10
+ normalizeLimit,
11
+ string
12
+ } from "@arktype/schema"
13
+ import type { BigintLiteral, array } from "@arktype/util"
14
+ import type {
15
+ UnparsedScope,
16
+ resolve,
17
+ tryInferSubmoduleReference
18
+ } from "../../scope.js"
19
+ import type { inferDefinition } from "../definition.js"
20
+ import type { Comparator, MinComparator } from "../string/reduce/shared.js"
21
+ import type { StringLiteral } from "../string/shift/operand/enclosed.js"
22
+
23
+ export type inferAstRoot<ast, $, args> = inferConstrainableAst<ast, $, args>
24
+
25
+ export type inferAstIn<ast, $, args> = distillIn<inferAstRoot<ast, $, args>>
26
+
27
+ export type inferConstrainableAst<ast, $, args> =
28
+ ast extends array ? inferExpression<ast, $, args>
29
+ : ast extends string ? inferTerminal<ast, $, args>
30
+ : never
31
+
32
+ export type GenericInstantiationAst<
33
+ generic extends GenericProps = GenericProps,
34
+ argAsts extends unknown[] = unknown[]
35
+ > = [generic, "<>", argAsts]
36
+
37
+ export type inferExpression<ast extends array, $, args> =
38
+ ast extends GenericInstantiationAst<infer generic, infer argAsts> ?
39
+ inferDefinition<
40
+ generic["def"],
41
+ generic["$"]["$"] extends UnparsedScope ?
42
+ // If the generic was defined in the current scope, its definition can be
43
+ // resolved using the same scope as that of the input args.
44
+ $
45
+ : // Otherwise, use the scope that was explicitly associated with it.
46
+ generic["$"]["$"],
47
+ {
48
+ // Using keyof g["params"] & number here results in the element types
49
+ // being mixed- another reason TS should not have separate `${number}` and number keys!
50
+ [i in keyof generic["params"] &
51
+ `${number}` as generic["params"][i]]: inferConstrainableAst<
52
+ argAsts[i & keyof argAsts],
53
+ $,
54
+ args
55
+ >
56
+ }
57
+ >
58
+ : ast[1] extends "[]" ? inferConstrainableAst<ast[0], $, args>[]
59
+ : ast[1] extends "|" ?
60
+ | inferConstrainableAst<ast[0], $, args>
61
+ | inferConstrainableAst<ast[2], $, args>
62
+ : ast[1] extends "&" ?
63
+ inferIntersection<
64
+ inferConstrainableAst<ast[0], $, args>,
65
+ inferConstrainableAst<ast[2], $, args>
66
+ >
67
+ : ast[1] extends Comparator ?
68
+ ast[0] extends LimitLiteral ?
69
+ constrainBound<inferConstrainableAst<ast[2], $, args>, ast[1], ast[0]>
70
+ : constrainBound<inferConstrainableAst<ast[0], $, args>, ast[1], ast[2]>
71
+ : ast[1] extends "%" ?
72
+ constrain<
73
+ inferConstrainableAst<ast[0], $, args>,
74
+ "divisor",
75
+ ast[2] & number
76
+ >
77
+ : ast[0] extends "keyof" ? keyof inferConstrainableAst<ast[1], $, args>
78
+ : never
79
+
80
+ export type constrainBound<
81
+ constrainableIn,
82
+ comparator extends Comparator,
83
+ limit
84
+ > =
85
+ distillIn<constrainableIn> extends infer In ?
86
+ comparator extends "==" ?
87
+ In extends number ? limit
88
+ : In extends Date ? Date.literal<normalizeLimit<limit>>
89
+ : constrain<constrainableIn, "exactLength", limit & number>
90
+ : constrain<
91
+ constrainableIn,
92
+ In extends number ?
93
+ comparator extends MinComparator ?
94
+ "min"
95
+ : "max"
96
+ : In extends string | array ?
97
+ comparator extends MinComparator ?
98
+ "minLength"
99
+ : "maxLength"
100
+ : comparator extends MinComparator ? "after"
101
+ : "before",
102
+ {
103
+ rule: normalizeLimit<limit>
104
+ exclusive: comparator extends ">" | "<" ? true : false
105
+ }
106
+ >
107
+ : never
108
+
109
+ export type PrefixOperator = "keyof" | "instanceof" | "===" | "node"
110
+
111
+ export type PrefixExpression<
112
+ operator extends PrefixOperator = PrefixOperator,
113
+ operand = unknown
114
+ > = [operator, operand]
115
+
116
+ export type PostfixOperator = "[]"
117
+
118
+ export type PostfixExpression<
119
+ operator extends PostfixOperator = PostfixOperator,
120
+ operand = unknown
121
+ > = readonly [operand, operator]
122
+
123
+ export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@"
124
+
125
+ export type InfixExpression<
126
+ operator extends InfixOperator = InfixOperator,
127
+ l = unknown,
128
+ r = unknown
129
+ > = [l, operator, r]
130
+
131
+ export type inferTerminal<token extends string, $, args> =
132
+ token extends keyof args | keyof $ ? resolve<token, $, args>
133
+ : `#${token}` extends keyof $ ? resolve<`#${token}`, $, args>
134
+ : token extends StringLiteral<infer text> ? text
135
+ : token extends `${infer n extends number}` ? n
136
+ : token extends BigintLiteral<infer b> ? b
137
+ : token extends RegexLiteral<infer source> ? string.matching<source>
138
+ : token extends DateLiteral<infer source> ? Date.literal<source>
139
+ : // doing this last allows us to infer never if it isn't valid rather than check
140
+ // if it's a valid submodule reference ahead of time
141
+ tryInferSubmoduleReference<$, token>
@@ -1,26 +1,15 @@
1
- import type { List, Stringifiable } from "@arktype/util"
1
+ import type { Stringifiable } from "@arktype/util"
2
2
  import type { Comparator } from "../string/reduce/shared.js"
3
- import type { InfixExpression, PostfixExpression } from "./semantic.js"
3
+ import type { InfixExpression, PostfixExpression } from "./infer.js"
4
4
 
5
- export type astToString<ast> = `'${astToStringRecurse<ast>}'`
6
-
7
- type astToStringRecurse<ast> = ast extends PostfixExpression<
8
- infer operator,
9
- infer operand
10
- >
11
- ? operator extends "[]"
12
- ? `${groupAst<operand>}[]`
13
- : never
14
- : ast extends InfixExpression<infer operator, infer l, infer r>
15
- ? operator extends "&" | "|" | "%" | Comparator
16
- ? `${groupAst<l>}${operator}${groupAst<r>}`
17
- : never
18
- : ast extends Stringifiable
19
- ? `${ast extends bigint ? `${ast}n` : ast}`
5
+ export type astToString<ast> =
6
+ ast extends PostfixExpression<infer operator, infer operand> ?
7
+ operator extends "[]" ?
8
+ `${astToString<operand>}[]`
9
+ : never
10
+ : ast extends InfixExpression<infer operator, infer l, infer r> ?
11
+ operator extends "&" | "|" | "%" | Comparator ?
12
+ `${astToString<l>} ${operator} ${astToString<r>}`
13
+ : never
14
+ : ast extends Stringifiable ? `${ast extends bigint ? `${ast}n` : ast}`
20
15
  : "..."
21
-
22
- type groupAst<ast> = ast extends List
23
- ? ast[1] extends "[]"
24
- ? astToStringRecurse<ast>
25
- : `(${astToStringRecurse<ast>})`
26
- : astToStringRecurse<ast>
@@ -1,16 +1,19 @@
1
- import type { Refinements } from "@arktype/schema"
1
+ import type {
2
+ arkKind,
3
+ GenericProps,
4
+ PrivateDeclaration,
5
+ writeMissingSubmoduleAccessMessage
6
+ } from "@arktype/schema"
2
7
  import type {
3
8
  BigintLiteral,
9
+ charsAfterFirst,
4
10
  Completion,
5
11
  ErrorMessage,
6
- NumberLiteral,
12
+ isAnyOrNever,
7
13
  writeMalformedNumericLiteralMessage
8
14
  } from "@arktype/util"
9
- import type { Module } from "../../scope.js"
10
- import type { GenericProps } from "../../type.js"
11
15
  import type { Comparator } from "../string/reduce/shared.js"
12
16
  import type { writeInvalidGenericArgsMessage } from "../string/shift/operand/genericArgs.js"
13
- import type { writeMissingSubmoduleAccessMessage } from "../string/shift/operand/unenclosed.js"
14
17
  import type { parseString } from "../string/string.js"
15
18
  import type { validateRange } from "./bounds.js"
16
19
  import type { validateDivisor } from "./divisor.js"
@@ -18,40 +21,36 @@ import type {
18
21
  GenericInstantiationAst,
19
22
  InfixExpression,
20
23
  PostfixExpression
21
- } from "./semantic.js"
24
+ } from "./infer.js"
22
25
  import type { astToString } from "./utils.js"
23
26
 
24
- export type validateAst<ast, $, args> = ast extends string
25
- ? validateStringAst<ast, $>
26
- : ast extends PostfixExpression<infer operator, infer operand>
27
- ? operator extends "[]"
28
- ? validateAst<operand, $, args>
29
- : never
30
- : ast extends InfixExpression<infer operator, infer l, infer r>
31
- ? operator extends "&" | "|"
32
- ? validateInfix<ast, $, args>
33
- : operator extends Comparator
34
- ? validateRange<l, operator, r, $, args>
35
- : operator extends "%"
36
- ? validateDivisor<l, $, args>
27
+ export type validateAst<ast, $, args> =
28
+ ast extends string ? validateStringAst<ast, $>
29
+ : ast extends PostfixExpression<infer operator, infer operand> ?
30
+ operator extends "[]" ?
31
+ validateAst<operand, $, args>
32
+ : never
33
+ : ast extends InfixExpression<infer operator, infer l, infer r> ?
34
+ operator extends "&" | "|" ? validateInfix<ast, $, args>
35
+ : operator extends Comparator ? validateRange<l, operator, r, $, args>
36
+ : operator extends "%" ? validateDivisor<l, $, args>
37
37
  : undefined
38
- : ast extends readonly ["keyof", infer operand]
39
- ? validateAst<operand, $, args>
40
- : ast extends GenericInstantiationAst
41
- ? validateGenericArgs<ast["2"], $, args>
42
- : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>>
38
+ : ast extends readonly ["keyof", infer operand] ?
39
+ validateAst<operand, $, args>
40
+ : ast extends GenericInstantiationAst ? validateGenericArgs<ast[2], $, args>
41
+ : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
42
+ ast: ast
43
+ }
43
44
 
44
45
  type writeUnexpectedExpressionMessage<expression extends string> =
45
46
  `Unexpectedly failed to parse the expression resulting from ${expression}`
46
47
 
47
- type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [
48
- infer head,
49
- ...infer tail
50
- ]
51
- ? validateAst<head, $, args> extends ErrorMessage<infer message>
52
- ? ErrorMessage<message>
53
- : validateGenericArgs<tail, $, args>
54
- : undefined
48
+ type validateGenericArgs<argAsts extends unknown[], $, args> =
49
+ argAsts extends [infer head, ...infer tail] ?
50
+ validateAst<head, $, args> extends ErrorMessage<infer message> ?
51
+ ErrorMessage<message>
52
+ : validateGenericArgs<tail, $, args>
53
+ : undefined
55
54
 
56
55
  export const writeUnsatisfiableExpressionError = <expression extends string>(
57
56
  expression: expression
@@ -61,49 +60,58 @@ export const writeUnsatisfiableExpressionError = <expression extends string>(
61
60
  export type writeUnsatisfiableExpressionError<expression extends string> =
62
61
  `${expression} results in an unsatisfiable type`
63
62
 
64
- type validateStringAst<def extends string, $> = def extends NumberLiteral<
65
- infer value
66
- >
67
- ? number extends value
68
- ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">>
69
- : undefined
70
- : def extends BigintLiteral<infer value>
71
- ? bigint extends value
72
- ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">>
73
- : undefined
74
- : def extends keyof $
75
- ? $[def] extends null
76
- ? // handle any/never
77
- def
63
+ export const writePrefixedPrivateReferenceMessage = <
64
+ def extends PrivateDeclaration
65
+ >(
66
+ def: def
67
+ ): writePrefixedPrivateReferenceMessage<def> =>
68
+ `Private type references should not include '#'. Use '${def.slice(1) as charsAfterFirst<def>}' instead.`
69
+
70
+ export type writePrefixedPrivateReferenceMessage<
71
+ def extends PrivateDeclaration
72
+ > =
73
+ `Private type references should not include '#'. Use '${charsAfterFirst<def>}' instead.`
74
+
75
+ type validateStringAst<def extends string, $> =
76
+ def extends `${infer n extends number}` ?
77
+ number extends n ?
78
+ ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">>
79
+ : undefined
80
+ : def extends BigintLiteral<infer b> ?
81
+ bigint extends b ?
82
+ ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">>
83
+ : undefined
84
+ : maybeExtractAlias<def, $> extends infer alias extends keyof $ ?
85
+ isAnyOrNever<$[alias]> extends true ? def
86
+ : def extends PrivateDeclaration ?
87
+ ErrorMessage<writePrefixedPrivateReferenceMessage<def>>
78
88
  : // these problems would've been caught during a fullStringParse, but it's most
79
89
  // efficient to check for them here in case the string was naively parsed
80
- $[def] extends GenericProps
81
- ? ErrorMessage<
82
- writeInvalidGenericArgsMessage<def, $[def]["parameters"], []>
83
- >
84
- : $[def] extends Module
85
- ? ErrorMessage<writeMissingSubmoduleAccessMessage<def>>
86
- : undefined
87
- : def extends ErrorMessage
88
- ? def
90
+ $[alias] extends GenericProps ?
91
+ ErrorMessage<writeInvalidGenericArgsMessage<def, $[alias]["params"], []>>
92
+ : $[alias] extends { [arkKind]: "module" } ?
93
+ ErrorMessage<writeMissingSubmoduleAccessMessage<def>>
94
+ : undefined
95
+ : def extends ErrorMessage ? def
89
96
  : undefined
90
97
 
91
- export type validateString<def extends string, $, args> = validateAst<
92
- parseString<def, $, args>,
93
- $,
94
- args
95
- > extends infer result extends ErrorMessage
96
- ? result extends Completion<infer text>
97
- ? text
98
- : result
99
- : def
98
+ export type maybeExtractAlias<def extends string, $> =
99
+ def extends keyof $ ? def
100
+ : `#${def}` extends keyof $ ? `#${def}`
101
+ : null
100
102
 
101
- type validateInfix<ast extends InfixExpression, $, args> = validateAst<
102
- ast[0],
103
- $,
104
- args
105
- > extends ErrorMessage<infer message>
106
- ? ErrorMessage<message>
107
- : validateAst<ast[2], $, args> extends ErrorMessage<infer message>
108
- ? ErrorMessage<message>
109
- : undefined
103
+ export type validateString<def extends string, $, args> =
104
+ validateAst<parseString<def, $, args>, $, args> extends (
105
+ infer result extends ErrorMessage
106
+ ) ?
107
+ result extends Completion<infer text> ?
108
+ text
109
+ : result
110
+ : def
111
+
112
+ type validateInfix<ast extends InfixExpression, $, args> =
113
+ validateAst<ast[0], $, args> extends ErrorMessage<infer message> ?
114
+ ErrorMessage<message>
115
+ : validateAst<ast[2], $, args> extends ErrorMessage<infer message> ?
116
+ ErrorMessage<message>
117
+ : undefined
@@ -1,43 +1,48 @@
1
- import type { TypeNode } from "@arktype/schema"
1
+ import type { LimitLiteral, RawSchema } from "@arktype/schema"
2
2
  import {
3
3
  isKeyOf,
4
+ type requireKeys,
4
5
  throwInternalError,
5
- throwParseError,
6
- type requireKeys
6
+ throwParseError
7
7
  } from "@arktype/util"
8
8
  import type { ParseContext } from "../../../scope.js"
9
+ import type { InfixOperator } from "../../semantic/infer.js"
9
10
  import { parseOperand } from "../shift/operand/operand.js"
10
11
  import { parseOperator } from "../shift/operator/operator.js"
11
12
  import { Scanner } from "../shift/scanner.js"
12
13
  import { parseUntilFinalizer } from "../string.js"
13
14
  import {
15
+ type Comparator,
16
+ type MinComparator,
17
+ type OpenLeftBound,
18
+ type StringifiablePrefixOperator,
14
19
  invertedComparators,
15
20
  minComparators,
16
21
  writeMultipleLeftBoundsMessage,
17
22
  writeOpenRangeMessage,
18
23
  writeUnclosedGroupMessage,
19
24
  writeUnmatchedGroupCloseMessage,
20
- writeUnpairableComparatorMessage,
21
- type Comparator,
22
- type LimitLiteral,
23
- type OpenLeftBound,
24
- type StringifiablePrefixOperator
25
+ writeUnpairableComparatorMessage
25
26
  } from "./shared.js"
26
27
 
27
28
  type BranchState = {
28
29
  prefixes: StringifiablePrefixOperator[]
29
- leftBound?: OpenLeftBound
30
- "&"?: TypeNode
31
- "|"?: TypeNode
30
+ leftBound: OpenLeftBound | null
31
+ intersection: RawSchema | null
32
+ union: RawSchema | null
32
33
  }
33
34
 
34
35
  export type DynamicStateWithRoot = requireKeys<DynamicState, "root">
35
36
 
36
37
  export class DynamicState {
37
38
  readonly scanner: Scanner
38
- root: TypeNode | undefined
39
+ // set root type to `any` so that all constraints can be applied
40
+ root: RawSchema<any> | undefined
39
41
  branches: BranchState = {
40
- prefixes: []
42
+ prefixes: [],
43
+ leftBound: null,
44
+ intersection: null,
45
+ union: null
41
46
  }
42
47
  finalizer: Scanner.FinalizingLookahead | undefined
43
48
  groups: BranchState[] = []
@@ -49,7 +54,7 @@ export class DynamicState {
49
54
  this.scanner = new Scanner(def)
50
55
  }
51
56
 
52
- error(message: string) {
57
+ error(message: string): never {
53
58
  return throwParseError(message)
54
59
  }
55
60
 
@@ -57,33 +62,32 @@ export class DynamicState {
57
62
  return this.root !== undefined
58
63
  }
59
64
 
65
+ setRoot(root: RawSchema): void {
66
+ this.root = root
67
+ }
68
+
60
69
  unsetRoot(): this["root"] {
61
70
  const value = this.root
62
71
  this.root = undefined
63
72
  return value
64
73
  }
65
74
 
66
- constrainRoot(...args: Parameters<TypeNode["constrain"]>) {
67
- this.root = this.root!.constrain(...args)
75
+ constrainRoot(...args: Parameters<RawSchema<any>["constrain"]>): void {
76
+ this.root = this.root!.constrain(args[0], args[1])
68
77
  }
69
78
 
70
- setRoot(root: TypeNode) {
71
- this.root = root
72
- }
79
+ finalize(finalizer: Scanner.FinalizingLookahead): void {
80
+ if (this.groups.length) return this.error(writeUnclosedGroupMessage(")"))
73
81
 
74
- finalize(finalizer: Scanner.FinalizingLookahead) {
75
- if (this.groups.length) {
76
- return this.error(writeUnclosedGroupMessage(")"))
77
- }
78
82
  this.finalizeBranches()
79
83
  this.finalizer = finalizer
80
84
  }
81
85
 
82
- reduceLeftBound(limit: LimitLiteral, comparator: Comparator) {
86
+ reduceLeftBound(limit: LimitLiteral, comparator: Comparator): void {
83
87
  const invertedComparator = invertedComparators[comparator]
84
- if (!isKeyOf(invertedComparator, minComparators)) {
88
+ if (!isKeyOf(invertedComparator, minComparators))
85
89
  return this.error(writeUnpairableComparatorMessage(comparator))
86
- }
90
+
87
91
  if (this.branches.leftBound) {
88
92
  return this.error(
89
93
  writeMultipleLeftBoundsMessage(
@@ -100,66 +104,65 @@ export class DynamicState {
100
104
  }
101
105
  }
102
106
 
103
- finalizeBranches() {
107
+ finalizeBranches(): void {
104
108
  this.assertRangeUnset()
105
- if (this.branches["|"]) {
109
+ if (this.branches.union) {
106
110
  this.pushRootToBranch("|")
107
- this.root = this.branches["|"]
108
- } else if (this.branches["&"]) {
111
+ this.root = this.branches.union
112
+ } else if (this.branches.intersection) {
109
113
  this.pushRootToBranch("&")
110
- this.root = this.branches["&"]
111
- } else {
112
- this.applyPrefixes()
113
- }
114
+ this.root = this.branches.intersection
115
+ } else this.applyPrefixes()
114
116
  }
115
117
 
116
- finalizeGroup() {
118
+ finalizeGroup(): void {
117
119
  this.finalizeBranches()
118
120
  const topBranchState = this.groups.pop()
119
- if (!topBranchState) {
121
+ if (!topBranchState)
120
122
  return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned))
121
- }
123
+
122
124
  this.branches = topBranchState
123
125
  }
124
126
 
125
- addPrefix(prefix: StringifiablePrefixOperator) {
127
+ addPrefix(prefix: StringifiablePrefixOperator): void {
126
128
  this.branches.prefixes.push(prefix)
127
129
  }
128
130
 
129
- applyPrefixes() {
131
+ applyPrefixes(): void {
130
132
  while (this.branches.prefixes.length) {
131
133
  const lastPrefix = this.branches.prefixes.pop()!
132
134
  this.root =
133
- lastPrefix === "keyof"
134
- ? this.root!.keyof()
135
- : throwInternalError(`Unexpected prefix '${lastPrefix}'`)
135
+ lastPrefix === "keyof" ?
136
+ this.root!.keyof()
137
+ : throwInternalError(`Unexpected prefix '${lastPrefix}'`)
136
138
  }
137
139
  }
138
140
 
139
- pushRootToBranch(token: "|" | "&") {
141
+ pushRootToBranch(token: "|" | "&"): void {
140
142
  this.assertRangeUnset()
141
143
  this.applyPrefixes()
142
144
  const root = this.root!
143
- this.branches["&"] = this.branches["&"]?.and(root) ?? root
145
+ this.branches.intersection = this.branches.intersection?.and(root) ?? root
144
146
  if (token === "|") {
145
- this.branches["|"] =
146
- this.branches["|"]?.or(this.branches["&"]) ?? this.branches["&"]
147
- delete this.branches["&"]
147
+ this.branches.union =
148
+ this.branches.union?.or(this.branches.intersection) ??
149
+ this.branches.intersection
150
+ this.branches.intersection = null
148
151
  }
149
152
  this.root = undefined
150
153
  }
151
154
 
152
- parseUntilFinalizer() {
155
+ parseUntilFinalizer(): DynamicStateWithRoot {
153
156
  return parseUntilFinalizer(
154
157
  new DynamicState(this.scanner.unscanned, this.ctx)
155
158
  )
156
159
  }
157
160
 
158
- parseOperator(this: DynamicStateWithRoot) {
161
+ parseOperator(this: DynamicStateWithRoot): void {
159
162
  return parseOperator(this)
160
163
  }
161
164
 
162
- parseOperand() {
165
+ parseOperand(): void {
163
166
  return parseOperand(this)
164
167
  }
165
168
 
@@ -174,22 +177,31 @@ export class DynamicState {
174
177
  }
175
178
  }
176
179
 
177
- reduceGroupOpen() {
180
+ reduceGroupOpen(): void {
178
181
  this.groups.push(this.branches)
179
182
  this.branches = {
180
- prefixes: []
183
+ prefixes: [],
184
+ leftBound: null,
185
+ union: null,
186
+ intersection: null
181
187
  }
182
188
  }
183
189
 
184
- previousOperator() {
190
+ previousOperator():
191
+ | MinComparator
192
+ | StringifiablePrefixOperator
193
+ | InfixOperator
194
+ | undefined {
185
195
  return (
186
196
  this.branches.leftBound?.comparator ??
187
197
  this.branches.prefixes.at(-1) ??
188
- (this.branches["&"] ? "&" : this.branches["|"] ? "|" : undefined)
198
+ (this.branches.intersection ? "&"
199
+ : this.branches.union ? "|"
200
+ : undefined)
189
201
  )
190
202
  }
191
203
 
192
- shiftedByOne() {
204
+ shiftedByOne(): this {
193
205
  this.scanner.shift()
194
206
  return this
195
207
  }
@@ -1,4 +1,4 @@
1
- import type { DateLiteral } from "@arktype/schema"
1
+ import type { LimitLiteral } from "@arktype/schema"
2
2
 
3
3
  export type StringifiablePrefixOperator = "keyof"
4
4
 
@@ -34,8 +34,6 @@ export const invertedComparators = {
34
34
 
35
35
  export type InvertedComparators = typeof invertedComparators
36
36
 
37
- export type LimitLiteral = number | DateLiteral
38
-
39
37
  export type OpenLeftBound = { limit: LimitLiteral; comparator: MinComparator }
40
38
 
41
39
  export const writeUnmatchedGroupCloseMessage = <unscanned extends string>(