arktype 1.1.0-dev-1 → 2.0.0-dev.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 (213) hide show
  1. package/__tests__/array.test.ts +272 -0
  2. package/__tests__/astUtils.test.ts +17 -0
  3. package/__tests__/badDefinitionType.test.ts +51 -0
  4. package/__tests__/basis.test.ts +46 -0
  5. package/__tests__/bounds.test.ts +388 -0
  6. package/__tests__/cast.test.ts +32 -0
  7. package/__tests__/completions.test.ts +46 -0
  8. package/__tests__/config.test.ts +168 -0
  9. package/__tests__/cyclic.test.ts +144 -0
  10. package/__tests__/dateLiteral.test.ts +32 -0
  11. package/__tests__/declared.test.ts +123 -0
  12. package/__tests__/discrimination.test.ts +74 -0
  13. package/__tests__/divisor.test.ts +90 -0
  14. package/__tests__/enclosed.test.ts +93 -0
  15. package/__tests__/examples.test.ts +56 -0
  16. package/__tests__/expressions.test.ts +138 -0
  17. package/__tests__/generateBenchData.ts +89 -0
  18. package/__tests__/generated/cyclic.ts +3058 -0
  19. package/__tests__/generic.test.ts +294 -0
  20. package/__tests__/group.test.ts +72 -0
  21. package/__tests__/hkt.test.ts +22 -0
  22. package/__tests__/imports.test.ts +113 -0
  23. package/__tests__/instanceof.test.ts +102 -0
  24. package/__tests__/intersection.test.ts +104 -0
  25. package/__tests__/jsdoc.test.ts +64 -0
  26. package/__tests__/keyTraversal.test.ts +73 -0
  27. package/__tests__/keyof.test.ts +134 -0
  28. package/__tests__/keywords.test.ts +188 -0
  29. package/__tests__/literal.test.ts +51 -0
  30. package/__tests__/match.test.ts +34 -0
  31. package/__tests__/methods.bench.ts +22 -0
  32. package/__tests__/morph.test.ts +409 -0
  33. package/__tests__/narrow.test.ts +102 -0
  34. package/__tests__/nodeDefinitions.test.ts +190 -0
  35. package/__tests__/object.bench.ts +28 -0
  36. package/__tests__/objectLiteral.test.ts +300 -0
  37. package/__tests__/operand.bench.ts +45 -0
  38. package/__tests__/operator.bench.ts +130 -0
  39. package/__tests__/regex.test.ts +58 -0
  40. package/__tests__/scope.bench.ts +39 -0
  41. package/__tests__/scope.test.ts +140 -0
  42. package/__tests__/string.test.ts +48 -0
  43. package/__tests__/submodule.test.ts +99 -0
  44. package/__tests__/this.test.ts +57 -0
  45. package/__tests__/thunk.test.ts +80 -0
  46. package/__tests__/traverse.test.ts +119 -0
  47. package/__tests__/tsGenerics.test.ts +12 -0
  48. package/__tests__/typeMethods.test.ts +69 -0
  49. package/__tests__/typeReference.test.ts +39 -0
  50. package/__tests__/unenclosed.test.ts +120 -0
  51. package/__tests__/union.test.ts +147 -0
  52. package/ark.ts +91 -0
  53. package/main.ts +18 -0
  54. package/match.ts +116 -0
  55. package/out/ark.d.ts +52 -0
  56. package/out/ark.d.ts.map +1 -0
  57. package/out/ark.js +19 -0
  58. package/out/ark.js.map +1 -0
  59. package/out/arkConfig.d.ts +2 -0
  60. package/out/arkConfig.d.ts.map +1 -0
  61. package/out/arkConfig.js +10 -0
  62. package/out/arkConfig.js.map +1 -0
  63. package/out/main.d.ts +7 -0
  64. package/out/main.d.ts.map +1 -0
  65. package/out/main.js +4 -0
  66. package/out/main.js.map +1 -0
  67. package/out/match.d.ts +48 -0
  68. package/out/match.d.ts.map +1 -0
  69. package/out/match.js +18 -0
  70. package/out/match.js.map +1 -0
  71. package/out/parser/definition.d.ts +32 -0
  72. package/out/parser/definition.d.ts.map +1 -0
  73. package/out/parser/definition.js +32 -0
  74. package/out/parser/definition.js.map +1 -0
  75. package/out/parser/generic.d.ts +18 -0
  76. package/out/parser/generic.d.ts.map +1 -0
  77. package/out/parser/generic.js +19 -0
  78. package/out/parser/generic.js.map +1 -0
  79. package/out/parser/objectLiteral.d.ts +33 -0
  80. package/out/parser/objectLiteral.d.ts.map +1 -0
  81. package/out/parser/objectLiteral.js +66 -0
  82. package/out/parser/objectLiteral.js.map +1 -0
  83. package/out/parser/semantic/bounds.d.ts +13 -0
  84. package/out/parser/semantic/bounds.d.ts.map +1 -0
  85. package/out/parser/semantic/bounds.js +2 -0
  86. package/out/parser/semantic/bounds.js.map +1 -0
  87. package/out/parser/semantic/divisor.d.ts +9 -0
  88. package/out/parser/semantic/divisor.d.ts.map +1 -0
  89. package/out/parser/semantic/divisor.js +2 -0
  90. package/out/parser/semantic/divisor.js.map +1 -0
  91. package/out/parser/semantic/intersections.d.ts +14 -0
  92. package/out/parser/semantic/intersections.d.ts.map +1 -0
  93. package/out/parser/semantic/intersections.js +2 -0
  94. package/out/parser/semantic/intersections.js.map +1 -0
  95. package/out/parser/semantic/semantic.d.ts +33 -0
  96. package/out/parser/semantic/semantic.d.ts.map +1 -0
  97. package/out/parser/semantic/semantic.js +2 -0
  98. package/out/parser/semantic/semantic.js.map +1 -0
  99. package/out/parser/semantic/utils.d.ts +8 -0
  100. package/out/parser/semantic/utils.d.ts.map +1 -0
  101. package/out/parser/semantic/utils.js +2 -0
  102. package/out/parser/semantic/utils.js.map +1 -0
  103. package/out/parser/semantic/validate.d.ts +24 -0
  104. package/out/parser/semantic/validate.d.ts.map +1 -0
  105. package/out/parser/semantic/validate.js +2 -0
  106. package/out/parser/semantic/validate.js.map +1 -0
  107. package/out/parser/shared.d.ts +54 -0
  108. package/out/parser/shared.d.ts.map +1 -0
  109. package/out/parser/shared.js +47 -0
  110. package/out/parser/shared.js.map +1 -0
  111. package/out/parser/string/reduce/dynamic.d.ts +42 -0
  112. package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
  113. package/out/parser/string/reduce/dynamic.js +133 -0
  114. package/out/parser/string/reduce/dynamic.js.map +1 -0
  115. package/out/parser/string/reduce/shared.d.ts +56 -0
  116. package/out/parser/string/reduce/shared.d.ts.map +1 -0
  117. package/out/parser/string/reduce/shared.js +26 -0
  118. package/out/parser/string/reduce/shared.js.map +1 -0
  119. package/out/parser/string/reduce/static.d.ts +176 -0
  120. package/out/parser/string/reduce/static.d.ts.map +1 -0
  121. package/out/parser/string/reduce/static.js +2 -0
  122. package/out/parser/string/reduce/static.js.map +1 -0
  123. package/out/parser/string/shift/operand/date.d.ts +11 -0
  124. package/out/parser/string/shift/operand/date.d.ts.map +1 -0
  125. package/out/parser/string/shift/operand/date.js +26 -0
  126. package/out/parser/string/shift/operand/date.js.map +1 -0
  127. package/out/parser/string/shift/operand/enclosed.d.ts +49 -0
  128. package/out/parser/string/shift/operand/enclosed.d.ts.map +1 -0
  129. package/out/parser/string/shift/operand/enclosed.js +50 -0
  130. package/out/parser/string/shift/operand/enclosed.js.map +1 -0
  131. package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
  132. package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
  133. package/out/parser/string/shift/operand/genericArgs.js +25 -0
  134. package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
  135. package/out/parser/string/shift/operand/operand.d.ts +9 -0
  136. package/out/parser/string/shift/operand/operand.d.ts.map +1 -0
  137. package/out/parser/string/shift/operand/operand.js +17 -0
  138. package/out/parser/string/shift/operand/operand.js.map +1 -0
  139. package/out/parser/string/shift/operand/unenclosed.d.ts +34 -0
  140. package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
  141. package/out/parser/string/shift/operand/unenclosed.js +68 -0
  142. package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
  143. package/out/parser/string/shift/operator/bounds.d.ts +33 -0
  144. package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
  145. package/out/parser/string/shift/operator/bounds.js +106 -0
  146. package/out/parser/string/shift/operator/bounds.js.map +1 -0
  147. package/out/parser/string/shift/operator/divisor.d.ts +9 -0
  148. package/out/parser/string/shift/operator/divisor.d.ts.map +1 -0
  149. package/out/parser/string/shift/operator/divisor.js +13 -0
  150. package/out/parser/string/shift/operator/divisor.js.map +1 -0
  151. package/out/parser/string/shift/operator/operator.d.ts +13 -0
  152. package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
  153. package/out/parser/string/shift/operator/operator.js +29 -0
  154. package/out/parser/string/shift/operator/operator.js.map +1 -0
  155. package/out/parser/string/shift/scanner.d.ts +71 -0
  156. package/out/parser/string/shift/scanner.d.ts.map +1 -0
  157. package/out/parser/string/shift/scanner.js +99 -0
  158. package/out/parser/string/shift/scanner.js.map +1 -0
  159. package/out/parser/string/string.d.ts +24 -0
  160. package/out/parser/string/string.d.ts.map +1 -0
  161. package/out/parser/string/string.js +23 -0
  162. package/out/parser/string/string.js.map +1 -0
  163. package/out/parser/tuple.d.ts +71 -0
  164. package/out/parser/tuple.d.ts.map +1 -0
  165. package/out/parser/tuple.js +137 -0
  166. package/out/parser/tuple.js.map +1 -0
  167. package/out/scope.d.ts +159 -0
  168. package/out/scope.d.ts.map +1 -0
  169. package/out/scope.js +253 -0
  170. package/out/scope.js.map +1 -0
  171. package/out/test.d.ts +2 -0
  172. package/out/test.d.ts.map +1 -0
  173. package/out/test.js +7 -0
  174. package/out/test.js.map +1 -0
  175. package/out/tsconfig.build.tsbuildinfo +1 -0
  176. package/out/type.d.ts +77 -0
  177. package/out/type.d.ts.map +1 -0
  178. package/out/type.js +134 -0
  179. package/out/type.js.map +1 -0
  180. package/out/util.d.ts +19 -0
  181. package/out/util.d.ts.map +1 -0
  182. package/out/util.js +7 -0
  183. package/out/util.js.map +1 -0
  184. package/package.json +13 -10
  185. package/parser/definition.ts +159 -0
  186. package/parser/generic.ts +68 -0
  187. package/parser/objectLiteral.ts +197 -0
  188. package/parser/semantic/bounds.ts +61 -0
  189. package/parser/semantic/divisor.ts +13 -0
  190. package/parser/semantic/intersections.ts +46 -0
  191. package/parser/semantic/semantic.ts +144 -0
  192. package/parser/semantic/utils.ts +26 -0
  193. package/parser/semantic/validate.ts +109 -0
  194. package/parser/shared.ts +152 -0
  195. package/parser/string/reduce/dynamic.ts +196 -0
  196. package/parser/string/reduce/shared.ts +101 -0
  197. package/parser/string/reduce/static.ts +298 -0
  198. package/parser/string/shift/operand/date.ts +54 -0
  199. package/parser/string/shift/operand/enclosed.ts +118 -0
  200. package/parser/string/shift/operand/genericArgs.ts +127 -0
  201. package/parser/string/shift/operand/operand.ts +48 -0
  202. package/parser/string/shift/operand/unenclosed.ts +268 -0
  203. package/parser/string/shift/operator/bounds.ts +264 -0
  204. package/parser/string/shift/operator/divisor.ts +36 -0
  205. package/parser/string/shift/operator/operator.ts +82 -0
  206. package/parser/string/shift/scanner.ts +209 -0
  207. package/parser/string/string.ts +90 -0
  208. package/parser/tuple.ts +474 -0
  209. package/scope.ts +574 -0
  210. package/type.ts +347 -0
  211. package/util.ts +36 -0
  212. package/dist/main.d.ts +0 -1018
  213. package/dist/main.js +0 -1450
package/type.ts ADDED
@@ -0,0 +1,347 @@
1
+ import {
2
+ inferred,
3
+ keywords,
4
+ type ArkResult,
5
+ type BaseMeta,
6
+ type KeyCheckKind,
7
+ type Morph,
8
+ type Out,
9
+ type Predicate,
10
+ type TypeNode,
11
+ type distill,
12
+ type extractIn,
13
+ type extractOut,
14
+ type includesMorphs,
15
+ type inferMorphOut,
16
+ type inferNarrow
17
+ } from "@arktype/schema"
18
+ import {
19
+ Callable,
20
+ CastableBase,
21
+ map,
22
+ type Constructor,
23
+ type Json,
24
+ type conform
25
+ } from "@arktype/util"
26
+ import type {
27
+ inferDefinition,
28
+ validateDeclared,
29
+ validateDefinition
30
+ } from "./parser/definition.js"
31
+ import {
32
+ parseGenericParams,
33
+ type GenericParamsParseError
34
+ } from "./parser/generic.js"
35
+ import type { inferIntersection } from "./parser/semantic/intersections.js"
36
+ import type {
37
+ IndexOneOperator,
38
+ IndexZeroOperator,
39
+ TupleInfixOperator
40
+ } from "./parser/tuple.js"
41
+ import type { Scope, bindThis } from "./scope.js"
42
+ import { arkKind } from "./util.js"
43
+
44
+ export type TypeParser<$> = {
45
+ // Parse and check the definition, returning either the original input for a
46
+ // valid definition or a string representing an error message.
47
+ <const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>
48
+
49
+ // Spread version of a tuple expression
50
+ <const zero, const one, const rest extends readonly unknown[]>(
51
+ _0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>,
52
+ _1: zero extends "keyof"
53
+ ? validateTypeRoot<one, $>
54
+ : zero extends "instanceof"
55
+ ? conform<one, Constructor>
56
+ : zero extends "==="
57
+ ? conform<one, unknown>
58
+ : conform<one, IndexOneOperator>,
59
+ ..._2: zero extends "==="
60
+ ? rest
61
+ : zero extends "instanceof"
62
+ ? conform<rest, readonly Constructor[]>
63
+ : one extends TupleInfixOperator
64
+ ? one extends ":"
65
+ ? [Predicate<extractIn<inferTypeRoot<zero, $>>>]
66
+ : one extends "=>"
67
+ ? // TODO: centralize
68
+ [Morph<extractOut<inferTypeRoot<zero, $>>, unknown>]
69
+ : one extends "@"
70
+ ? [string | BaseMeta]
71
+ : [validateTypeRoot<rest[0], $>]
72
+ : []
73
+ ): Type<inferTypeRoot<[zero, one, ...rest], $>, $>
74
+
75
+ <params extends string, const def>(
76
+ params: `<${validateParameterString<params>}>`,
77
+ def: validateDefinition<
78
+ def,
79
+ $,
80
+ {
81
+ [param in parseGenericParams<params>[number]]: unknown
82
+ }
83
+ >
84
+ ): Generic<parseGenericParams<params>, def, $>
85
+ }
86
+
87
+ export type DeclarationParser<$> = <preinferred>() => {
88
+ // for some reason, making this a const parameter breaks preinferred validation
89
+ type: <def>(
90
+ def: validateDeclared<preinferred, def, $, bindThis<def>>
91
+ ) => Type<preinferred, $>
92
+ }
93
+
94
+ export const createTypeParser = <$>(scope: Scope): TypeParser<$> => {
95
+ const parser = (...args: unknown[]): Type | Generic => {
96
+ if (args.length === 1) {
97
+ // treat as a simple definition
98
+ return new Type(args[0], scope)
99
+ }
100
+ if (
101
+ args.length === 2 &&
102
+ typeof args[0] === "string" &&
103
+ args[0][0] === "<" &&
104
+ args[0].at(-1) === ">"
105
+ ) {
106
+ // if there are exactly two args, the first of which looks like <${string}>,
107
+ // treat as a generic
108
+ const params = parseGenericParams(args[0].slice(1, -1))
109
+ const def = args[1]
110
+ return validateUninstantiatedGeneric(generic(params, def, scope) 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 new Type(args, scope)
116
+ }
117
+ return parser as never
118
+ }
119
+
120
+ export type DefinitionParser<$> = <def>(
121
+ def: validateDefinition<def, $, bindThis<def>>
122
+ ) => def
123
+
124
+ export type TypeConfig = {
125
+ keys?: KeyCheckKind
126
+ mustBe?: string
127
+ }
128
+
129
+ export class Type<t = unknown, $ = any> extends Callable<
130
+ (data: unknown) => ArkResult<distill<extractOut<t>>>
131
+ > {
132
+ declare [inferred]: t
133
+ // TODO: in/out?
134
+ declare infer: distill<extractOut<t>>
135
+
136
+ config: TypeConfig
137
+ root: TypeNode<t>
138
+ allows: this["root"]["allows"]
139
+ expected: string
140
+ json: Json
141
+
142
+ constructor(
143
+ public definition: unknown,
144
+ public scope: Scope
145
+ ) {
146
+ const root = parseTypeRoot(definition, scope) as TypeNode<t>
147
+ super(root.apply, root)
148
+ this.root = root
149
+ this.allows = root.allows
150
+ this.config = scope.config
151
+ this.json = root.json
152
+ this.expected = this.root.description
153
+ }
154
+
155
+ configure(config: TypeConfig) {
156
+ this.config = { ...this.config, ...config }
157
+ return this
158
+ }
159
+
160
+ // TODO: should return out
161
+ from(literal: this["in"]["infer"]) {
162
+ return literal
163
+ }
164
+
165
+ // TODO: Morph intersections, ordering
166
+ and<def>(
167
+ def: validateTypeRoot<def, $>
168
+ ): Type<inferIntersection<t, inferTypeRoot<def, $>>, $> {
169
+ return new Type(
170
+ this.root.and(parseTypeRoot(def, this.scope)),
171
+ this.scope
172
+ ) as never
173
+ }
174
+
175
+ or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $> {
176
+ return new Type(
177
+ this.root.or(parseTypeRoot(def, this.scope)),
178
+ this.scope
179
+ ) as never
180
+ }
181
+
182
+ // TODO: standardize these
183
+ morph<morph extends Morph<this["infer"]>>(
184
+ morph: morph
185
+ ): Type<(In: this["in"]["infer"]) => Out<inferMorphOut<ReturnType<morph>>>, $>
186
+ morph<morph extends Morph<this["infer"]>, def>(
187
+ morph: morph,
188
+ outValidator: validateTypeRoot<def, $>
189
+ ): Type<
190
+ (In: this["in"]["infer"]) => Out<
191
+ // TODO: validate overlapping
192
+ // inferMorphOut<ReturnType<morph>> &
193
+ extractOut<inferTypeRoot<def, $>>
194
+ >,
195
+ $
196
+ >
197
+ morph(morph: Morph, outValidator?: unknown) {
198
+ // TODO: tuple expression for out validator
199
+ outValidator
200
+ return this as never
201
+ // return new Type(
202
+ // this.root.constrain("morph", morph),
203
+ // this.scope
204
+ // ) as never
205
+ }
206
+
207
+ // TODO: based on below, should maybe narrow morph output if used after
208
+ narrow<def extends Predicate<extractOut<t>>>(
209
+ def: def
210
+ ): Type<
211
+ includesMorphs<t> extends true
212
+ ? (In: this["in"]["infer"]) => Out<inferNarrow<this["infer"], def>>
213
+ : inferNarrow<this["infer"], def>,
214
+ $
215
+ > {
216
+ return new Type(this.root.constrain("predicate", def), this.scope) as never
217
+ }
218
+
219
+ array(): Type<t[], $> {
220
+ return new Type(this.root.array(), this.scope) as never
221
+ }
222
+
223
+ keyof(): Type<keyof this["in"]["infer"], $> {
224
+ return new Type(this.root.keyof(), this.scope) as never
225
+ }
226
+
227
+ assert(data: unknown): this["infer"] {
228
+ const result = this.call(null, data)
229
+ return result.errors ? result.errors.throw() : result.out
230
+ }
231
+
232
+ equals<def>(
233
+ other: validateTypeRoot<def, $>
234
+ ): this is Type<inferTypeRoot<def, $>, $> {
235
+ return this.root.equals(parseTypeRoot(other, this.scope))
236
+ }
237
+
238
+ extends<def>(
239
+ other: validateTypeRoot<def, $>
240
+ ): this is Type<inferTypeRoot<def, $>, $> {
241
+ return this.root.extends(parseTypeRoot(other, this.scope))
242
+ }
243
+
244
+ private inCache?: Type<extractIn<t>, $>;
245
+ get in() {
246
+ this.inCache ??= new Type(this.root.in, this.scope) as never
247
+ return this.inCache
248
+ }
249
+
250
+ outCache?: Type<extractOut<t>, $>
251
+ get out() {
252
+ this.outCache ??= new Type(this.root.out, this.scope) as never
253
+ return this.outCache
254
+ }
255
+ }
256
+
257
+ const parseTypeRoot = (def: unknown, scope: Scope, args?: BoundArgs) =>
258
+ scope.parseDefinition(def, {
259
+ args: args ?? scope.bindThis(),
260
+ baseName: "type"
261
+ })
262
+
263
+ export type validateTypeRoot<def, $> = validateDefinition<def, $, bindThis<def>>
264
+
265
+ export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>
266
+
267
+ type validateParameterString<params extends string> =
268
+ parseGenericParams<params> extends GenericParamsParseError<infer message>
269
+ ? message
270
+ : params
271
+
272
+ export const validateUninstantiatedGeneric = (g: Generic) => {
273
+ // the unconstrained instantiation of the generic is not used for now
274
+ // other than to eagerly validate that the def does not contain any errors
275
+ g.scope.parseDefinition(
276
+ g.definition,
277
+ // once we support constraints on generic parameters, we'd use
278
+ // the base type here: https://github.com/arktypeio/arktype/issues/796
279
+ {
280
+ baseName: "generic",
281
+ args: map(g.parameters, (_, name) => [name, keywords.unknown])
282
+ }
283
+ )
284
+ return g
285
+ }
286
+
287
+ export const generic = (
288
+ parameters: string[],
289
+ definition: unknown,
290
+ scope: Scope
291
+ ) => {
292
+ return Object.assign(
293
+ (...args: unknown[]) => {
294
+ const argNodes = map(parameters, (i, param) => [
295
+ param,
296
+ parseTypeRoot(args[i], scope)
297
+ ])
298
+ const root = parseTypeRoot(definition, scope, argNodes)
299
+ return new Type(root, scope)
300
+ },
301
+ {
302
+ [arkKind]: "generic",
303
+ parameters,
304
+ definition,
305
+ scope
306
+ // $ is only needed at compile-time
307
+ } satisfies Omit<GenericProps, "$">
308
+ ) as unknown as Generic
309
+ }
310
+
311
+ // Comparing to Generic directly doesn't work well, so we compare to only its props
312
+ export type GenericProps<
313
+ params extends string[] = string[],
314
+ def = unknown,
315
+ $ = any
316
+ > = {
317
+ [arkKind]: "generic"
318
+ $: $
319
+ parameters: params
320
+ definition: def
321
+ scope: Scope
322
+ }
323
+
324
+ export type BoundArgs = Record<string, TypeNode>
325
+
326
+ // TODO: Fix external reference (i.e. if this is attached to a scope, then args are defined using it)
327
+ export type Generic<
328
+ params extends string[] = string[],
329
+ def = unknown,
330
+ $ = any
331
+ > = {
332
+ <args>(
333
+ ...args: conform<
334
+ args,
335
+ {
336
+ [i in keyof params]: validateTypeRoot<args[i & keyof args], $>
337
+ }
338
+ >
339
+ ): Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>
340
+ } & GenericProps<params, def, $>
341
+
342
+ type bindGenericInstantiation<params extends string[], $, args> = {
343
+ [i in keyof params & `${number}` as params[i]]: inferTypeRoot<
344
+ args[i & keyof args],
345
+ $
346
+ >
347
+ }
package/util.ts ADDED
@@ -0,0 +1,36 @@
1
+ import type { Module, Scope } from "./scope.js"
2
+ import type { Generic, Type } from "./type.js"
3
+
4
+ export const arkKind = Symbol("ArkTypeInternalKind")
5
+
6
+ export type ArkKinds = {
7
+ type: Type
8
+ scope: Scope
9
+ generic: Generic
10
+ module: Module
11
+ }
12
+
13
+ export type ArkKind = keyof ArkKinds
14
+
15
+ export const addArkKind = <kind extends ArkKind>(
16
+ value: Omit<ArkKinds[kind], arkKind> & {
17
+ [arkKind]?: kind
18
+ },
19
+ kind: kind
20
+ ): ArkKinds[kind] =>
21
+ Object.defineProperty(value, arkKind, {
22
+ value: kind,
23
+ enumerable: false
24
+ }) as never
25
+
26
+ export type addArkKind<
27
+ kind extends ArkKind,
28
+ t extends Omit<ArkKinds[kind], arkKind>
29
+ > = t & { [arkKind]: kind }
30
+
31
+ export type arkKind = typeof arkKind
32
+
33
+ export const hasArkKind = <kind extends ArkKind>(
34
+ value: unknown,
35
+ kind: kind
36
+ ): value is ArkKinds[kind] => (value as any)?.[arkKind] === kind