arktype 2.0.0-dev.9 → 2.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (263) hide show
  1. package/LICENSE +7 -0
  2. package/out/config.d.ts +25 -0
  3. package/out/config.js +2 -0
  4. package/out/generic.d.ts +154 -18
  5. package/out/generic.js +37 -23
  6. package/out/index.d.ts +7 -0
  7. package/out/index.js +6 -0
  8. package/out/intersect.d.ts +28 -0
  9. package/out/intersect.js +1 -0
  10. package/out/keys.d.ts +16 -0
  11. package/out/keys.js +1 -0
  12. package/out/keywords/ark.d.ts +33 -0
  13. package/out/keywords/ark.js +18 -0
  14. package/out/keywords/ast.d.ts +193 -0
  15. package/out/keywords/ast.js +2 -0
  16. package/out/keywords/builtin.d.ts +13 -0
  17. package/out/keywords/builtin.js +10 -0
  18. package/out/keywords/constructors/Array.d.ts +16 -0
  19. package/out/keywords/constructors/Array.js +19 -0
  20. package/out/keywords/constructors/FormData.d.ts +9 -0
  21. package/out/keywords/constructors/FormData.js +25 -0
  22. package/out/keywords/constructors/TypedArray.d.ts +15 -0
  23. package/out/keywords/constructors/TypedArray.js +14 -0
  24. package/out/keywords/constructors/constructors.d.ts +41 -0
  25. package/out/keywords/constructors/constructors.js +31 -0
  26. package/out/keywords/number/epoch.d.ts +10 -0
  27. package/out/keywords/number/epoch.js +26 -0
  28. package/out/keywords/number/integer.d.ts +3 -0
  29. package/out/keywords/number/integer.js +5 -0
  30. package/out/keywords/number/number.d.ts +26 -0
  31. package/out/keywords/number/number.js +9 -0
  32. package/out/keywords/string/alpha.d.ts +7 -0
  33. package/out/keywords/string/alpha.js +2 -0
  34. package/out/keywords/string/alphanumeric.d.ts +7 -0
  35. package/out/keywords/string/alphanumeric.js +2 -0
  36. package/out/keywords/string/capitalize.d.ts +14 -0
  37. package/out/keywords/string/capitalize.js +12 -0
  38. package/out/keywords/string/creditCard.d.ts +8 -0
  39. package/out/keywords/string/creditCard.js +37 -0
  40. package/out/keywords/string/date.d.ts +54 -0
  41. package/out/keywords/string/date.js +98 -0
  42. package/out/keywords/string/digits.d.ts +7 -0
  43. package/out/keywords/string/digits.js +2 -0
  44. package/out/keywords/string/email.d.ts +7 -0
  45. package/out/keywords/string/email.js +4 -0
  46. package/out/keywords/string/integer.d.ts +15 -0
  47. package/out/keywords/string/integer.js +16 -0
  48. package/out/keywords/string/ip.d.ts +20 -0
  49. package/out/keywords/string/ip.js +23 -0
  50. package/out/keywords/string/json.d.ts +14 -0
  51. package/out/keywords/string/json.js +45 -0
  52. package/out/keywords/string/lower.d.ts +14 -0
  53. package/out/keywords/string/lower.js +12 -0
  54. package/out/keywords/string/normalize.d.ts +72 -0
  55. package/out/keywords/string/normalize.js +43 -0
  56. package/out/keywords/string/numeric.d.ts +14 -0
  57. package/out/keywords/string/numeric.js +13 -0
  58. package/out/keywords/string/semver.d.ts +7 -0
  59. package/out/keywords/string/semver.js +4 -0
  60. package/out/keywords/string/string.d.ts +57 -0
  61. package/out/keywords/string/string.js +41 -0
  62. package/out/keywords/string/trim.d.ts +14 -0
  63. package/out/keywords/string/trim.js +14 -0
  64. package/out/keywords/string/upper.d.ts +14 -0
  65. package/out/keywords/string/upper.js +12 -0
  66. package/out/keywords/string/url.d.ts +14 -0
  67. package/out/keywords/string/url.js +37 -0
  68. package/out/keywords/string/utils.d.ts +2 -0
  69. package/out/keywords/string/utils.js +10 -0
  70. package/out/keywords/string/uuid.d.ts +38 -0
  71. package/out/keywords/string/uuid.js +19 -0
  72. package/out/keywords/ts.d.ts +53 -0
  73. package/out/keywords/ts.js +51 -0
  74. package/out/keywords/utils.d.ts +1 -0
  75. package/out/keywords/utils.js +2 -0
  76. package/out/module.d.ts +21 -9
  77. package/out/module.js +3 -3
  78. package/out/parser/definition.d.ts +20 -15
  79. package/out/parser/definition.js +5 -5
  80. package/out/parser/objectLiteral.d.ts +53 -35
  81. package/out/parser/objectLiteral.js +75 -50
  82. package/out/parser/semantic/bounds.d.ts +10 -9
  83. package/out/parser/semantic/default.d.ts +8 -0
  84. package/out/parser/semantic/default.js +1 -0
  85. package/out/parser/semantic/divisor.d.ts +5 -5
  86. package/out/parser/semantic/divisor.js +1 -0
  87. package/out/parser/semantic/infer.d.ts +21 -12
  88. package/out/parser/semantic/infer.js +1 -0
  89. package/out/parser/semantic/keyof.d.ts +7 -0
  90. package/out/parser/semantic/keyof.js +1 -0
  91. package/out/parser/semantic/utils.d.ts +3 -3
  92. package/out/parser/semantic/utils.js +1 -0
  93. package/out/parser/semantic/validate.d.ts +21 -17
  94. package/out/parser/semantic/validate.js +0 -1
  95. package/out/parser/string/reduce/dynamic.d.ts +16 -14
  96. package/out/parser/string/reduce/dynamic.js +8 -7
  97. package/out/parser/string/reduce/shared.d.ts +15 -15
  98. package/out/parser/string/reduce/shared.js +4 -2
  99. package/out/parser/string/reduce/static.d.ts +22 -23
  100. package/out/parser/string/reduce/static.js +1 -0
  101. package/out/parser/string/shift/operand/date.d.ts +2 -4
  102. package/out/parser/string/shift/operand/date.js +1 -1
  103. package/out/parser/string/shift/operand/enclosed.d.ts +10 -6
  104. package/out/parser/string/shift/operand/enclosed.js +5 -4
  105. package/out/parser/string/shift/operand/genericArgs.d.ts +13 -13
  106. package/out/parser/string/shift/operand/genericArgs.js +7 -12
  107. package/out/parser/string/shift/operand/operand.d.ts +8 -7
  108. package/out/parser/string/shift/operand/operand.js +5 -6
  109. package/out/parser/string/shift/operand/unenclosed.d.ts +29 -16
  110. package/out/parser/string/shift/operand/unenclosed.js +9 -12
  111. package/out/parser/string/shift/operator/bounds.d.ts +15 -20
  112. package/out/parser/string/shift/operator/bounds.js +11 -16
  113. package/out/parser/string/shift/operator/default.d.ts +19 -0
  114. package/out/parser/string/shift/operator/default.js +17 -0
  115. package/out/parser/string/shift/operator/divisor.d.ts +3 -3
  116. package/out/parser/string/shift/operator/divisor.js +1 -1
  117. package/out/parser/string/shift/operator/operator.d.ts +7 -6
  118. package/out/parser/string/shift/operator/operator.js +1 -1
  119. package/out/parser/string/shift/scanner.d.ts +26 -30
  120. package/out/parser/string/shift/scanner.js +23 -20
  121. package/out/parser/string/string.d.ts +14 -11
  122. package/out/parser/string/string.js +5 -2
  123. package/out/parser/tuple.d.ts +21 -19
  124. package/out/parser/tuple.js +12 -23
  125. package/out/scope.d.ts +102 -55
  126. package/out/scope.js +61 -60
  127. package/out/subtypes/array.d.ts +13 -0
  128. package/out/subtypes/array.js +1 -0
  129. package/out/subtypes/base.d.ts +84 -0
  130. package/out/subtypes/base.js +1 -0
  131. package/out/subtypes/date.d.ts +11 -0
  132. package/out/subtypes/date.js +1 -0
  133. package/out/subtypes/instantiate.d.ts +16 -0
  134. package/out/subtypes/instantiate.js +1 -0
  135. package/out/subtypes/morph.d.ts +13 -0
  136. package/out/subtypes/morph.js +1 -0
  137. package/out/subtypes/number.d.ts +12 -0
  138. package/out/subtypes/number.js +1 -0
  139. package/out/subtypes/object.d.ts +33 -0
  140. package/out/subtypes/object.js +1 -0
  141. package/out/subtypes/string.d.ts +13 -0
  142. package/out/subtypes/string.js +1 -0
  143. package/out/subtypes/validator.d.ts +11 -0
  144. package/out/subtypes/validator.js +1 -0
  145. package/out/type.d.ts +30 -49
  146. package/out/type.js +19 -14
  147. package/package.json +46 -45
  148. package/__tests__/array.test.ts +0 -391
  149. package/__tests__/badDefinitionType.test.ts +0 -58
  150. package/__tests__/basis.test.ts +0 -50
  151. package/__tests__/bounds.test.ts +0 -318
  152. package/__tests__/cast.test.ts +0 -37
  153. package/__tests__/completions.test.ts +0 -51
  154. package/__tests__/config.test.ts +0 -96
  155. package/__tests__/dateLiteral.test.ts +0 -36
  156. package/__tests__/declared.test.ts +0 -124
  157. package/__tests__/discrimination.test.ts +0 -74
  158. package/__tests__/divisor.test.ts +0 -114
  159. package/__tests__/enclosed.test.ts +0 -107
  160. package/__tests__/examples.test.ts +0 -56
  161. package/__tests__/expressions.test.ts +0 -210
  162. package/__tests__/generateBenchData.ts +0 -89
  163. package/__tests__/generated/cyclic.ts +0 -3058
  164. package/__tests__/generic.test.ts +0 -313
  165. package/__tests__/group.test.ts +0 -85
  166. package/__tests__/hkt.test.ts +0 -22
  167. package/__tests__/imports.test.ts +0 -126
  168. package/__tests__/instanceof.test.ts +0 -104
  169. package/__tests__/intersection.test.ts +0 -108
  170. package/__tests__/keyTraversal.test.ts +0 -77
  171. package/__tests__/keyof.test.ts +0 -150
  172. package/__tests__/keywords.test.ts +0 -214
  173. package/__tests__/literal.test.ts +0 -90
  174. package/__tests__/match.bench.ts +0 -31
  175. package/__tests__/match.test.ts +0 -234
  176. package/__tests__/methods.bench.ts +0 -22
  177. package/__tests__/narrow.test.ts +0 -100
  178. package/__tests__/object.bench.ts +0 -28
  179. package/__tests__/objectLiteral.test.ts +0 -442
  180. package/__tests__/operand.bench.ts +0 -45
  181. package/__tests__/operator.bench.ts +0 -130
  182. package/__tests__/pipe.test.ts +0 -501
  183. package/__tests__/realWorld.test.ts +0 -63
  184. package/__tests__/regex.test.ts +0 -68
  185. package/__tests__/scope.bench.ts +0 -39
  186. package/__tests__/scope.test.ts +0 -374
  187. package/__tests__/serialization.test.ts +0 -19
  188. package/__tests__/string.test.ts +0 -52
  189. package/__tests__/submodule.test.ts +0 -86
  190. package/__tests__/this.test.ts +0 -83
  191. package/__tests__/thunk.test.ts +0 -86
  192. package/__tests__/traverse.test.ts +0 -141
  193. package/__tests__/tsGenerics.test.ts +0 -12
  194. package/__tests__/type.test.ts +0 -36
  195. package/__tests__/typeReference.test.ts +0 -39
  196. package/__tests__/unenclosed.test.ts +0 -136
  197. package/__tests__/union.test.ts +0 -214
  198. package/ark.ts +0 -47
  199. package/generic.ts +0 -69
  200. package/main.ts +0 -6
  201. package/match.ts +0 -218
  202. package/module.ts +0 -15
  203. package/out/ark.d.ts +0 -28
  204. package/out/ark.js +0 -10
  205. package/out/ark.js.map +0 -1
  206. package/out/generic.js.map +0 -1
  207. package/out/main.d.ts +0 -6
  208. package/out/main.js +0 -5
  209. package/out/main.js.map +0 -1
  210. package/out/match.d.ts +0 -71
  211. package/out/match.js +0 -52
  212. package/out/match.js.map +0 -1
  213. package/out/module.js.map +0 -1
  214. package/out/parser/definition.js.map +0 -1
  215. package/out/parser/generic.d.ts +0 -16
  216. package/out/parser/generic.js +0 -15
  217. package/out/parser/generic.js.map +0 -1
  218. package/out/parser/objectLiteral.js.map +0 -1
  219. package/out/parser/semantic/bounds.js.map +0 -1
  220. package/out/parser/semantic/divisor.js.map +0 -1
  221. package/out/parser/semantic/infer.js.map +0 -1
  222. package/out/parser/semantic/utils.js.map +0 -1
  223. package/out/parser/semantic/validate.js.map +0 -1
  224. package/out/parser/string/reduce/dynamic.js.map +0 -1
  225. package/out/parser/string/reduce/shared.js.map +0 -1
  226. package/out/parser/string/reduce/static.js.map +0 -1
  227. package/out/parser/string/shift/operand/date.js.map +0 -1
  228. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  229. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  230. package/out/parser/string/shift/operand/operand.js.map +0 -1
  231. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  232. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  233. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  234. package/out/parser/string/shift/operator/operator.js.map +0 -1
  235. package/out/parser/string/shift/scanner.js.map +0 -1
  236. package/out/parser/string/string.js.map +0 -1
  237. package/out/parser/tuple.js.map +0 -1
  238. package/out/scope.js.map +0 -1
  239. package/out/type.js.map +0 -1
  240. package/parser/definition.ts +0 -143
  241. package/parser/generic.ts +0 -66
  242. package/parser/objectLiteral.ts +0 -233
  243. package/parser/semantic/bounds.ts +0 -62
  244. package/parser/semantic/divisor.ts +0 -11
  245. package/parser/semantic/infer.ts +0 -141
  246. package/parser/semantic/utils.ts +0 -15
  247. package/parser/semantic/validate.ts +0 -117
  248. package/parser/string/reduce/dynamic.ts +0 -208
  249. package/parser/string/reduce/shared.ts +0 -99
  250. package/parser/string/reduce/static.ts +0 -296
  251. package/parser/string/shift/operand/date.ts +0 -55
  252. package/parser/string/shift/operand/enclosed.ts +0 -123
  253. package/parser/string/shift/operand/genericArgs.ts +0 -128
  254. package/parser/string/shift/operand/operand.ts +0 -42
  255. package/parser/string/shift/operand/unenclosed.ts +0 -217
  256. package/parser/string/shift/operator/bounds.ts +0 -270
  257. package/parser/string/shift/operator/divisor.ts +0 -33
  258. package/parser/string/shift/operator/operator.ts +0 -71
  259. package/parser/string/shift/scanner.ts +0 -210
  260. package/parser/string/string.ts +0 -85
  261. package/parser/tuple.ts +0 -542
  262. package/scope.ts +0 -291
  263. package/type.ts +0 -266
package/scope.ts DELETED
@@ -1,291 +0,0 @@
1
- import {
2
- type ArkConfig,
3
- type GenericProps,
4
- type PreparsedNodeResolution,
5
- type PrivateDeclaration,
6
- type RawSchema,
7
- type RawSchemaResolutions,
8
- RawSchemaScope,
9
- type SchemaScope,
10
- type UnknownSchema,
11
- type ambient,
12
- type arkKind,
13
- type destructuredExportContext,
14
- type destructuredImportContext,
15
- type exportedNameOf,
16
- hasArkKind,
17
- type writeDuplicateAliasError
18
- } from "@arktype/schema"
19
- import {
20
- type Dict,
21
- domainOf,
22
- hasDomain,
23
- type isAnyOrNever,
24
- isThunk,
25
- type keyError,
26
- type nominal,
27
- type show,
28
- throwParseError
29
- } from "@arktype/util"
30
- import type { type } from "./ark.js"
31
- import { Generic } from "./generic.js"
32
- import { type MatchParser, createMatchParser } from "./match.js"
33
- import type { Module } from "./module.js"
34
- import {
35
- type inferDefinition,
36
- parseObject,
37
- type validateDefinition,
38
- writeBadDefinitionTypeMessage
39
- } from "./parser/definition.js"
40
- import {
41
- type GenericDeclaration,
42
- type GenericParamsParseError,
43
- parseGenericParams
44
- } from "./parser/generic.js"
45
- import { DynamicState } from "./parser/string/reduce/dynamic.js"
46
- import { fullStringParse } from "./parser/string/string.js"
47
- import {
48
- type DeclarationParser,
49
- type DefinitionParser,
50
- RawTypeParser,
51
- type Type,
52
- type TypeParser
53
- } from "./type.js"
54
-
55
- export type ScopeParser = <const def>(
56
- def: validateScope<def>,
57
- config?: ArkConfig
58
- ) => Scope<inferBootstrapped<bootstrapAliases<def>>>
59
-
60
- type validateScope<def> = {
61
- [k in keyof def]: k extends symbol ?
62
- // this should only occur when importing/exporting modules, and those
63
- // keys should be ignored
64
- unknown
65
- : parseScopeKey<k>["params"] extends [] ?
66
- // Not including Type here directly breaks inference
67
- def[k] extends Type | PreparsedResolution ? def[k]
68
- : k extends PrivateDeclaration<infer name extends keyof def & string> ?
69
- keyError<writeDuplicateAliasError<name>>
70
- : validateDefinition<def[k], ambient & bootstrapAliases<def>, {}>
71
- : parseScopeKey<k>["params"] extends GenericParamsParseError ?
72
- // use the full nominal type here to avoid an overlap between the
73
- // error message and a possible value for the property
74
- parseScopeKey<k>["params"][0]
75
- : validateDefinition<
76
- def[k],
77
- ambient & bootstrapAliases<def>,
78
- {
79
- // once we support constraints on generic parameters, we'd use
80
- // the base type here: https://github.com/arktypeio/arktype/issues/796
81
- [param in parseScopeKey<k>["params"][number]]: unknown
82
- }
83
- >
84
- }
85
-
86
- export type bindThis<def> = { this: Def<def> }
87
-
88
- /** nominal type for an unparsed definition used during scope bootstrapping */
89
- type Def<def = {}> = nominal<def, "unparsed">
90
-
91
- /** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
92
- export type UnparsedScope = "$"
93
-
94
- /** These are legal as values of a scope but not as definitions in other contexts */
95
- type PreparsedResolution = PreparsedNodeResolution
96
-
97
- type bootstrapAliases<def> = {
98
- [k in Exclude<
99
- keyof def,
100
- // avoid inferring nominal symbols, e.g. arkKind from Module
101
- GenericDeclaration | symbol
102
- >]: def[k] extends PreparsedResolution ? def[k]
103
- : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ?
104
- thunkReturn
105
- : Def<def[k]>
106
- } & {
107
- [k in keyof def & GenericDeclaration as extractGenericName<k>]: GenericProps<
108
- parseGenericParams<extractGenericParameters<k>>,
109
- def[k],
110
- UnparsedScope
111
- >
112
- }
113
-
114
- type inferBootstrapped<$> = show<{
115
- [name in keyof $]: $[name] extends Def<infer def> ?
116
- inferDefinition<def, $ & ambient, {}>
117
- : $[name] extends GenericProps<infer params, infer def> ?
118
- // add the scope in which the generic was defined here
119
- Generic<params, def, $>
120
- : // otherwise should be a submodule
121
- $[name]
122
- }>
123
-
124
- type extractGenericName<k> =
125
- k extends GenericDeclaration<infer name> ? name : never
126
-
127
- type extractGenericParameters<k> =
128
- k extends GenericDeclaration<string, infer params> ? params : never
129
-
130
- export type resolve<reference extends keyof $ | keyof args, $, args> =
131
- (
132
- reference extends keyof args ?
133
- args[reference]
134
- : $[reference & keyof $]
135
- ) extends infer resolution ?
136
- isAnyOrNever<resolution> extends true ? resolution
137
- : resolution extends Def<infer def> ? inferDefinition<def, $, args>
138
- : resolution
139
- : never
140
-
141
- export type moduleKeyOf<$> = {
142
- // TODO: check against module directly?
143
- [k in keyof $]: $[k] extends { [arkKind]: "module" } ? k & string : never
144
- }[keyof $]
145
-
146
- export type tryInferSubmoduleReference<$, token> =
147
- token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ?
148
- subalias extends keyof $[submodule] ?
149
- $[submodule][subalias] extends type.cast<infer t> ?
150
- t
151
- : never
152
- : never
153
- : never
154
-
155
- export interface ParseContext {
156
- $: RawScope
157
- args?: Record<string, UnknownSchema>
158
- }
159
-
160
- export const scope: ScopeParser = ((def: Dict, config: ArkConfig = {}) =>
161
- new RawScope(def, config)) as never
162
-
163
- export interface Scope<$ = any> extends SchemaScope<$> {
164
- type: TypeParser<$>
165
-
166
- match: MatchParser<$>
167
-
168
- declare: DeclarationParser<$>
169
-
170
- define: DefinitionParser<$>
171
-
172
- import<names extends exportedNameOf<$>[]>(
173
- ...names: names
174
- ): Module<show<destructuredImportContext<$, names>>>
175
-
176
- export<names extends exportedNameOf<$>[]>(
177
- ...names: names
178
- ): Module<show<destructuredExportContext<$, names>>>
179
- }
180
-
181
- export class RawScope<
182
- $ extends RawSchemaResolutions = RawSchemaResolutions
183
- > extends RawSchemaScope<$> {
184
- private parseCache: Record<string, RawSchema> = {}
185
-
186
- constructor(def: Record<string, unknown>, config?: ArkConfig) {
187
- const aliases: Record<string, unknown> = {}
188
- for (const k in def) {
189
- const parsedKey = parseScopeKey(k)
190
- aliases[parsedKey.name] =
191
- parsedKey.params.length ?
192
- // TODO: this
193
- new Generic(parsedKey.params, def[k], {} as never)
194
- : def[k]
195
- }
196
- super(aliases, config)
197
- }
198
-
199
- type = new RawTypeParser(this as never)
200
-
201
- match: MatchParser<$> = createMatchParser(this as never) as never
202
-
203
- declare = (() => ({
204
- type: this.type
205
- })).bind(this)
206
-
207
- define = ((def: unknown) => def).bind(this)
208
-
209
- override preparseRoot(def: unknown): unknown {
210
- if (isThunk(def) && !hasArkKind(def, "generic")) return def()
211
-
212
- return def
213
- }
214
-
215
- override parseRoot(def: unknown): RawSchema {
216
- // args: { this: {} as RawSchema },
217
- return this.parse(def, {
218
- $: this as never,
219
- args: {}
220
- // type parsing can bypass nodes if it hits the cache,
221
- // so bind it directly (could be optimized)
222
- }).bindScope(this)
223
- }
224
-
225
- parse(def: unknown, ctx: ParseContext): RawSchema {
226
- if (typeof def === "string") {
227
- if (ctx.args && Object.keys(ctx.args).every(k => !def.includes(k))) {
228
- // we can only rely on the cache if there are no contextual
229
- // resolutions like "this" or generic args
230
- return this.parseString(def, ctx)
231
- }
232
- if (!this.parseCache[def])
233
- this.parseCache[def] = this.parseString(def, ctx)
234
-
235
- return this.parseCache[def]
236
- }
237
- return hasDomain(def, "object") ?
238
- parseObject(def, ctx)
239
- : throwParseError(writeBadDefinitionTypeMessage(domainOf(def)))
240
- }
241
-
242
- parseString(def: string, ctx: ParseContext): RawSchema {
243
- return (
244
- this.maybeResolveSchema(def) ??
245
- ((def.endsWith("[]") &&
246
- this.maybeResolveSchema(def.slice(0, -2))?.array()) ||
247
- fullStringParse(new DynamicState(def, ctx)))
248
- )
249
- }
250
- }
251
-
252
- export const writeShallowCycleErrorMessage = (
253
- name: string,
254
- seen: string[]
255
- ): string =>
256
- `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`
257
-
258
- export type ParsedScopeKey = {
259
- name: string
260
- params: string[]
261
- }
262
-
263
- export const parseScopeKey = (k: string): ParsedScopeKey => {
264
- const firstParamIndex = k.indexOf("<")
265
- if (firstParamIndex === -1) {
266
- return {
267
- name: k,
268
- params: []
269
- }
270
- }
271
- if (k.at(-1) !== ">") {
272
- throwParseError(
273
- `'>' must be the last character of a generic declaration in a scope`
274
- )
275
- }
276
- return {
277
- name: k.slice(0, firstParamIndex),
278
- params: parseGenericParams(k.slice(firstParamIndex + 1, -1))
279
- }
280
- }
281
-
282
- export type parseScopeKey<k> =
283
- k extends GenericDeclaration<infer name, infer paramString> ?
284
- {
285
- name: name
286
- params: parseGenericParams<paramString>
287
- }
288
- : {
289
- name: k
290
- params: []
291
- }
package/type.ts DELETED
@@ -1,266 +0,0 @@
1
- import {
2
- ArkErrors,
3
- type BaseMeta,
4
- type BaseRoot,
5
- type Disjoint,
6
- type Morph,
7
- type NodeDef,
8
- type Out,
9
- type Predicate,
10
- type PrimitiveConstraintKind,
11
- RawSchema,
12
- type Schema,
13
- type ambient,
14
- type constrain,
15
- type constraintKindOf,
16
- type distillConstrainableOut,
17
- type distillIn,
18
- type distillOut,
19
- type includesMorphs,
20
- type inferIntersection,
21
- type inferMorphOut,
22
- type inferNarrow,
23
- type inferPipes,
24
- type internalImplementationOf
25
- } from "@arktype/schema"
26
- import {
27
- Callable,
28
- type Constructor,
29
- type array,
30
- type conform
31
- } from "@arktype/util"
32
- import { Generic, type validateParameterString } from "./generic.js"
33
- import type {
34
- inferDefinition,
35
- validateDeclared,
36
- validateDefinition
37
- } from "./parser/definition.js"
38
- import { parseGenericParams } from "./parser/generic.js"
39
- import type {
40
- IndexOneOperator,
41
- IndexZeroOperator,
42
- TupleInfixOperator
43
- } from "./parser/tuple.js"
44
- import type { RawScope, Scope, bindThis } from "./scope.js"
45
-
46
- export interface TypeParserAttachments {
47
- errors: typeof ArkErrors
48
- }
49
-
50
- export interface TypeParser<$> extends TypeParserAttachments {
51
- // Parse and check the definition, returning either the original input for a
52
- // valid definition or a string representing an error message.
53
- <const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>
54
-
55
- // Spread version of a tuple expression
56
- <const zero, const one, const rest extends array>(
57
- _0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>,
58
- _1: zero extends "keyof" ? validateTypeRoot<one, $>
59
- : zero extends "instanceof" ? conform<one, Constructor>
60
- : zero extends "===" ? conform<one, unknown>
61
- : conform<one, IndexOneOperator>,
62
- ..._2: zero extends "===" ? rest
63
- : zero extends "instanceof" ? conform<rest, readonly Constructor[]>
64
- : one extends TupleInfixOperator ?
65
- one extends ":" ? [Predicate<distillIn<inferTypeRoot<zero, $>>>]
66
- : one extends "=>" ? [Morph<distillOut<inferTypeRoot<zero, $>>, unknown>]
67
- : one extends "@" ? [string | BaseMeta]
68
- : [validateTypeRoot<rest[0], $>]
69
- : []
70
- ): Type<inferTypeRoot<[zero, one, ...rest], $>, $>
71
-
72
- <params extends string, const def>(
73
- params: `<${validateParameterString<params>}>`,
74
- def: validateDefinition<
75
- def,
76
- $,
77
- {
78
- [param in parseGenericParams<params>[number]]: unknown
79
- }
80
- >
81
- ): Generic<parseGenericParams<params>, def, $>
82
- }
83
-
84
- const typeParserAttachments = Object.freeze({
85
- errors: ArkErrors
86
- } satisfies TypeParserAttachments)
87
-
88
- export class RawTypeParser extends Callable<
89
- (...args: unknown[]) => RawSchema | Generic,
90
- TypeParserAttachments
91
- > {
92
- constructor($: RawScope) {
93
- super(
94
- (...args) => {
95
- if (args.length === 1) {
96
- // treat as a simple definition
97
- return $.parseRoot(args[0])
98
- }
99
- if (
100
- args.length === 2 &&
101
- typeof args[0] === "string" &&
102
- args[0][0] === "<" &&
103
- args[0].at(-1) === ">"
104
- ) {
105
- // if there are exactly two args, the first of which looks like <${string}>,
106
- // treat as a generic
107
- const params = parseGenericParams(args[0].slice(1, -1))
108
- const def = args[1]
109
- // TODO: validateUninstantiatedGeneric, remove this cast
110
- return new Generic(params, def, $ as never) as never
111
- }
112
- // otherwise, treat as a tuple expression. technically, this also allows
113
- // non-expression tuple definitions to be parsed, but it's not a supported
114
- // part of the API as specified by the associated types
115
- return $.parseRoot(args)
116
- },
117
- { bind: $, attach: typeParserAttachments }
118
- )
119
- }
120
- }
121
-
122
- export type DeclarationParser<$> = <preinferred>() => {
123
- // for some reason, making this a const parameter breaks preinferred validation
124
- type: <def>(
125
- def: validateDeclared<preinferred, def, $ & ambient, bindThis<def>>
126
- ) => Type<preinferred, $>
127
- }
128
-
129
- // this is declared as a class internally so we can ensure all "abstract"
130
- // methods of BaseRoot are overridden, but we end up exporting it as an interface
131
- // to ensure it is not accessed as a runtime value
132
- declare class _Type<t = unknown, $ = any>
133
- extends BaseRoot<t, $>
134
- implements internalImplementationOf<Schema>
135
- {
136
- $: Scope<$>;
137
-
138
- get in(): Type<this["tIn"], $>
139
- get out(): Type<this["tOut"], $>
140
-
141
- intersect<def>(
142
- def: validateTypeRoot<def, $>
143
- ): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint
144
-
145
- and<def>(
146
- def: validateTypeRoot<def, $>
147
- ): Type<inferIntersection<t, inferTypeRoot<def, $>>, $>
148
-
149
- or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $>
150
-
151
- array(): Type<t[], $>
152
-
153
- keyof(): Type<keyof this["inferIn"], $>
154
-
155
- pipe<a extends Morph<this["infer"]>>(a: a): Type<inferPipes<t, [a]>, $>
156
- pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>>(
157
- a: a,
158
- b: b
159
- ): Type<inferPipes<t, [a, b]>, $>
160
- pipe<
161
- a extends Morph<this["infer"]>,
162
- b extends Morph<inferMorphOut<a>>,
163
- c extends Morph<inferMorphOut<b>>
164
- >(a: a, b: b, c: c): Type<inferPipes<t, [a, b, c]>, $>
165
- pipe<
166
- a extends Morph<this["infer"]>,
167
- b extends Morph<inferMorphOut<a>>,
168
- c extends Morph<inferMorphOut<b>>,
169
- d extends Morph<inferMorphOut<c>>
170
- >(a: a, b: b, c: c, d: d): Type<inferPipes<t, [a, b, c, d]>, $>
171
- pipe<
172
- a extends Morph<this["infer"]>,
173
- b extends Morph<inferMorphOut<a>>,
174
- c extends Morph<inferMorphOut<b>>,
175
- d extends Morph<inferMorphOut<c>>,
176
- e extends Morph<inferMorphOut<d>>
177
- >(a: a, b: b, c: c, d: d, e: e): Type<inferPipes<t, [a, b, c, d, e]>, $>
178
- pipe<
179
- a extends Morph<this["infer"]>,
180
- b extends Morph<inferMorphOut<a>>,
181
- c extends Morph<inferMorphOut<b>>,
182
- d extends Morph<inferMorphOut<c>>,
183
- e extends Morph<inferMorphOut<d>>,
184
- f extends Morph<inferMorphOut<e>>
185
- >(
186
- a: a,
187
- b: b,
188
- c: c,
189
- d: d,
190
- e: e,
191
- f: f
192
- ): Type<inferPipes<t, [a, b, c, d, e, f]>, $>
193
- pipe<
194
- a extends Morph<this["infer"]>,
195
- b extends Morph<inferMorphOut<a>>,
196
- c extends Morph<inferMorphOut<b>>,
197
- d extends Morph<inferMorphOut<c>>,
198
- e extends Morph<inferMorphOut<d>>,
199
- f extends Morph<inferMorphOut<e>>,
200
- g extends Morph<inferMorphOut<f>>
201
- >(
202
- a: a,
203
- b: b,
204
- c: c,
205
- d: d,
206
- e: e,
207
- f: f,
208
- g: g
209
- ): Type<inferPipes<t, [a, b, c, d, e, f, g]>, $>
210
-
211
- // TODO: based on below, should maybe narrow morph output if used after
212
- narrow<def extends Predicate<distillConstrainableOut<t>>>(
213
- def: def
214
- ): Type<
215
- includesMorphs<t> extends true ?
216
- (In: this["tIn"]) => Out<inferNarrow<this["infer"], def>>
217
- : inferNarrow<this["infer"], def>,
218
- $
219
- >
220
-
221
- equals<def>(
222
- def: validateTypeRoot<def, $>
223
- ): this is Type<inferTypeRoot<def>, $>
224
-
225
- // TODO: i/o
226
- extract<def>(r: validateTypeRoot<def, $>): Type<t, $>
227
- exclude<def>(r: validateTypeRoot<def, $>): Type<t, $>
228
- extends<def>(
229
- other: validateTypeRoot<def, $>
230
- ): this is Type<inferTypeRoot<def>, $>
231
-
232
- constrain<
233
- kind extends PrimitiveConstraintKind,
234
- const def extends NodeDef<kind>
235
- >(
236
- kind: conform<kind, constraintKindOf<this["inferIn"]>>,
237
- def: def
238
- ): Type<constrain<t, kind, def>, $>
239
- }
240
-
241
- export interface Type<
242
- /** @ts-expect-error allow instantiation assignment to the base type */
243
- out t = unknown,
244
- $ = any
245
- > extends _Type<t, $> {}
246
-
247
- export type TypeConstructor<t = unknown, $ = any> = new (
248
- def: unknown,
249
- $: Scope<$>
250
- ) => Type<t, $>
251
-
252
- export const Type: TypeConstructor = RawSchema as never
253
-
254
- export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def
255
-
256
- export type validateTypeRoot<def, $ = {}> = validateDefinition<
257
- def,
258
- $ & ambient,
259
- bindThis<def>
260
- >
261
-
262
- export type inferTypeRoot<def, $ = {}> = inferDefinition<
263
- def,
264
- $ & ambient,
265
- bindThis<def>
266
- >