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
package/type.ts CHANGED
@@ -1,75 +1,72 @@
1
1
  import {
2
- inferred,
3
- keywords,
4
- type ArkResult,
2
+ ArkErrors,
5
3
  type BaseMeta,
6
- type KeyCheckKind,
4
+ type BaseRoot,
5
+ type Disjoint,
7
6
  type Morph,
7
+ type NodeDef,
8
8
  type Out,
9
9
  type Predicate,
10
- type TypeNode,
11
- type distill,
12
- type extractIn,
13
- type extractOut,
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,
14
19
  type includesMorphs,
20
+ type inferIntersection,
15
21
  type inferMorphOut,
16
- type inferNarrow
22
+ type inferNarrow,
23
+ type inferPipes,
24
+ type internalImplementationOf
17
25
  } from "@arktype/schema"
18
26
  import {
19
27
  Callable,
20
- CastableBase,
21
- map,
22
28
  type Constructor,
23
- type Json,
29
+ type array,
24
30
  type conform
25
31
  } from "@arktype/util"
32
+ import { Generic, type validateParameterString } from "./generic.js"
26
33
  import type {
27
34
  inferDefinition,
28
35
  validateDeclared,
29
36
  validateDefinition
30
37
  } 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"
38
+ import { parseGenericParams } from "./parser/generic.js"
36
39
  import type {
37
40
  IndexOneOperator,
38
41
  IndexZeroOperator,
39
42
  TupleInfixOperator
40
43
  } from "./parser/tuple.js"
41
- import type { Scope, bindThis } from "./scope.js"
42
- import { arkKind } from "./util.js"
44
+ import type { RawScope, Scope, bindThis } from "./scope.js"
45
+
46
+ export interface TypeParserAttachments {
47
+ errors: typeof ArkErrors
48
+ }
43
49
 
44
- export type TypeParser<$> = {
50
+ export interface TypeParser<$> extends TypeParserAttachments {
45
51
  // Parse and check the definition, returning either the original input for a
46
52
  // valid definition or a string representing an error message.
47
53
  <const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>
48
54
 
49
55
  // Spread version of a tuple expression
50
- <const zero, const one, const rest extends readonly unknown[]>(
56
+ <const zero, const one, const rest extends array>(
51
57
  _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
- : []
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
+ : []
73
70
  ): Type<inferTypeRoot<[zero, one, ...rest], $>, $>
74
71
 
75
72
  <params extends string, const def>(
@@ -84,264 +81,186 @@ export type TypeParser<$> = {
84
81
  ): Generic<parseGenericParams<params>, def, $>
85
82
  }
86
83
 
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
- }
84
+ const typeParserAttachments = Object.freeze({
85
+ errors: ArkErrors
86
+ } satisfies TypeParserAttachments)
93
87
 
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)
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
+ )
116
119
  }
117
- return parser as never
118
120
  }
119
121
 
120
- export type DefinitionParser<$> = <def>(
121
- def: validateDefinition<def, $, bindThis<def>>
122
- ) => def
123
-
124
- export type TypeConfig = {
125
- keys?: KeyCheckKind
126
- mustBe?: string
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
127
  }
128
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>>
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<$>;
135
137
 
136
- config: TypeConfig
137
- root: TypeNode<t>
138
- allows: this["root"]["allows"]
139
- expected: string
140
- json: Json
138
+ get in(): Type<this["tIn"], $>
139
+ get out(): Type<this["tOut"], $>
141
140
 
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
- }
141
+ intersect<def>(
142
+ def: validateTypeRoot<def, $>
143
+ ): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint
164
144
 
165
- // TODO: Morph intersections, ordering
166
145
  and<def>(
167
146
  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
- }
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]>, $>
206
210
 
207
211
  // TODO: based on below, should maybe narrow morph output if used after
208
- narrow<def extends Predicate<extractOut<t>>>(
212
+ narrow<def extends Predicate<distillConstrainableOut<t>>>(
209
213
  def: def
210
214
  ): Type<
211
- includesMorphs<t> extends true
212
- ? (In: this["in"]["infer"]) => Out<inferNarrow<this["infer"], def>>
213
- : inferNarrow<this["infer"], def>,
215
+ includesMorphs<t> extends true ?
216
+ (In: this["tIn"]) => Out<inferNarrow<this["infer"], def>>
217
+ : inferNarrow<this["infer"], def>,
214
218
  $
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
- }
219
+ >
231
220
 
232
221
  equals<def>(
233
- other: validateTypeRoot<def, $>
234
- ): this is Type<inferTypeRoot<def, $>, $> {
235
- return this.root.equals(parseTypeRoot(other, this.scope))
236
- }
222
+ def: validateTypeRoot<def, $>
223
+ ): this is Type<inferTypeRoot<def>, $>
237
224
 
225
+ // TODO: i/o
226
+ extract<def>(r: validateTypeRoot<def, $>): Type<t, $>
227
+ exclude<def>(r: validateTypeRoot<def, $>): Type<t, $>
238
228
  extends<def>(
239
229
  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
- }
230
+ ): this is Type<inferTypeRoot<def>, $>
249
231
 
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
- }
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>, $>
255
239
  }
256
240
 
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
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, $> {}
271
246
 
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
- }
247
+ export type TypeConstructor<t = unknown, $ = any> = new (
248
+ def: unknown,
249
+ $: Scope<$>
250
+ ) => Type<t, $>
286
251
 
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
- }
252
+ export const Type: TypeConstructor = RawSchema as never
310
253
 
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
- }
254
+ export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def
323
255
 
324
- export type BoundArgs = Record<string, TypeNode>
256
+ export type validateTypeRoot<def, $ = {}> = validateDefinition<
257
+ def,
258
+ $ & ambient,
259
+ bindThis<def>
260
+ >
325
261
 
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
- }
262
+ export type inferTypeRoot<def, $ = {}> = inferDefinition<
263
+ def,
264
+ $ & ambient,
265
+ bindThis<def>
266
+ >
@@ -1,17 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import type { astToString } from "../parser/semantic/utils.js"
3
-
4
- describe("astToString", () => {
5
- it("no parentheses if nested ast is an array", () => {
6
- const t = {} as unknown as astToString<[["number", "[]"], "|", "number"]>
7
- attest<"'number[]|number'">(t)
8
- })
9
- it("parentheses if nested ast is an infix expression", () => {
10
- const t = {} as unknown as astToString<[["0", "|", "1"], "|", "string"]>
11
- attest<"'(0|1)|string'">(t)
12
- })
13
- it("defaults to '...' if input is bad", () => {
14
- const t = {} as unknown as astToString<["0", "///", "1"]>
15
- attest<"'...'">(t)
16
- })
17
- })