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/out/module.d.ts CHANGED
@@ -1,10 +1,22 @@
1
- import type { PreparsedNodeResolution, arkKind } from "@arktype/schema";
2
- import { DynamicBase, type isAnyOrNever } from "@arktype/util";
3
- import type { Type } from "./type.js";
4
- type exportScope<$> = {
5
- [k in keyof $]: $[k] extends PreparsedNodeResolution ? isAnyOrNever<$[k]> extends true ? Type<$[k], $> : $[k] : Type<$[k], $>;
6
- };
7
- export declare class Module<$ = any> extends DynamicBase<exportScope<$>> {
8
- readonly [arkKind]: "module";
1
+ import { RootModule, type GenericAst, type PreparsedNodeResolution } from "@ark/schema";
2
+ import type { anyOrNever, inferred } from "@ark/util";
3
+ import type { Generic } from "./generic.ts";
4
+ import type { Type } from "./type.ts";
5
+ export declare const Module: new <$ extends {}>(exports: exportScope<$>) => Module<$>;
6
+ export interface Module<$ extends {} = {}> extends RootModule<exportScope<$>> {
9
7
  }
10
- export {};
8
+ export type exportScope<$> = bindExportsToScope<$, $>;
9
+ export declare const BoundModule: new <exports extends {}, $ extends {}>(exports: bindExportsToScope<exports, $>, $: $) => BoundModule<exports, $>;
10
+ export interface BoundModule<exports extends {}, $> extends RootModule<bindExportsToScope<exports, $>> {
11
+ }
12
+ export type bindExportsToScope<exports, $> = {
13
+ [k in keyof exports]: instantiateExport<exports[k], $>;
14
+ } & unknown;
15
+ export type Submodule<exports extends {}> = RootModule<exports & ("$root" extends keyof exports ? {
16
+ [inferred]: exports["$root"];
17
+ } : {})>;
18
+ export type instantiateExport<t, $> = [
19
+ t
20
+ ] extends [PreparsedNodeResolution] ? [
21
+ t
22
+ ] extends [anyOrNever] ? Type<t, $> : t extends GenericAst<infer params, infer body, infer body$> ? Generic<params, body, body$, $> : t extends Submodule<infer exports> ? BoundModule<exports, $> : never : Type<t, $>;
package/out/module.js CHANGED
@@ -1,3 +1,3 @@
1
- import { DynamicBase } from "@arktype/util";
2
- export class Module extends DynamicBase {
3
- }
1
+ import { RootModule } from "@ark/schema";
2
+ export const Module = RootModule;
3
+ export const BoundModule = RootModule;
@@ -1,28 +1,33 @@
1
- import { type RawSchema, type string } from "@arktype/schema";
2
- import { type ErrorMessage, type Primitive, type array, type defined, type equals, type isAny, type isUnknown, type objectKindOrDomainOf, type optionalKeyOf, type requiredKeyOf, type show } from "@arktype/util";
3
- import type { type } from "../ark.js";
4
- import type { ParseContext } from "../scope.js";
5
- import { type inferObjectLiteral, type validateObjectLiteral } from "./objectLiteral.js";
6
- import type { validateString } from "./semantic/validate.js";
7
- import type { BaseCompletions, inferString } from "./string/string.js";
8
- import { type TupleExpression, type inferTuple, type validateTuple } from "./tuple.js";
9
- export declare const parseObject: (def: object, ctx: ParseContext) => RawSchema;
10
- export type inferDefinition<def, $, args> = isAny<def> extends true ? never : def extends type.cast<infer t> | ThunkCast<infer t> ? t : def extends string ? inferString<def, $, args> : def extends array ? inferTuple<def, $, args> : def extends RegExp ? string.matching<string> : def extends object ? inferObjectLiteral<def, $, args> : never;
11
- export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [Terminal] ? def : def extends string ? validateString<def, $, args> : def extends array ? validateTuple<def, $, args> : def extends BadDefinitionType ? writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>> : isUnknown<def> extends true ? BaseCompletions<$, args> | {} : validateObjectLiteral<def, $, args>;
1
+ import { type BaseRoot } from "@ark/schema";
2
+ import { type ErrorMessage, type Fn, type Primitive, type anyOrNever, type array, type defined, type equals, type objectKindOrDomainOf, type optionalKeyOf, type requiredKeyOf, type show } from "@ark/util";
3
+ import type { type } from "../keywords/ark.ts";
4
+ import type { string } from "../keywords/string/string.ts";
5
+ import type { ParseContext } from "../scope.ts";
6
+ import { type inferObjectLiteral, type validateObjectLiteral } from "./objectLiteral.ts";
7
+ import type { validateString } from "./semantic/validate.ts";
8
+ import type { BaseCompletions, inferString } from "./string/string.ts";
9
+ import { type TupleExpression, type inferTuple, type validateTuple } from "./tuple.ts";
10
+ export declare const parseObject: (def: object, ctx: ParseContext) => BaseRoot;
11
+ export type inferDefinition<def, $, args> = [
12
+ def
13
+ ] extends [anyOrNever] ? def : def extends type.cast<infer t> ? {} extends t ? [
14
+ t
15
+ ] extends [anyOrNever] ? t : object : t : def extends ThunkCast<infer t> ? t : def extends string ? inferString<def, $, args> : def extends array ? inferTuple<def, $, args> : def extends RegExp ? string.matching<string> : def extends object ? inferObjectLiteral<def, $, args> : never;
16
+ export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [Terminal] ? def : def extends string ? validateString<def, $, args> : def extends array ? validateTuple<def, $, args> : def extends BadDefinitionType ? ErrorMessage<writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>> : unknown extends def ? BaseCompletions<$, args> | {} : validateObjectLiteral<def, $, args>;
12
17
  export type validateDeclared<declared, def, $, args> = def extends validateDefinition<def, $, args> ? validateInference<def, declared, $, args> : validateDefinition<def, $, args>;
13
18
  type validateInference<def, declared, $, args> = def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ? validateShallowInference<def, declared, $, args> : def extends array ? declared extends array ? {
14
- [i in keyof declared]: i extends keyof def ? validateInference<def[i], declared[i], $, args> : unknown;
19
+ [i in keyof declared]: i extends keyof def ? validateInference<def[i], declared[i], $, args> : declared[i];
15
20
  } : show<declarationMismatch<def, declared, $, args>> : def extends object ? show<{
16
- [k in requiredKeyOf<declared>]: k extends keyof def ? validateInference<def[k], declared[k], $, args> : unknown;
21
+ [k in requiredKeyOf<declared>]: k extends keyof def ? validateInference<def[k], declared[k], $, args> : declared[k];
17
22
  } & {
18
- [k in optionalKeyOf<declared> & string as `${k}?`]: `${k}?` extends (keyof def) ? validateInference<def[`${k}?`], defined<declared[k]>, $, args> : unknown;
23
+ [k in optionalKeyOf<declared> & string as `${k}?`]: `${k}?` extends (keyof def) ? validateInference<def[`${k}?`], defined<declared[k]>, $, args> : declared[k];
19
24
  }> : validateShallowInference<def, declared, $, args>;
20
25
  type validateShallowInference<def, declared, $, args> = equals<inferDefinition<def, $, args>, declared> extends true ? def : show<declarationMismatch<def, declared, $, args>>;
21
26
  type declarationMismatch<def, declared, $, args> = {
22
27
  declared: declared;
23
28
  inferred: inferDefinition<def, $, args>;
24
29
  };
25
- type Terminal = RegExp | type.cast<unknown> | ((...args: never[]) => unknown);
30
+ type Terminal = RegExp | type.cast<unknown> | Fn;
26
31
  export type ThunkCast<t = unknown> = () => type.cast<t>;
27
32
  type BadDefinitionType = Exclude<Primitive, string>;
28
33
  export declare const writeBadDefinitionTypeMessage: <actual extends string>(actual: actual) => writeBadDefinitionTypeMessage<actual>;
@@ -1,12 +1,12 @@
1
- import { hasArkKind } from "@arktype/schema";
2
- import { isThunk, objectKindOf, printable, throwParseError } from "@arktype/util";
1
+ import { hasArkKind } from "@ark/schema";
2
+ import { isThunk, objectKindOf, printable, throwParseError } from "@ark/util";
3
3
  import { parseObjectLiteral } from "./objectLiteral.js";
4
4
  import { parseTuple } from "./tuple.js";
5
5
  export const parseObject = (def, ctx) => {
6
6
  const objectKind = objectKindOf(def);
7
7
  switch (objectKind) {
8
8
  case undefined:
9
- if (hasArkKind(def, "schema"))
9
+ if (hasArkKind(def, "root"))
10
10
  return def;
11
11
  return parseObjectLiteral(def, ctx);
12
12
  case "Array":
@@ -14,11 +14,11 @@ export const parseObject = (def, ctx) => {
14
14
  case "RegExp":
15
15
  return ctx.$.node("intersection", {
16
16
  domain: "string",
17
- regex: def
17
+ pattern: def
18
18
  }, { prereduced: true });
19
19
  case "Function": {
20
20
  const resolvedDef = isThunk(def) ? def() : def;
21
- if (hasArkKind(resolvedDef, "schema"))
21
+ if (hasArkKind(resolvedDef, "root"))
22
22
  return resolvedDef;
23
23
  return throwParseError(writeBadDefinitionTypeMessage("Function"));
24
24
  }
@@ -1,64 +1,82 @@
1
- import { type RawSchema, type writeInvalidPropertyKeyMessage } from "@arktype/schema";
2
- import { type Dict, type ErrorMessage, type Key, type merge, type show } from "@arktype/util";
3
- import type { ParseContext } from "../scope.js";
4
- import type { inferDefinition, validateDefinition } from "./definition.js";
5
- import type { astToString } from "./semantic/utils.js";
6
- import type { validateString } from "./semantic/validate.js";
7
- import { Scanner } from "./string/shift/scanner.js";
8
- export declare const parseObjectLiteral: (def: Dict, ctx: ParseContext) => RawSchema;
1
+ import { type BaseRoot, type Index, type Optional, type Required, type UndeclaredKeyBehavior, type writeInvalidPropertyKeyMessage } from "@ark/schema";
2
+ import { type anyOrNever, type conform, type Dict, type ErrorMessage, type ErrorType, type EscapeToken, type Key, type listable, type merge, type show } from "@ark/util";
3
+ import type { constrain, Default } from "../keywords/ast.ts";
4
+ import type { ParseContext } from "../scope.ts";
5
+ import type { inferDefinition, validateDefinition } from "./definition.ts";
6
+ import type { astToString } from "./semantic/utils.ts";
7
+ import type { validateString } from "./semantic/validate.ts";
8
+ export declare const parseObjectLiteral: (def: Dict, ctx: ParseContext) => BaseRoot;
9
+ export declare const writeInvalidUndeclaredBehaviorMessage: (actual: unknown) => string;
9
10
  export declare const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
11
+ export type inferObjectLiteral<def extends object, $, args> = show<"..." extends keyof def ? merge<inferDefinition<def["..."], $, args>, _inferObjectLiteral<def, $, args>> : _inferObjectLiteral<def, $, args>>;
10
12
  /**
11
13
  * Infers the contents of an object literal, ignoring a spread definition
12
- * You probably want to use {@link inferObjectLiteral} instead.
13
14
  */
14
- type inferObjectLiteralInner<def extends object, $, args> = {
15
- -readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: inferDefinition<def[k], $, args>;
15
+ type _inferObjectLiteral<def extends object, $, args> = {
16
+ -readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: def[k] extends (DefaultValueTuple<infer baseDef, infer defaultValue>) ? [
17
+ def[k]
18
+ ] extends [anyOrNever] ? def[k] : (In?: inferDefinition<baseDef, $, args>) => Default<defaultValue> : inferDefinition<def[k], $, args>;
16
19
  } & {
17
20
  -readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<def[k], $, args>;
18
21
  };
19
- export type inferObjectLiteral<def extends object, $, args> = show<"..." extends keyof def ? merge<inferDefinition<def["..."], $, args>, inferObjectLiteralInner<def, $, args>> : inferObjectLiteralInner<def, $, args>>;
20
22
  export type validateObjectLiteral<def, $, args> = {
21
- [k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? indexParseError<message> : inferDefinition<indexDef, $, args> extends PropertyKey ? validateDefinition<def[k], $, args> : indexParseError<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : validateDefinition<def[k], $, args>;
23
+ [k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? ErrorType<message> : inferDefinition<indexDef, $, args> extends Key | constrain<Key, {}> ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : k extends "+" ? UndeclaredKeyBehavior : validateDefaultableValue<def, k, $, args>;
22
24
  };
23
- type nonOptionalKeyFrom<k, $, args> = parseKey<k> extends PreparsedKey<"required", infer inner> ? inner : parseKey<k> extends PreparsedKey<"index", infer inner> ? inferDefinition<inner, $, args> & Key : never;
25
+ type validateDefaultableValue<def, k extends keyof def, $, args> = def[k] extends DefaultValueTuple ? validateDefaultValueTuple<def[k], k, $, args> : validateDefinition<def[k], $, args>;
26
+ type DefaultValueTuple<baseDef = unknown, defaultValue = unknown> = readonly [
27
+ baseDef,
28
+ "=",
29
+ defaultValue
30
+ ];
31
+ type validateDefaultValueTuple<def extends DefaultValueTuple, k extends PropertyKey, $, args> = parseKey<k>["kind"] extends "required" ? conform<def, readonly [
32
+ validateDefinition<def[0], $, args>,
33
+ "=",
34
+ inferDefinition<def[0], $, args>
35
+ ]> : ErrorMessage<invalidDefaultKeyKindMessage>;
36
+ type nonOptionalKeyFrom<k, $, args> = parseKey<k> extends PreparsedKey<"required", infer inner> ? inner : parseKey<k> extends PreparsedKey<"index", infer inner> ? inferDefinition<inner, $, args> extends infer t extends Key ? t : never : never;
24
37
  type optionalKeyFrom<k> = parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never;
25
- type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, inner extends Key = Key> = {
38
+ type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, key extends Key = Key> = {
26
39
  kind: kind;
27
- inner: inner;
40
+ key: key;
28
41
  };
29
42
  declare namespace PreparsedKey {
30
43
  type from<t extends PreparsedKey> = t;
31
44
  }
32
- type ParsedKeyKind = "required" | "optional" | "index" | "spread";
45
+ type ParsedKeyKind = "required" | "optional" | "index" | MetaKey;
46
+ export type MetaKey = "..." | "+";
33
47
  export type IndexKey<def extends string = string> = `[${def}]`;
34
- type PreparsedEntry = PreparsedKey & {
35
- value: unknown;
48
+ export type ParsedEntry = ParsedUndeclaredEntry | ParsedSpreadEntry | Required.Node | Optional.Node | Index.Node;
49
+ export type ParsedUndeclaredEntry = {
50
+ kind: "undeclared";
51
+ behavior: UndeclaredKeyBehavior;
36
52
  };
37
- export declare const parseEntry: (entry: readonly [Key, unknown]) => PreparsedEntry;
38
- declare const parseKey: (key: Key) => PreparsedKey<ParsedKeyKind, Key>;
39
- type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${Scanner.EscapeToken}` ? PreparsedKey.from<{
53
+ export type ParsedSpreadEntry = {
54
+ kind: "spread";
55
+ node: BaseRoot;
56
+ };
57
+ export declare const parseEntry: (key: Key, value: unknown, ctx: ParseContext) => listable<ParsedEntry>;
58
+ export declare const invalidDefaultKeyKindMessage = "Only required keys may specify default values, e.g. { value: 'number = 0' }";
59
+ export type invalidDefaultKeyKindMessage = typeof invalidDefaultKeyKindMessage;
60
+ declare const parseKey: (key: Key) => PreparsedKey;
61
+ type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${EscapeToken}` ? PreparsedKey.from<{
40
62
  kind: "required";
41
- inner: `${baseName}?`;
63
+ key: `${baseName}?`;
42
64
  }> : PreparsedKey.from<{
43
65
  kind: "optional";
44
- inner: inner;
45
- }> : k extends "..." ? PreparsedKey.from<{
46
- kind: "spread";
47
- inner: "...";
48
- }> : k extends `${Scanner.EscapeToken}...` ? PreparsedKey.from<{
66
+ key: inner;
67
+ }> : k extends MetaKey ? PreparsedKey.from<{
68
+ kind: k;
69
+ key: k;
70
+ }> : k extends `${EscapeToken}${infer escapedMeta extends MetaKey}` ? PreparsedKey.from<{
49
71
  kind: "required";
50
- inner: "...";
72
+ key: escapedMeta;
51
73
  }> : k extends IndexKey<infer def> ? PreparsedKey.from<{
52
74
  kind: "index";
53
- inner: def;
75
+ key: def;
54
76
  }> : PreparsedKey.from<{
55
77
  kind: "required";
56
- inner: k extends (`${Scanner.EscapeToken}${infer escapedIndexKey extends IndexKey}`) ? escapedIndexKey : k extends Key ? k : `${k & number}`;
78
+ key: k extends `${EscapeToken}${infer escapedIndexKey extends IndexKey}` ? escapedIndexKey : k extends Key ? k : `${k & number}`;
57
79
  }>;
58
- declare const indexParseSymbol: unique symbol;
59
- export type indexParseError<message extends string = string> = {
60
- [indexParseSymbol]: message;
61
- };
62
80
  export declare const writeInvalidSpreadTypeMessage: <def extends string>(def: def) => writeInvalidSpreadTypeMessage<def>;
63
81
  type writeInvalidSpreadTypeMessage<def extends string> = `Spread operand must resolve to an object literal type (was ${def})`;
64
82
  export {};
@@ -1,75 +1,100 @@
1
- import { tsKeywords } from "@arktype/schema";
2
- import { printable, spliterate, stringAndSymbolicEntriesOf, throwParseError } from "@arktype/util";
3
- import { Scanner } from "./string/shift/scanner.js";
1
+ import { ArkErrors, normalizeIndex } from "@ark/schema";
2
+ import { append, escapeToken, isArray, printable, stringAndSymbolicEntriesOf, throwParseError } from "@ark/util";
3
+ import { writeUnassignableDefaultValueMessage } from "./semantic/default.js";
4
4
  export const parseObjectLiteral = (def, ctx) => {
5
- const propNodes = [];
6
- const indexNodes = [];
5
+ let spread;
6
+ const structure = {};
7
7
  // We only allow a spread operator to be used as the first key in an object
8
8
  // because to match JS behavior any keys before the spread are overwritten
9
9
  // by the values in the target object, so there'd be no useful purpose in having it
10
10
  // anywhere except for the beginning.
11
- const parsedEntries = stringAndSymbolicEntriesOf(def).map(parseEntry);
11
+ const parsedEntries = stringAndSymbolicEntriesOf(def).flatMap(entry => parseEntry(entry[0], entry[1], ctx));
12
12
  if (parsedEntries[0]?.kind === "spread") {
13
13
  // remove the spread entry so we can iterate over the remaining entries
14
14
  // expecting non-spread entries
15
15
  const spreadEntry = parsedEntries.shift();
16
- const spreadNode = ctx.$.parse(spreadEntry.value, ctx);
17
- if (!spreadNode.hasKind("intersection") ||
18
- !spreadNode.extends(tsKeywords.object)) {
19
- return throwParseError(writeInvalidSpreadTypeMessage(printable(spreadEntry.value)));
16
+ if (!spreadEntry.node.hasKind("intersection") ||
17
+ !spreadEntry.node.structure) {
18
+ return throwParseError(writeInvalidSpreadTypeMessage(typeof spreadEntry.node.expression));
20
19
  }
21
- // TODO: move to props group merge in schema
22
- // For each key on spreadNode, add it to our object.
23
- // We filter out keys from the spreadNode that will be defined later on this same object
24
- // because the currently parsed definition will overwrite them.
25
- spreadNode.prop?.forEach(spreadRequired => !parsedEntries.some(({ inner: innerKey }) => innerKey === spreadRequired.key) && propNodes.push(spreadRequired));
20
+ spread = spreadEntry.node.structure;
26
21
  }
27
22
  for (const entry of parsedEntries) {
28
23
  if (entry.kind === "spread")
29
24
  return throwParseError(nonLeadingSpreadError);
30
- if (entry.kind === "index") {
31
- // handle key parsing first to match type behavior
32
- const key = ctx.$.parse(entry.inner, ctx);
33
- const value = ctx.$.parse(entry.value, ctx);
34
- // extract enumerable named props from the index signature
35
- const [enumerable, nonEnumerable] = spliterate(key.branches, k => k.hasKind("unit"));
36
- if (enumerable.length) {
37
- propNodes.push(...enumerable.map(k => ctx.$.node("prop", { key: k.unit, value })));
38
- if (nonEnumerable.length)
39
- indexNodes.push(ctx.$.node("index", { key: nonEnumerable, value }));
40
- }
41
- else
42
- indexNodes.push(ctx.$.node("index", { key, value }));
43
- }
44
- else {
45
- const value = ctx.$.parse(entry.value, ctx);
46
- propNodes.push({
47
- key: entry.inner,
48
- value,
49
- optional: entry.kind === "optional"
50
- });
25
+ if (entry.kind === "undeclared") {
26
+ structure.undeclared = entry.behavior;
27
+ continue;
51
28
  }
29
+ structure[entry.kind] = append(structure[entry.kind], entry);
52
30
  }
53
- return ctx.$.schema({
31
+ const structureNode = ctx.$.node("structure", structure);
32
+ return ctx.$.rootNode({
54
33
  domain: "object",
55
- prop: propNodes,
56
- index: indexNodes
34
+ structure: spread?.merge(structureNode) ?? structureNode
57
35
  });
58
36
  };
37
+ export const writeInvalidUndeclaredBehaviorMessage = (actual) => `Value of '+' key must be 'reject', 'delete', or 'ignore' (was ${printable(actual)})`;
59
38
  export const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
60
- export const parseEntry = (entry) => Object.assign(parseKey(entry[0]), { value: entry[1] });
61
- const parseKey = (key) => typeof key === "symbol" ? { inner: key, kind: "required" }
39
+ export const parseEntry = (key, value, ctx) => {
40
+ const parsedKey = parseKey(key);
41
+ if (parsedKey.kind === "+") {
42
+ if (value !== "reject" && value !== "delete" && value !== "ignore")
43
+ throwParseError(writeInvalidUndeclaredBehaviorMessage(value));
44
+ return { kind: "undeclared", behavior: value };
45
+ }
46
+ if (parsedKey.kind === "...")
47
+ return { kind: "spread", node: ctx.$.parse(value, ctx) };
48
+ const parsedValue = isArray(value) && value[1] === "=" ?
49
+ [ctx.$.parse(value[0], ctx), "=", value[2]]
50
+ : ctx.$.parse(value, ctx, true);
51
+ if (isArray(parsedValue)) {
52
+ if (parsedKey.kind !== "required")
53
+ throwParseError(invalidDefaultKeyKindMessage);
54
+ const out = parsedValue[0].traverse(parsedValue[2]);
55
+ if (out instanceof ArkErrors) {
56
+ throwParseError(writeUnassignableDefaultValueMessage(printable(parsedKey.key), out.message));
57
+ }
58
+ return ctx.$.node("optional", {
59
+ key: parsedKey.key,
60
+ value: parsedValue[0],
61
+ default: parsedValue[2]
62
+ });
63
+ }
64
+ if (parsedKey.kind === "index") {
65
+ const signature = ctx.$.parse(parsedKey.key, ctx);
66
+ const normalized = normalizeIndex(signature, parsedValue, ctx.$);
67
+ return (normalized.index ?
68
+ normalized.required ?
69
+ [normalized.index, ...normalized.required]
70
+ : normalized.index
71
+ : (normalized.required ?? []));
72
+ }
73
+ return ctx.$.node(parsedKey.kind, {
74
+ key: parsedKey.key,
75
+ value: parsedValue
76
+ });
77
+ };
78
+ // single quote use here is better for TypeScript's inlined error to avoid escapes
79
+ export const invalidDefaultKeyKindMessage = `Only required keys may specify default values, e.g. { value: 'number = 0' }`;
80
+ const parseKey = (key) => typeof key === "symbol" ? { kind: "required", key }
62
81
  : key.at(-1) === "?" ?
63
- key.at(-2) === Scanner.escapeToken ?
64
- { inner: `${key.slice(0, -2)}?`, kind: "required" }
82
+ key.at(-2) === escapeToken ?
83
+ { kind: "required", key: `${key.slice(0, -2)}?` }
65
84
  : {
66
- inner: key.slice(0, -1),
67
- kind: "optional"
85
+ kind: "optional",
86
+ key: key.slice(0, -1)
68
87
  }
69
88
  : key[0] === "[" && key.at(-1) === "]" ?
70
- { inner: key.slice(1, -1), kind: "index" }
71
- : key[0] === Scanner.escapeToken && key[1] === "[" && key.at(-1) === "]" ?
72
- { inner: key.slice(1), kind: "required" }
73
- : key === "..." ? { inner: "...", kind: "spread" }
74
- : { inner: key === "\\..." ? "..." : key, kind: "required" };
89
+ { kind: "index", key: key.slice(1, -1) }
90
+ : key[0] === escapeToken && key[1] === "[" && key.at(-1) === "]" ?
91
+ { kind: "required", key: key.slice(1) }
92
+ : key === "..." ? { kind: key, key }
93
+ : key === "+" ? { kind: key, key }
94
+ : {
95
+ kind: "required",
96
+ key: key === "\\..." ? "..."
97
+ : key === "\\+" ? "+"
98
+ : key
99
+ };
75
100
  export const writeInvalidSpreadTypeMessage = (def) => `Spread operand must resolve to an object literal type (was ${def})`;
@@ -1,12 +1,13 @@
1
- import type { LimitLiteral, writeUnboundableMessage } from "@arktype/schema";
2
- import type { ErrorMessage, array } from "@arktype/util";
3
- import type { Comparator, InvertedComparators } from "../string/reduce/shared.js";
4
- import type { BoundExpressionKind, writeInvalidLimitMessage } from "../string/shift/operator/bounds.js";
5
- import type { inferAstIn } from "./infer.js";
6
- import type { astToString } from "./utils.js";
7
- import type { validateAst } from "./validate.js";
8
- export type validateRange<l, comparator extends Comparator, r, $, args> = l extends LimitLiteral ? validateBound<r, InvertedComparators[comparator], l, "left", $, args> : l extends [infer leftAst, Comparator, unknown] ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>> : validateBound<l, comparator, r & LimitLiteral, "right", $, args>;
9
- export type validateBound<boundedAst, comparator extends Comparator, limit extends LimitLiteral, boundKind extends BoundExpressionKind, $, args> = inferAstIn<boundedAst, $, args> extends infer bounded ? isNumericallyBoundable<bounded> extends true ? limit extends number ? validateAst<boundedAst, $, args> : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>> : bounded extends Date ? validateAst<boundedAst, $, args> : ErrorMessage<writeUnboundableMessage<astToString<boundKind extends "left" ? boundedAst[0 & keyof boundedAst] : boundedAst>>> : never;
1
+ import type { writeUnboundableMessage } from "@ark/schema";
2
+ import type { ErrorMessage, array, typeToString } from "@ark/util";
3
+ import type { LimitLiteral } from "../../keywords/ast.ts";
4
+ import type { Comparator } from "../string/reduce/shared.ts";
5
+ import type { BoundExpressionKind, writeInvalidLimitMessage } from "../string/shift/operator/bounds.ts";
6
+ import type { inferAstIn } from "./infer.ts";
7
+ import type { astToString } from "./utils.ts";
8
+ import type { validateAst } from "./validate.ts";
9
+ export type validateRange<l, comparator extends Comparator, r, $, args> = l extends LimitLiteral ? validateBound<r, comparator, l, "left", $, args> : l extends [infer leftAst, Comparator, unknown] ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>> : validateBound<l, comparator, r & LimitLiteral, "right", $, args>;
10
+ export type validateBound<boundedAst, comparator extends Comparator, limit extends LimitLiteral, boundKind extends BoundExpressionKind, $, args> = inferAstIn<boundedAst, $, args> extends infer bounded ? isNumericallyBoundable<bounded> extends true ? limit extends number ? validateAst<boundedAst, $, args> : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>> : [bounded] extends [Date] ? validateAst<boundedAst, $, args> : ErrorMessage<writeUnboundableMessage<typeToString<bounded>>> : never;
10
11
  type isNumericallyBoundable<bounded> = [
11
12
  bounded
12
13
  ] extends [number] ? true : [bounded] extends [string] ? true : [bounded] extends [array] ? true : false;
@@ -0,0 +1,8 @@
1
+ import type { ErrorMessage } from "@ark/util";
2
+ import type { UnitLiteral } from "../string/shift/operator/default.ts";
3
+ import type { inferAstOut, inferTerminal } from "./infer.ts";
4
+ import type { astToString } from "./utils.ts";
5
+ import type { validateAst } from "./validate.ts";
6
+ export type validateDefault<baseAst, unitLiteral extends UnitLiteral, $, args> = validateAst<baseAst, $, args> extends infer e extends ErrorMessage ? e : inferTerminal<unitLiteral, $, args> extends inferAstOut<baseAst, $, args> ? undefined : ErrorMessage<writeUnassignableDefaultValueMessage<astToString<baseAst>, unitLiteral>>;
7
+ export declare const writeUnassignableDefaultValueMessage: <key extends string, message extends string>(key: key, message: message) => string;
8
+ export type writeUnassignableDefaultValueMessage<baseDef extends string, defaultValue extends string> = `Default value ${defaultValue} is not assignable to ${baseDef}`;
@@ -0,0 +1 @@
1
+ export const writeUnassignableDefaultValueMessage = (key, message) => `Default value at ${key} ${message}`;
@@ -1,7 +1,7 @@
1
- import type { Schema, writeIndivisibleMessage } from "@arktype/schema";
2
- import type { ErrorMessage } from "@arktype/util";
3
- import type { inferAstIn } from "./infer.js";
4
- import type { validateAst } from "./validate.js";
1
+ import type { writeIndivisibleMessage } from "@ark/schema";
2
+ import type { ErrorMessage } from "@ark/util";
3
+ import type { inferAstIn } from "./infer.ts";
4
+ import type { validateAst } from "./validate.ts";
5
5
  export type validateDivisor<l, $, args> = inferAstIn<l, $, args> extends infer data ? [
6
6
  data
7
- ] extends [number] ? validateAst<l, $, args> : ErrorMessage<writeIndivisibleMessage<Schema<data>>> : never;
7
+ ] extends [number] ? validateAst<l, $, args> : ErrorMessage<writeIndivisibleMessage<data>> : never;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,17 +1,25 @@
1
- import type { Date, DateLiteral, GenericProps, LimitLiteral, RegexLiteral, constrain, distillIn, inferIntersection, normalizeLimit, string } from "@arktype/schema";
2
- import type { BigintLiteral, array } from "@arktype/util";
3
- import type { UnparsedScope, resolve, tryInferSubmoduleReference } from "../../scope.js";
4
- import type { inferDefinition } from "../definition.js";
5
- import type { Comparator, MinComparator } from "../string/reduce/shared.js";
6
- import type { StringLiteral } from "../string/shift/operand/enclosed.js";
1
+ import type { GenericAst } from "@ark/schema";
2
+ import type { BigintLiteral, Hkt, array } from "@ark/util";
3
+ import type { ArkAmbient } from "../../config.ts";
4
+ import type { inferIntersection } from "../../intersect.ts";
5
+ import type { Date, DateLiteral, Default, LimitLiteral, RegexLiteral, applyConstraint, distillIn, distillOut, normalizeLimit } from "../../keywords/ast.ts";
6
+ import type { string } from "../../keywords/string/string.ts";
7
+ import type { UnparsedScope, resolve, tryInferSubmoduleReference } from "../../scope.ts";
8
+ import type { inferDefinition } from "../definition.ts";
9
+ import type { Comparator, MinComparator } from "../string/reduce/shared.ts";
10
+ import type { StringLiteral } from "../string/shift/operand/enclosed.ts";
7
11
  export type inferAstRoot<ast, $, args> = inferConstrainableAst<ast, $, args>;
8
12
  export type inferAstIn<ast, $, args> = distillIn<inferAstRoot<ast, $, args>>;
13
+ export type inferAstOut<ast, $, args> = distillOut<inferAstRoot<ast, $, args>>;
9
14
  export type inferConstrainableAst<ast, $, args> = ast extends array ? inferExpression<ast, $, args> : ast extends string ? inferTerminal<ast, $, args> : never;
10
- export type GenericInstantiationAst<generic extends GenericProps = GenericProps, argAsts extends unknown[] = unknown[]> = [generic, "<>", argAsts];
11
- export type inferExpression<ast extends array, $, args> = ast extends GenericInstantiationAst<infer generic, infer argAsts> ? inferDefinition<generic["def"], generic["$"]["$"] extends UnparsedScope ? $ : generic["$"]["$"], {
12
- [i in keyof generic["params"] & `${number}` as generic["params"][i]]: inferConstrainableAst<argAsts[i & keyof argAsts], $, args>;
13
- }> : ast[1] extends "[]" ? inferConstrainableAst<ast[0], $, args>[] : ast[1] extends "|" ? inferConstrainableAst<ast[0], $, args> | inferConstrainableAst<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferConstrainableAst<ast[0], $, args>, inferConstrainableAst<ast[2], $, args>> : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? constrainBound<inferConstrainableAst<ast[2], $, args>, ast[1], ast[0]> : constrainBound<inferConstrainableAst<ast[0], $, args>, ast[1], ast[2]> : ast[1] extends "%" ? constrain<inferConstrainableAst<ast[0], $, args>, "divisor", ast[2] & number> : ast[0] extends "keyof" ? keyof inferConstrainableAst<ast[1], $, args> : never;
14
- export type constrainBound<constrainableIn, comparator extends Comparator, limit> = distillIn<constrainableIn> extends infer In ? comparator extends "==" ? In extends number ? limit : In extends Date ? Date.literal<normalizeLimit<limit>> : constrain<constrainableIn, "exactLength", limit & number> : constrain<constrainableIn, In extends number ? comparator extends MinComparator ? "min" : "max" : In extends string | array ? comparator extends MinComparator ? "minLength" : "maxLength" : comparator extends MinComparator ? "after" : "before", {
15
+ export type GenericInstantiationAst<generic extends GenericAst = GenericAst, argAsts extends unknown[] = unknown[]> = [generic, "<>", argAsts];
16
+ type resolveScope<g$, $> = g$ extends UnparsedScope ? $ : g$;
17
+ export type inferExpression<ast extends array, $, args> = ast extends GenericInstantiationAst<infer g, infer argAsts> ? g["bodyDef"] extends Hkt ? Hkt.apply<g["bodyDef"], {
18
+ [i in keyof argAsts]: inferConstrainableAst<argAsts[i], $, args>;
19
+ }> : inferDefinition<g["bodyDef"], resolveScope<g["$"], $>, {
20
+ [i in keyof g["names"] & `${number}` as g["names"][i]]: inferConstrainableAst<argAsts[i & keyof argAsts], resolveScope<g["arg$"], $>, args>;
21
+ }> : ast[1] extends "[]" ? inferConstrainableAst<ast[0], $, args>[] : ast[1] extends "|" ? inferConstrainableAst<ast[0], $, args> | inferConstrainableAst<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferConstrainableAst<ast[0], $, args>, inferConstrainableAst<ast[2], $, args>> : ast[1] extends "=" ? inferTerminal<ast[2] & string, $, args> extends infer defaultValue ? (In?: inferConstrainableAst<ast[0], $, args>) => Default<defaultValue> : never : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? constrainBound<inferConstrainableAst<ast[2], $, args>, ast[1], ast[0]> : constrainBound<inferConstrainableAst<ast[0], $, args>, ast[1], ast[2]> : ast[1] extends "%" ? applyConstraint<inferConstrainableAst<ast[0], $, args>, "divisor", ast[2] & number> : ast[0] extends "keyof" ? keyof inferConstrainableAst<ast[1], $, args> : never;
22
+ export type constrainBound<constrainableIn, comparator extends Comparator, limit> = distillIn<constrainableIn> extends infer In ? comparator extends "==" ? In extends number ? limit : In extends Date ? Date.literal<normalizeLimit<limit>> : applyConstraint<constrainableIn, "exactLength", limit & number> : applyConstraint<constrainableIn, In extends number ? comparator extends MinComparator ? "min" : "max" : In extends string | array ? comparator extends MinComparator ? "minLength" : "maxLength" : comparator extends MinComparator ? "after" : "before", {
15
23
  rule: normalizeLimit<limit>;
16
24
  exclusive: comparator extends ">" | "<" ? true : false;
17
25
  }> : never;
@@ -21,4 +29,5 @@ export type PostfixOperator = "[]";
21
29
  export type PostfixExpression<operator extends PostfixOperator = PostfixOperator, operand = unknown> = readonly [operand, operator];
22
30
  export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@";
23
31
  export type InfixExpression<operator extends InfixOperator = InfixOperator, l = unknown, r = unknown> = [l, operator, r];
24
- export type inferTerminal<token extends string, $, args> = token extends keyof args | keyof $ ? resolve<token, $, args> : `#${token}` extends keyof $ ? resolve<`#${token}`, $, args> : token extends StringLiteral<infer text> ? text : token extends `${infer n extends number}` ? n : token extends BigintLiteral<infer b> ? b : token extends RegexLiteral<infer source> ? string.matching<source> : token extends DateLiteral<infer source> ? Date.literal<source> : tryInferSubmoduleReference<$, token>;
32
+ export type inferTerminal<token extends string, $, args> = token extends keyof args | keyof $ ? resolve<token, $, args> : token extends keyof ArkAmbient.$ ? resolve<token, ArkAmbient.$, args> : `#${token}` extends keyof $ ? resolve<`#${token}`, $, args> : token extends StringLiteral<infer text> ? text : token extends `${infer n extends number}` ? n : token extends BigintLiteral<infer b> ? b : token extends RegexLiteral<infer source> ? string.matching<source> : token extends DateLiteral<infer source> ? Date.literal<source> : tryInferSubmoduleReference<$, token>;
33
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { writeNonStructuralOperandMessage } from "@ark/schema";
2
+ import type { ErrorMessage, typeToString } from "@ark/util";
3
+ import type { inferAstRoot } from "./infer.ts";
4
+ import type { validateAst } from "./validate.ts";
5
+ export type validateKeyof<operandAst, $, args> = inferAstRoot<operandAst, $, args> extends infer data ? [
6
+ data
7
+ ] extends [object] ? validateAst<operandAst, $, args> : ErrorMessage<writeNonStructuralOperandMessage<"keyof", typeToString<data>>> : never;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import type { Stringifiable } from "@arktype/util";
2
- import type { Comparator } from "../string/reduce/shared.js";
3
- import type { InfixExpression, PostfixExpression } from "./infer.js";
1
+ import type { Stringifiable } from "@ark/util";
2
+ import type { Comparator } from "../string/reduce/shared.ts";
3
+ import type { InfixExpression, PostfixExpression } from "./infer.ts";
4
4
  export type astToString<ast> = ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? `${astToString<operand>}[]` : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" | "%" | Comparator ? `${astToString<l>} ${operator} ${astToString<r>}` : never : ast extends Stringifiable ? `${ast extends bigint ? `${ast}n` : ast}` : "...";
@@ -0,0 +1 @@
1
+ export {};
@@ -1,25 +1,29 @@
1
- import type { arkKind, GenericProps, PrivateDeclaration, writeMissingSubmoduleAccessMessage } from "@arktype/schema";
2
- import type { BigintLiteral, charsAfterFirst, Completion, ErrorMessage, isAnyOrNever, writeMalformedNumericLiteralMessage } from "@arktype/util";
3
- import type { Comparator } from "../string/reduce/shared.js";
4
- import type { writeInvalidGenericArgsMessage } from "../string/shift/operand/genericArgs.js";
5
- import type { parseString } from "../string/string.js";
6
- import type { validateRange } from "./bounds.js";
7
- import type { validateDivisor } from "./divisor.js";
8
- import type { GenericInstantiationAst, InfixExpression, PostfixExpression } from "./infer.js";
9
- import type { astToString } from "./utils.js";
10
- export type validateAst<ast, $, args> = ast extends string ? validateStringAst<ast, $> : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : undefined : ast extends readonly ["keyof", infer operand] ? validateAst<operand, $, args> : ast extends GenericInstantiationAst ? validateGenericArgs<ast[2], $, args> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
1
+ import type { arkKind, GenericParamAst, PrivateDeclaration, writeMissingSubmoduleAccessMessage, writeUnsatisfiedParameterConstraintMessage } from "@ark/schema";
2
+ import type { anyOrNever, array, BigintLiteral, charsAfterFirst, Completion, ErrorMessage, typeToString, writeMalformedNumericLiteralMessage } from "@ark/util";
3
+ import type { Generic } from "../../generic.ts";
4
+ import type { Comparator } from "../string/reduce/shared.ts";
5
+ import type { writeInvalidGenericArgCountMessage } from "../string/shift/operand/genericArgs.ts";
6
+ import type { UnitLiteral } from "../string/shift/operator/default.ts";
7
+ import type { parseString } from "../string/string.ts";
8
+ import type { validateRange } from "./bounds.ts";
9
+ import type { validateDefault } from "./default.ts";
10
+ import type { validateDivisor } from "./divisor.ts";
11
+ import type { GenericInstantiationAst, inferAstRoot, InfixExpression, PostfixExpression } from "./infer.ts";
12
+ import type { validateKeyof } from "./keyof.ts";
13
+ import type { astToString } from "./utils.ts";
14
+ export type validateAst<ast, $, args> = ast extends string ? validateStringAst<ast, $> : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : undefined : ast extends [infer baseAst, "=", infer unitLiteral extends UnitLiteral] ? validateDefault<baseAst, unitLiteral, $, args> : ast extends ["keyof", infer operand] ? validateKeyof<operand, $, args> : ast extends GenericInstantiationAst<infer g, infer argAsts> ? validateGenericArgs<g["paramsAst"], argAsts, $, args, []> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
11
15
  ast: ast;
12
16
  };
13
17
  type writeUnexpectedExpressionMessage<expression extends string> = `Unexpectedly failed to parse the expression resulting from ${expression}`;
14
- type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [infer head, ...infer tail] ? validateAst<head, $, args> extends ErrorMessage<infer message> ? ErrorMessage<message> : validateGenericArgs<tail, $, args> : undefined;
15
- export declare const writeUnsatisfiableExpressionError: <expression extends string>(expression: expression) => writeUnsatisfiableExpressionError<expression>;
16
- export type writeUnsatisfiableExpressionError<expression extends string> = `${expression} results in an unsatisfiable type`;
17
- export declare const writePrefixedPrivateReferenceMessage: <def extends `#${string}`>(def: def) => writePrefixedPrivateReferenceMessage<def>;
18
+ type validateGenericArgs<params extends array<GenericParamAst>, argAsts extends array, $, args, indices extends 1[]> = argAsts extends readonly [infer arg, ...infer argsTail] ? validateAst<arg, $, args> extends infer e extends ErrorMessage ? e : inferAstRoot<arg, $, args> extends params[indices["length"]][1] ? validateGenericArgs<params, argsTail, $, args, [...indices, 1]> : ErrorMessage<writeUnsatisfiedParameterConstraintMessage<params[indices["length"]][0], typeToString<params[indices["length"]][1]>, astToString<arg>>> : undefined;
19
+ export declare const writePrefixedPrivateReferenceMessage: <def extends PrivateDeclaration>(def: def) => writePrefixedPrivateReferenceMessage<def>;
18
20
  export type writePrefixedPrivateReferenceMessage<def extends PrivateDeclaration> = `Private type references should not include '#'. Use '${charsAfterFirst<def>}' instead.`;
19
- type validateStringAst<def extends string, $> = def extends `${infer n extends number}` ? number extends n ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">> : undefined : def extends BigintLiteral<infer b> ? bigint extends b ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">> : undefined : maybeExtractAlias<def, $> extends infer alias extends keyof $ ? isAnyOrNever<$[alias]> extends true ? def : def extends PrivateDeclaration ? ErrorMessage<writePrefixedPrivateReferenceMessage<def>> : $[alias] extends GenericProps ? ErrorMessage<writeInvalidGenericArgsMessage<def, $[alias]["params"], []>> : $[alias] extends {
21
+ type validateStringAst<def extends string, $> = def extends `${infer n extends number}` ? number extends n ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">> : undefined : def extends BigintLiteral<infer b> ? bigint extends b ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">> : undefined : maybeExtractAlias<def, $> extends infer alias extends keyof $ ? [
22
+ $[alias]
23
+ ] extends [anyOrNever] ? def : def extends PrivateDeclaration ? ErrorMessage<writePrefixedPrivateReferenceMessage<def>> : $[alias] extends Generic ? ErrorMessage<writeInvalidGenericArgCountMessage<def, $[alias]["names"], []>> : $[alias] extends {
20
24
  [arkKind]: "module";
21
- } ? ErrorMessage<writeMissingSubmoduleAccessMessage<def>> : undefined : def extends ErrorMessage ? def : undefined;
25
+ } ? "$root" extends keyof $[alias] ? undefined : ErrorMessage<writeMissingSubmoduleAccessMessage<def>> : undefined : def extends ErrorMessage ? def : undefined;
22
26
  export type maybeExtractAlias<def extends string, $> = def extends keyof $ ? def : `#${def}` extends keyof $ ? `#${def}` : null;
23
27
  export type validateString<def extends string, $, args> = validateAst<parseString<def, $, args>, $, args> extends (infer result extends ErrorMessage) ? result extends Completion<infer text> ? text : result : def;
24
- type validateInfix<ast extends InfixExpression, $, args> = validateAst<ast[0], $, args> extends ErrorMessage<infer message> ? ErrorMessage<message> : validateAst<ast[2], $, args> extends ErrorMessage<infer message> ? ErrorMessage<message> : undefined;
28
+ type validateInfix<ast extends InfixExpression, $, args> = validateAst<ast[0], $, args> extends infer e extends ErrorMessage ? e : validateAst<ast[2], $, args> extends infer e extends ErrorMessage ? e : undefined;
25
29
  export {};