arktype 2.0.0-rc.2 → 2.0.0-rc.22

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 (155) hide show
  1. package/out/attributes.d.ts +274 -0
  2. package/out/attributes.js +2 -0
  3. package/out/config.d.ts +1 -1
  4. package/out/generic.d.ts +52 -55
  5. package/out/generic.js +6 -8
  6. package/out/index.d.ts +4 -4
  7. package/out/index.js +2 -2
  8. package/out/keywords/builtins.js +2 -2
  9. package/out/keywords/constructors/Array.d.ts +2 -2
  10. package/out/keywords/constructors/Array.js +7 -7
  11. package/out/keywords/constructors/Date.d.ts +37 -0
  12. package/out/keywords/constructors/FormData.d.ts +2 -2
  13. package/out/keywords/constructors/FormData.js +8 -8
  14. package/out/keywords/constructors/TypedArray.js +2 -2
  15. package/out/keywords/constructors/constructors.d.ts +0 -1
  16. package/out/keywords/constructors/constructors.js +2 -2
  17. package/out/keywords/keywords.d.ts +64 -0
  18. package/out/keywords/{ark.js → keywords.js} +14 -9
  19. package/out/keywords/number/epoch.d.ts +1 -1
  20. package/out/keywords/number/epoch.js +2 -2
  21. package/out/keywords/number/integer.js +2 -2
  22. package/out/keywords/number/number.d.ts +44 -14
  23. package/out/keywords/number/number.js +17 -5
  24. package/out/keywords/string/alpha.d.ts +2 -2
  25. package/out/keywords/string/alphanumeric.d.ts +2 -2
  26. package/out/keywords/string/base64.d.ts +21 -0
  27. package/out/keywords/string/base64.js +6 -0
  28. package/out/keywords/string/capitalize.d.ts +3 -3
  29. package/out/keywords/string/capitalize.js +4 -4
  30. package/out/keywords/string/creditCard.d.ts +2 -2
  31. package/out/keywords/string/creditCard.js +2 -2
  32. package/out/keywords/string/date.d.ts +7 -11
  33. package/out/keywords/string/date.js +16 -16
  34. package/out/keywords/string/digits.d.ts +2 -2
  35. package/out/keywords/string/email.d.ts +2 -2
  36. package/out/keywords/string/integer.d.ts +3 -3
  37. package/out/keywords/string/integer.js +7 -7
  38. package/out/keywords/string/ip.d.ts +5 -5
  39. package/out/keywords/string/ip.js +5 -5
  40. package/out/keywords/string/json.d.ts +4 -3
  41. package/out/keywords/string/json.js +39 -30
  42. package/out/keywords/string/lower.d.ts +3 -3
  43. package/out/keywords/string/lower.js +4 -4
  44. package/out/keywords/string/normalize.d.ts +27 -31
  45. package/out/keywords/string/normalize.js +14 -14
  46. package/out/keywords/string/numeric.d.ts +3 -3
  47. package/out/keywords/string/numeric.js +7 -7
  48. package/out/keywords/string/semver.d.ts +2 -2
  49. package/out/keywords/string/string.d.ts +74 -51
  50. package/out/keywords/string/string.js +5 -3
  51. package/out/keywords/string/trim.d.ts +3 -3
  52. package/out/keywords/string/trim.js +4 -4
  53. package/out/keywords/string/upper.d.ts +3 -3
  54. package/out/keywords/string/upper.js +4 -4
  55. package/out/keywords/string/url.d.ts +3 -3
  56. package/out/keywords/string/url.js +8 -8
  57. package/out/keywords/string/uuid.d.ts +20 -20
  58. package/out/keywords/string/uuid.js +3 -3
  59. package/out/keywords/ts.d.ts +38 -1
  60. package/out/keywords/ts.js +20 -4
  61. package/out/keywords/utils.d.ts +1 -1
  62. package/out/keywords/utils.js +1 -1
  63. package/out/{subtypes → methods}/array.d.ts +6 -6
  64. package/out/methods/base.d.ts +183 -0
  65. package/out/{subtypes → methods}/date.d.ts +5 -5
  66. package/out/methods/instantiate.d.ts +17 -0
  67. package/out/methods/morph.d.ts +12 -0
  68. package/out/methods/number.d.ts +12 -0
  69. package/out/methods/object.d.ts +106 -0
  70. package/out/methods/string.d.ts +13 -0
  71. package/out/module.d.ts +2 -2
  72. package/out/parser/{semantic → ast}/bounds.d.ts +3 -3
  73. package/out/parser/ast/default.d.ts +8 -0
  74. package/out/parser/ast/infer.d.ts +39 -0
  75. package/out/parser/{semantic → ast}/utils.d.ts +1 -1
  76. package/out/parser/{semantic → ast}/validate.d.ts +10 -10
  77. package/out/parser/ast/validate.js +1 -0
  78. package/out/parser/definition.d.ts +8 -9
  79. package/out/parser/objectLiteral.d.ts +14 -40
  80. package/out/parser/objectLiteral.js +8 -26
  81. package/out/parser/{string/reduce → reduce}/dynamic.d.ts +9 -12
  82. package/out/parser/{string/reduce → reduce}/dynamic.js +5 -6
  83. package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
  84. package/out/parser/{string/reduce → reduce}/static.d.ts +4 -4
  85. package/out/parser/{string/shift → shift}/operand/date.d.ts +3 -1
  86. package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +6 -4
  87. package/out/parser/{string/shift → shift}/operand/enclosed.js +7 -3
  88. package/out/parser/shift/operand/operand.d.ts +9 -0
  89. package/out/parser/{string/shift → shift}/operand/operand.js +2 -2
  90. package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +14 -7
  91. package/out/parser/{string/shift → shift}/operand/unenclosed.js +12 -7
  92. package/out/parser/{string/shift → shift}/operator/bounds.d.ts +7 -7
  93. package/out/parser/shift/operator/brand.d.ts +6 -0
  94. package/out/parser/shift/operator/brand.js +5 -0
  95. package/out/parser/{string/shift → shift}/operator/default.d.ts +3 -6
  96. package/out/parser/{string/shift → shift}/operator/default.js +4 -6
  97. package/out/parser/{string/shift → shift}/operator/divisor.d.ts +2 -2
  98. package/out/parser/shift/operator/operator.d.ts +14 -0
  99. package/out/parser/{string/shift → shift}/operator/operator.js +7 -5
  100. package/out/parser/shift/scanner.d.ts +47 -0
  101. package/out/parser/shift/scanner.js +42 -0
  102. package/out/parser/{string/string.d.ts → string.d.ts} +6 -7
  103. package/out/parser/{string/string.js → string.js} +2 -0
  104. package/out/parser/tuple.d.ts +60 -44
  105. package/out/parser/tuple.js +36 -30
  106. package/out/scope.d.ts +38 -39
  107. package/out/scope.js +47 -36
  108. package/out/type.d.ts +45 -21
  109. package/out/type.js +14 -10
  110. package/package.json +3 -3
  111. package/out/intersect.d.ts +0 -28
  112. package/out/keys.d.ts +0 -16
  113. package/out/keywords/ark.d.ts +0 -39
  114. package/out/keywords/ast.d.ts +0 -193
  115. package/out/keywords/ast.js +0 -2
  116. package/out/parser/semantic/default.d.ts +0 -9
  117. package/out/parser/semantic/default.js +0 -1
  118. package/out/parser/semantic/infer.d.ts +0 -39
  119. package/out/parser/semantic/validate.js +0 -1
  120. package/out/parser/string/shift/operand/operand.d.ts +0 -9
  121. package/out/parser/string/shift/operator/operator.d.ts +0 -13
  122. package/out/parser/string/shift/scanner.d.ts +0 -69
  123. package/out/parser/string/shift/scanner.js +0 -100
  124. package/out/subtypes/base.d.ts +0 -85
  125. package/out/subtypes/instantiate.d.ts +0 -16
  126. package/out/subtypes/morph.d.ts +0 -13
  127. package/out/subtypes/number.d.ts +0 -12
  128. package/out/subtypes/object.d.ts +0 -33
  129. package/out/subtypes/string.d.ts +0 -13
  130. package/out/subtypes/validator.d.ts +0 -11
  131. package/out/subtypes/validator.js +0 -1
  132. /package/out/{intersect.js → keywords/constructors/Date.js} +0 -0
  133. /package/out/{subtypes → methods}/array.js +0 -0
  134. /package/out/{subtypes → methods}/base.js +0 -0
  135. /package/out/{subtypes → methods}/date.js +0 -0
  136. /package/out/{subtypes → methods}/instantiate.js +0 -0
  137. /package/out/{subtypes → methods}/morph.js +0 -0
  138. /package/out/{subtypes → methods}/number.js +0 -0
  139. /package/out/{subtypes → methods}/object.js +0 -0
  140. /package/out/{subtypes → methods}/string.js +0 -0
  141. /package/out/parser/{semantic → ast}/bounds.js +0 -0
  142. /package/out/{keys.js → parser/ast/default.js} +0 -0
  143. /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
  144. /package/out/parser/{semantic → ast}/divisor.js +0 -0
  145. /package/out/parser/{semantic → ast}/infer.js +0 -0
  146. /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
  147. /package/out/parser/{semantic → ast}/keyof.js +0 -0
  148. /package/out/parser/{semantic → ast}/utils.js +0 -0
  149. /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
  150. /package/out/parser/{string/reduce → reduce}/static.js +0 -0
  151. /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
  152. /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
  153. /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
  154. /package/out/parser/{string/shift → shift}/operator/bounds.js +0 -0
  155. /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
@@ -1,25 +1,18 @@
1
- import { $ark, makeRootAndArrayPropertiesMutable } from "@ark/schema";
2
- import { append, objectKindOrDomainOf, throwParseError } from "@ark/util";
3
- import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
1
+ import { $ark, Disjoint, intersectNodesRoot, makeRootAndArrayPropertiesMutable } from "@ark/schema";
2
+ import { append, isEmptyObject, objectKindOrDomainOf, throwParseError } from "@ark/util";
3
+ import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.js";
4
4
  export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
5
5
  export const parseTupleLiteral = (def, ctx) => {
6
6
  let sequences = [{}];
7
7
  let i = 0;
8
8
  while (i < def.length) {
9
9
  let spread = false;
10
- let optional = false;
11
10
  if (def[i] === "..." && i < def.length - 1) {
12
11
  spread = true;
13
12
  i++;
14
13
  }
15
- const element = ctx.$.parse(def[i], ctx);
14
+ const element = ctx.$.parseOwnDefinitionFormat(def[i], ctx);
16
15
  i++;
17
- if (def[i] === "?") {
18
- if (spread)
19
- return throwParseError(spreadOptionalMessage);
20
- optional = true;
21
- i++;
22
- }
23
16
  if (spread) {
24
17
  if (!element.extends($ark.intrinsic.Array))
25
18
  return throwParseError(writeNonArraySpreadMessage(element.expression));
@@ -31,13 +24,18 @@ export const parseTupleLiteral = (def, ctx) => {
31
24
  element.distribute(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
32
25
  }
33
26
  else {
34
- sequences = sequences.map(base => appendElement(base, optional ? "optional" : "required", element));
27
+ sequences = sequences.map(base => appendElement(base, element.meta.optional ? "optional" : "required", element));
35
28
  }
36
29
  }
37
- return ctx.$.internal.rootNode(sequences.map(sequence => ({
38
- proto: Array,
39
- sequence
40
- })));
30
+ return ctx.$.parseSchema(sequences.map(sequence => isEmptyObject(sequence) ?
31
+ {
32
+ proto: Array,
33
+ exactLength: 0
34
+ }
35
+ : {
36
+ proto: Array,
37
+ sequence
38
+ }));
41
39
  };
42
40
  const appendElement = (base, kind, element) => {
43
41
  switch (kind) {
@@ -86,11 +84,11 @@ const appendSpreadBranch = (base, branch) => {
86
84
  // the only array with no sequence reference is unknown[]
87
85
  return appendElement(base, "variadic", $ark.intrinsic.unknown);
88
86
  }
89
- spread.prefix.forEach(node => appendElement(base, "required", node));
90
- spread.optionals.forEach(node => appendElement(base, "optional", node));
87
+ spread.prefix?.forEach(node => appendElement(base, "required", node));
88
+ spread.optionals?.forEach(node => appendElement(base, "optional", node));
91
89
  if (spread.variadic)
92
90
  appendElement(base, "variadic", spread.variadic);
93
- spread.postfix.forEach(node => appendElement(base, "required", node));
91
+ spread.postfix?.forEach(node => appendElement(base, "required", node));
94
92
  return base;
95
93
  };
96
94
  const maybeParseTupleExpression = (def, ctx) => {
@@ -104,38 +102,46 @@ export const multipleVariadicMesage = "A tuple may have at most one variadic ele
104
102
  export const requiredPostOptionalMessage = "A required element may not follow an optional element";
105
103
  export const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
106
104
  export const spreadOptionalMessage = "A spread element cannot be optional";
107
- export const parseKeyOfTuple = (def, ctx) => ctx.$.parse(def[1], ctx).keyof();
105
+ export const parseKeyOfTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[1], ctx).keyof();
108
106
  const parseBranchTuple = (def, ctx) => {
109
107
  if (def[2] === undefined)
110
108
  return throwParseError(writeMissingRightOperandMessage(def[1], ""));
111
- const l = ctx.$.parse(def[0], ctx);
112
- const r = ctx.$.parse(def[2], ctx);
113
- return def[1] === "&" ? l.and(r) : l.or(r);
109
+ const l = ctx.$.parseOwnDefinitionFormat(def[0], ctx);
110
+ const r = ctx.$.parseOwnDefinitionFormat(def[2], ctx);
111
+ if (def[1] === "|")
112
+ return ctx.$.node("union", { branches: [l, r] });
113
+ const result = intersectNodesRoot(l, r, ctx.$);
114
+ if (result instanceof Disjoint)
115
+ return result.throw();
116
+ return result;
114
117
  };
115
- const parseArrayTuple = (def, ctx) => ctx.$.parse(def[0], ctx).array();
118
+ const parseArrayTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).array();
116
119
  const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
117
120
  export const parseMorphTuple = (def, ctx) => {
118
121
  if (typeof def[2] !== "function") {
119
122
  return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
120
123
  }
121
- // TODO: nested morphs?
122
- return ctx.$.parse(def[0], ctx).pipe(def[2]);
124
+ return ctx.$.parseOwnDefinitionFormat(def[0], ctx).pipe(def[2]);
123
125
  };
124
126
  export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
125
127
  export const parseNarrowTuple = (def, ctx) => {
126
128
  if (typeof def[2] !== "function") {
127
129
  return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
128
130
  }
129
- return ctx.$.parse(def[0], ctx).constrain("predicate", def[2]);
131
+ return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
130
132
  };
131
- const parseAttributeTuple = (def, ctx) => ctx.$.parse(def[0], ctx).configureShallowDescendants(def[2]);
133
+ const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
134
+ const parseDefaultTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).default(def[2]);
135
+ const parseOptionalTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).optional();
132
136
  const indexOneParsers = {
137
+ "[]": parseArrayTuple,
138
+ "?": parseOptionalTuple,
133
139
  "|": parseBranchTuple,
134
140
  "&": parseBranchTuple,
135
- "[]": parseArrayTuple,
136
141
  ":": parseNarrowTuple,
137
142
  "=>": parseMorphTuple,
138
- "@": parseAttributeTuple
143
+ "@": parseAttributeTuple,
144
+ "=": parseDefaultTuple
139
145
  };
140
146
  const prefixParsers = {
141
147
  keyof: parseKeyOfTuple,
package/out/scope.d.ts CHANGED
@@ -1,22 +1,24 @@
1
- import { BaseScope, type AliasDefEntry, type ArkScopeConfig, type BaseNode, type BaseRoot, type GenericArgResolutions, type GenericAst, type GenericParamAst, type NodeKind, type NodeParseOptions, type NodeSchema, type PreparsedNodeResolution, type PrivateDeclaration, type RootKind, type RootSchema, type arkKind, type exportedNameOf, type nodeOfKind, type reducibleKindOf, type toInternalScope, type writeDuplicateAliasError } from "@ark/schema";
2
- import { type ErrorType, type Json, type anyOrNever, type array, type flattenListable, type inferred, type noSuggest, type nominal } from "@ark/util";
3
- import type { ArkAmbient, ArkSchemaRegistry } from "./config.ts";
4
- import { parseGenericParams, type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseValidGenericParams } from "./generic.ts";
5
- import type { Ark, type } from "./keywords/ark.ts";
1
+ import { BaseScope, type AliasDefEntry, type ArkScopeConfig, type BaseNode, type BaseParseContext, type BaseParseContextInput, type BaseParseOptions, type BaseRoot, type GenericAst, type GenericParamAst, type GenericParamDef, type NodeKind, type NodeSchema, type PreparsedNodeResolution, type PrivateDeclaration, type RootKind, type RootSchema, type arkKind, type exportedNameOf, type nodeOfKind, type reducibleKindOf, type toInternalScope, type writeDuplicateAliasError } from "@ark/schema";
2
+ import { type ErrorType, type Json, type anyOrNever, type array, type flattenListable, type noSuggest, type nominal } from "@ark/util";
3
+ import type { ArkSchemaRegistry } from "./config.ts";
4
+ import { type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseGenericParams, type parseValidGenericParams } from "./generic.ts";
5
+ import type { Ark, type } from "./keywords/keywords.ts";
6
6
  import type { BoundModule, Module, Submodule, instantiateExport } from "./module.ts";
7
+ import type { DefAst, InferredAst } from "./parser/ast/infer.ts";
7
8
  import { type inferDefinition, type validateDefinition } from "./parser/definition.ts";
8
- import type { DefAst, InferredAst } from "./parser/semantic/infer.ts";
9
- import type { ParsedDefault } from "./parser/string/shift/operator/default.ts";
10
- import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type Type, type TypeParser } from "./type.ts";
11
- export type ScopeParser = <const def>(def: validateScope<def>, config?: ArkScopeConfig) => Scope<inferScope<def>>;
12
- export type ModuleParser = <const def>(def: validateScope<def>, config?: ArkScopeConfig) => inferScope<def> extends infer $ ? Module<{
9
+ import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type InstanceOfTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser } from "./type.ts";
10
+ /** The convenience properties attached to `scope` */
11
+ export type ScopeParserAttachments = Omit<ScopeParser, never>;
12
+ export interface ScopeParser {
13
+ <const def>(def: scope.validate<def>, config?: ArkScopeConfig): Scope<scope.infer<def>>;
14
+ define: <const def>(def: scope.validate<def>) => def;
15
+ }
16
+ export type ModuleParser = <const def>(def: scope.validate<def>, config?: ArkScopeConfig) => scope.infer<def> extends infer $ ? Module<{
13
17
  [k in exportedNameOf<$>]: $[k];
14
18
  }> : never;
15
- export type validateScope<def> = {
16
- [k in keyof def]: k extends noSuggest ? unknown : parseScopeKey<k, def>["params"] extends infer params ? params extends array<GenericParamAst> ? params["length"] extends 0 ? def[k] extends Type.Any | PreparsedResolution ? def[k] : k extends PrivateDeclaration<infer name extends keyof def & string> ? ErrorType<writeDuplicateAliasError<name>> : validateDefinition<def[k], bootstrapAliases<def>, {}> : validateDefinition<def[k], bootstrapAliases<def>, baseGenericConstraints<params>> : params : never;
19
+ export type bindThis<def> = {
20
+ this: Def<def>;
17
21
  };
18
- export type inferScope<def> = inferBootstrapped<bootstrapAliases<def>>;
19
- export type bindThis<def> = {};
20
22
  /** nominal type for an unparsed definition used during scope bootstrapping */
21
23
  type Def<def = {}> = nominal<def, "unparsed">;
22
24
  /** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
@@ -26,9 +28,9 @@ type PreparsedResolution = PreparsedNodeResolution;
26
28
  type bootstrapAliases<def> = {
27
29
  [k in Exclude<keyof def, GenericDeclaration>]: def[k] extends (PreparsedResolution) ? def[k] extends {
28
30
  t: infer g extends GenericAst;
29
- } ? g : def[k] extends Module<infer $> ? Submodule<$> : def[k] : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ? thunkReturn extends {
31
+ } ? g : def[k] extends Module<infer $> | BoundModule<infer $, any> ? Submodule<$> : def[k] : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ? thunkReturn extends {
30
32
  t: infer g extends GenericAst;
31
- } ? g : thunkReturn extends Module<infer $> ? Submodule<$> : thunkReturn : Def<def[k]>;
33
+ } ? g : thunkReturn extends Module<infer $> | BoundModule<infer $, any> ? Submodule<$> : thunkReturn : Def<def[k]>;
32
34
  } & {
33
35
  [k in keyof def & GenericDeclaration as extractGenericName<k>]: GenericAst<parseValidGenericParams<extractGenericParameters<k>, bootstrapAliases<def>>, def[k], UnparsedScope>;
34
36
  };
@@ -40,18 +42,12 @@ type inferBootstrapped<$> = {
40
42
  export type bindGenericToScope<g extends GenericAst, $> = GenericAst<g["paramsAst"], g["bodyDef"], g["$"] extends UnparsedScope ? $ : g["$"], $>;
41
43
  type extractGenericName<k> = k extends GenericDeclaration<infer name> ? name : never;
42
44
  type extractGenericParameters<k> = k extends `${string}<${infer params}>` ? ParameterString<params> : never;
43
- export type resolve<reference extends keyof $ | keyof args, $, args> = inferResolution<reference extends keyof args ? args[reference] : $[reference & keyof $], $, args>;
44
45
  export type resolutionToAst<alias extends string, resolution> = [
45
46
  resolution
46
47
  ] extends [anyOrNever] ? InferredAst<resolution, alias> : resolution extends Def<infer def> ? DefAst<def, alias> : resolution extends {
47
48
  [arkKind]: "module";
48
- $root: infer root;
49
+ root: infer root;
49
50
  } ? InferredAst<root, alias> : resolution extends GenericAst ? resolution : InferredAst<resolution, alias>;
50
- export type inferResolution<resolution, $, args> = [
51
- resolution
52
- ] extends [anyOrNever] ? resolution : resolution extends {
53
- [inferred]: infer t;
54
- } ? t : resolution extends Def<infer def> ? inferDefinition<def, $, args> : resolution;
55
51
  export type moduleKeyOf<$> = {
56
52
  [k in keyof $]: $[k] extends {
57
53
  [arkKind]: "module";
@@ -59,30 +55,26 @@ export type moduleKeyOf<$> = {
59
55
  $[k]
60
56
  ] extends [anyOrNever] ? never : k & string : never;
61
57
  }[keyof $];
62
- type unwrapPreinferred<t> = t extends type.cast<infer inferred> ? inferred : t;
63
- export type tryInferSubmoduleReference<$, token> = token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ? subalias extends keyof $[submodule] ? unwrapPreinferred<$[submodule][subalias]> : tryInferSubmoduleReference<$[submodule], subalias> : token extends (`${infer submodule extends moduleKeyOf<ArkAmbient.$>}.${infer subalias}`) ? subalias extends keyof ArkAmbient.$[submodule] ? unwrapPreinferred<ArkAmbient.$[submodule][subalias]> : tryInferSubmoduleReference<ArkAmbient.$[submodule], subalias> : never;
64
58
  export interface ArkTypeRegistry extends ArkSchemaRegistry {
65
59
  typeAttachments?: Ark.boundTypeAttachments<any>;
66
60
  }
67
61
  export declare const $arkTypeRegistry: ArkTypeRegistry;
68
- export interface ParseContext extends TypeParseOptions {
69
- $: InternalScope;
70
- }
71
- export interface TypeParseOptions {
72
- args?: GenericArgResolutions;
73
- }
74
62
  export interface InternalScope {
75
63
  constructor: typeof InternalScope;
76
64
  }
77
65
  export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
78
66
  private parseCache;
79
- preparseAlias(k: string, v: unknown): AliasDefEntry;
80
67
  protected cacheGetter<name extends keyof this>(name: name, value: this[name]): this[name];
81
- preparseRoot(def: unknown): unknown;
82
68
  get ambientAttachments(): Ark.boundTypeAttachments<$> | undefined;
83
- parse<defaultable extends boolean = false>(def: unknown, ctx: ParseContext, defaultable?: defaultable): BaseRoot | (defaultable extends false ? never : ParsedDefault);
84
- parseString<defaultable extends boolean>(def: string, ctx: ParseContext, defaultable: defaultable): BaseRoot | (defaultable extends false ? never : ParsedDefault);
85
- parseRoot: (def: unknown, opts?: TypeParseOptions) => BaseRoot;
69
+ protected preparseOwnAliasEntry(k: string, v: unknown): AliasDefEntry;
70
+ parseGenericParams(def: string, opts: BaseParseOptions): array<GenericParamDef>;
71
+ protected normalizeRootScopeValue(resolution: unknown): unknown;
72
+ protected preparseOwnDefinitionFormat(def: unknown, opts: BaseParseOptions): BaseRoot | BaseParseContextInput;
73
+ parseOwnDefinitionFormat(def: unknown, ctx: BaseParseContext): BaseRoot;
74
+ parseString(def: string, ctx: BaseParseContext): BaseRoot;
75
+ unit: UnitTypeParser<$>;
76
+ enumerated: EnumeratedTypeParser<$>;
77
+ instanceOf: InstanceOfTypeParser<$>;
86
78
  type: InternalTypeParser;
87
79
  declare: () => {
88
80
  type: InternalTypeParser;
@@ -92,6 +84,12 @@ export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
92
84
  static module: ModuleParser;
93
85
  }
94
86
  export declare const scope: ScopeParser;
87
+ export declare namespace scope {
88
+ type validate<def> = {
89
+ [k in keyof def]: k extends noSuggest ? unknown : parseScopeKey<k, def>["params"] extends infer params ? params extends array<GenericParamAst> ? params["length"] extends 0 ? def[k] extends type.Any | PreparsedResolution ? def[k] : k extends (PrivateDeclaration<infer name extends keyof def & string>) ? ErrorType<writeDuplicateAliasError<name>> : validateDefinition<def[k], bootstrapAliases<def>, {}> : validateDefinition<def[k], bootstrapAliases<def>, baseGenericConstraints<params>> : params : never;
90
+ };
91
+ type infer<def> = inferBootstrapped<bootstrapAliases<def>>;
92
+ }
95
93
  export declare const module: ModuleParser;
96
94
  export interface Scope<$ = {}> {
97
95
  t: $;
@@ -105,12 +103,14 @@ export interface Scope<$ = {}> {
105
103
  aliases: Record<string, unknown>;
106
104
  internal: toInternalScope<$>;
107
105
  defineSchema<const def extends RootSchema>(schema: def): def;
108
- schema(schema: RootSchema, opts?: NodeParseOptions): BaseRoot;
109
- node<kinds extends NodeKind | array<RootKind>>(kinds: kinds, schema: NodeSchema<flattenListable<kinds>>, opts?: NodeParseOptions): nodeOfKind<reducibleKindOf<flattenListable<kinds>>>;
106
+ node<kinds extends NodeKind | array<RootKind>>(kinds: kinds, schema: NodeSchema<flattenListable<kinds>>, opts?: BaseParseOptions): nodeOfKind<reducibleKindOf<flattenListable<kinds>>>;
107
+ unit: UnitTypeParser<$>;
108
+ enumerated: EnumeratedTypeParser<$>;
110
109
  type: TypeParser<$>;
111
110
  declare: DeclarationParser<$>;
112
111
  define: DefinitionParser<$>;
113
112
  generic: GenericParser<$>;
113
+ schema: SchemaParser<$>;
114
114
  import(): Module<{
115
115
  [k in exportedNameOf<$> as PrivateDeclaration<k>]: $[k];
116
116
  }>;
@@ -131,7 +131,6 @@ export interface ScopeConstructor {
131
131
  module: ModuleParser;
132
132
  }
133
133
  export declare const Scope: ScopeConstructor;
134
- export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
135
134
  export type parseScopeKey<k, def> = k extends `${infer name}<${infer params}>` ? parseGenericScopeKey<name, params, def> : {
136
135
  name: k;
137
136
  params: [];
package/out/scope.js CHANGED
@@ -1,16 +1,28 @@
1
1
  import { $ark, BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
2
2
  import { domainOf, flatMorph, hasDomain, isThunk, throwParseError } from "@ark/util";
3
- import { parseGenericParams } from "./generic.js";
3
+ import { parseGenericParamName } from "./generic.js";
4
4
  import { parseObject, writeBadDefinitionTypeMessage } from "./parser/definition.js";
5
- import { DynamicState } from "./parser/string/reduce/dynamic.js";
6
- import { writeUnexpectedCharacterMessage } from "./parser/string/shift/operator/operator.js";
7
- import { Scanner } from "./parser/string/shift/scanner.js";
8
- import { fullStringParse } from "./parser/string/string.js";
5
+ import { DynamicState } from "./parser/reduce/dynamic.js";
6
+ import { writeUnexpectedCharacterMessage } from "./parser/shift/operator/operator.js";
7
+ import { ArkTypeScanner } from "./parser/shift/scanner.js";
8
+ import { fullStringParse } from "./parser/string.js";
9
9
  import { InternalTypeParser } from "./type.js";
10
10
  export const $arkTypeRegistry = $ark;
11
11
  export class InternalScope extends BaseScope {
12
12
  parseCache = {};
13
- preparseAlias(k, v) {
13
+ cacheGetter(name, value) {
14
+ Object.defineProperty(this, name, { value });
15
+ return value;
16
+ }
17
+ get ambientAttachments() {
18
+ if (!$arkTypeRegistry.typeAttachments)
19
+ return;
20
+ return this.cacheGetter("ambientAttachments", flatMorph($arkTypeRegistry.typeAttachments, (k, v) => [
21
+ k,
22
+ this.bindReference(v)
23
+ ]));
24
+ }
25
+ preparseOwnAliasEntry(k, v) {
14
26
  const firstParamIndex = k.indexOf("<");
15
27
  if (firstParamIndex === -1)
16
28
  return [k, v];
@@ -24,47 +36,46 @@ export class InternalScope extends BaseScope {
24
36
  // use a thunk definition for the generic so that we can parse
25
37
  // constraints within the current scope
26
38
  () => {
27
- const params = parseGenericParams(paramString, {
28
- $: this,
29
- args: {}
30
- });
39
+ const params = this.parseGenericParams(paramString, { alias: name });
31
40
  const generic = parseGeneric(params, v, this);
32
41
  return generic;
33
42
  }
34
43
  ];
35
44
  }
36
- cacheGetter(name, value) {
37
- Object.defineProperty(this, name, { value });
38
- return value;
45
+ parseGenericParams(def, opts) {
46
+ return parseGenericParamName(new ArkTypeScanner(def), [], this.createParseContext({ ...opts, def, prefix: "generic" }));
39
47
  }
40
- preparseRoot(def) {
41
- if (isThunk(def) && !hasArkKind(def, "generic"))
42
- return def();
43
- return def;
48
+ normalizeRootScopeValue(resolution) {
49
+ if (isThunk(resolution) && !hasArkKind(resolution, "generic"))
50
+ return resolution();
51
+ return resolution;
44
52
  }
45
- get ambientAttachments() {
46
- if (!$arkTypeRegistry.typeAttachments)
47
- return;
48
- return this.cacheGetter("ambientAttachments", flatMorph($arkTypeRegistry.typeAttachments, (k, v) => [
49
- k,
50
- v.bindScope(this)
51
- ]));
53
+ preparseOwnDefinitionFormat(def, opts) {
54
+ return {
55
+ ...opts,
56
+ def,
57
+ prefix: opts.alias ?? "type"
58
+ };
52
59
  }
53
- parse(def, ctx, defaultable = false) {
60
+ parseOwnDefinitionFormat(def, ctx) {
61
+ const isScopeAlias = ctx.alias && ctx.alias in this.aliases;
62
+ // if the definition being parsed is not a scope alias and is not a
63
+ // generic instantiation (i.e. opts don't include args), add this as a resolution.
64
+ if (!isScopeAlias && !ctx.args)
65
+ ctx.args = { this: ctx.id };
54
66
  if (typeof def === "string") {
55
67
  if (ctx.args && Object.keys(ctx.args).some(k => def.includes(k))) {
56
68
  // we can only rely on the cache if there are no contextual
57
69
  // resolutions like "this" or generic args
58
- return this.parseString(def, ctx, defaultable);
70
+ return this.parseString(def, ctx);
59
71
  }
60
- const contextKey = `${def}${defaultable}`;
61
- return (this.parseCache[contextKey] ??= this.parseString(def, ctx, defaultable));
72
+ return (this.parseCache[def] ??= this.parseString(def, ctx));
62
73
  }
63
74
  return hasDomain(def, "object") ?
64
75
  parseObject(def, ctx)
65
76
  : throwParseError(writeBadDefinitionTypeMessage(domainOf(def)));
66
77
  }
67
- parseString(def, ctx, defaultable) {
78
+ parseString(def, ctx) {
68
79
  const aliasResolution = this.maybeResolveRoot(def);
69
80
  if (aliasResolution)
70
81
  return aliasResolution;
@@ -73,16 +84,15 @@ export class InternalScope extends BaseScope {
73
84
  : undefined;
74
85
  if (aliasArrayResolution)
75
86
  return aliasArrayResolution;
76
- const s = new DynamicState(new Scanner(def), ctx, defaultable);
87
+ const s = new DynamicState(new ArkTypeScanner(def), ctx);
77
88
  const node = fullStringParse(s);
78
89
  if (s.finalizer === ">")
79
90
  throwParseError(writeUnexpectedCharacterMessage(">"));
80
91
  return node;
81
92
  }
82
- parseRoot = (def, opts = {}) => {
83
- const node = this.parse(def, Object.assign(this.finalizeRootArgs(opts, () => node), { $: this })).bindScope(this);
84
- return node;
85
- };
93
+ unit = value => this.units([value]);
94
+ enumerated = (...values) => this.units(values);
95
+ instanceOf = ctor => this.node("proto", { proto: ctor }, { prereduced: true });
86
96
  type = new InternalTypeParser(this);
87
97
  declare = () => ({
88
98
  type: this.type
@@ -91,7 +101,8 @@ export class InternalScope extends BaseScope {
91
101
  static scope = ((def, config = {}) => new InternalScope(def, config));
92
102
  static module = ((def, config = {}) => this.scope(def, config).export());
93
103
  }
94
- export const scope = InternalScope.scope;
104
+ export const scope = Object.assign(InternalScope.scope, {
105
+ define: def => def
106
+ });
95
107
  export const module = InternalScope.module;
96
108
  export const Scope = InternalScope;
97
- export const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`;
package/out/type.d.ts CHANGED
@@ -1,25 +1,52 @@
1
- import { ArkErrors, BaseRoot, type MetaSchema, type Morph, type Predicate } from "@ark/schema";
2
- import { Callable, type Constructor, type array, type conform } from "@ark/util";
3
- import { type Generic, type GenericParser, type ParameterString, type baseGenericConstraints, type parseValidGenericParams, type validateParameterString } from "./generic.ts";
4
- import type { Ark, ark } from "./keywords/ark.ts";
5
- import type { distillIn, distillOut } from "./keywords/ast.ts";
6
- import type { inferDefinition, validateDeclared, validateDefinition } from "./parser/definition.ts";
1
+ import { ArkErrors, BaseRoot, type BaseParseOptions, type MetaSchema, type Morph, type Predicate, type RootSchema } from "@ark/schema";
2
+ import { Callable, Hkt, type Constructor, type array, type conform } from "@ark/util";
3
+ import type { DefaultFor, distill } from "./attributes.ts";
4
+ import type { Generic, GenericParser, ParameterString, baseGenericConstraints, parseValidGenericParams, validateParameterString } from "./generic.ts";
5
+ import type { Ark, keywords, type } from "./keywords/keywords.ts";
6
+ import type { BaseType } from "./methods/base.ts";
7
+ import type { instantiateType } from "./methods/instantiate.ts";
8
+ import type { validateDeclared, validateDefinition } from "./parser/definition.ts";
7
9
  import type { IndexOneOperator, IndexZeroOperator, TupleInfixOperator } from "./parser/tuple.ts";
8
10
  import type { InternalScope, ModuleParser, Scope, ScopeParser, bindThis } from "./scope.ts";
9
- import type { BaseType } from "./subtypes/base.ts";
10
- import type { instantiateType } from "./subtypes/instantiate.ts";
11
11
  /** The convenience properties attached to `type` */
12
12
  export type TypeParserAttachments = Omit<TypeParser, never>;
13
13
  export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
14
- <const def, r = Type<inferTypeRoot<def, $>, $>>(def: validateTypeRoot<def, $>): r;
15
- <params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
16
- <const zero, const one, const rest extends array, r = Type<inferTypeRoot<[zero, one, ...rest], $>, $>>(_0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>, _1: zero extends "keyof" ? validateTypeRoot<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, IndexOneOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distillIn<inferTypeRoot<zero, $>>>] : one extends "=>" ? [Morph<distillOut<inferTypeRoot<zero, $>>, unknown>] : one extends "@" ? [MetaSchema] : [validateTypeRoot<rest[0], $>] : []): r;
17
- raw(def: unknown): BaseType<any, $>;
14
+ <const def, r = Type<type.infer<def, $>, $>>(def: type.validate<def, $>): r;
15
+ <const params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
16
+ <const zero, const one, const rest extends array, r = Type<type.infer<[zero, one, ...rest], $>, $>>(_0: zero extends IndexZeroOperator ? zero : type.validate<zero, $>, _1: zero extends "keyof" ? type.validate<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, IndexOneOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distill.In<type.infer<zero, $>>>] : one extends "=>" ? [Morph<distill.Out<type.infer<zero, $>>, unknown>] : one extends "@" ? [MetaSchema] : one extends "=" ? [DefaultFor<distill.In<type.infer<NoInfer<zero>, $>>>] : [type.validate<rest[0], $>] : []): r;
17
+ /**
18
+ * Error class for validation errors
19
+ * Calling type instance returns an instance of this class on failure
20
+ * @example if ( T(data) instanceof type.errors ) { ... }
21
+ */
18
22
  errors: typeof ArkErrors;
23
+ hkt: typeof Hkt;
24
+ keywords: typeof keywords;
25
+ $: Scope<$>;
26
+ raw(def: unknown): BaseType<any, $>;
19
27
  module: ModuleParser;
20
28
  scope: ScopeParser;
29
+ define: DefinitionParser<$>;
21
30
  generic: GenericParser<$>;
22
- ark: typeof ark;
31
+ schema: SchemaParser<$>;
32
+ /**
33
+ * Create a `Type` that is satisfied only by a value strictly equal (`===`) to the argument passed to this function.
34
+ * @example const foo = type.unit('foo') // Type<'foo'>
35
+ * @example const sym: unique symbol = Symbol(); type.unit(sym) // Type<typeof sym>
36
+ */
37
+ unit: UnitTypeParser<$>;
38
+ /**
39
+ * Create a `Type` that is satisfied only by a value strictly equal (`===`) to one of the arguments passed to this function.
40
+ * @example const enum = type.enumerated('foo', 'bar', obj) // obj is a by-reference object
41
+ * @example const tupleForm = type(['===', 'foo', 'bar', obj])
42
+ * @example const argsForm = type('===', 'foo', 'bar', obj)
43
+ */
44
+ enumerated: EnumeratedTypeParser<$>;
45
+ /**
46
+ * Create a `Type` that is satisfied only by a value of a specific class.
47
+ * @example const array = type.instanceOf(Array)
48
+ */
49
+ instanceOf: InstanceOfTypeParser<$>;
23
50
  }
24
51
  export declare class InternalTypeParser extends Callable<(...args: unknown[]) => BaseRoot | Generic, TypeParserAttachments> {
25
52
  constructor($: InternalScope);
@@ -27,14 +54,11 @@ export declare class InternalTypeParser extends Callable<(...args: unknown[]) =>
27
54
  export type DeclarationParser<$> = <preinferred>() => {
28
55
  type: <const def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
29
56
  };
30
- export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
31
- export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $, bindThis<def>>;
32
- export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>;
33
- export type validateAmbient<def> = validateTypeRoot<def, {}>;
34
- export type inferAmbient<def> = inferTypeRoot<def, {}>;
57
+ export type UnitTypeParser<$> = <const t>(value: t) => Type<t, $>;
58
+ export type InstanceOfTypeParser<$> = <const t extends object>(ctor: Constructor<t>) => Type<t, $>;
59
+ export type EnumeratedTypeParser<$> = <const values extends readonly unknown[]>(...values: values) => Type<values[number], $>;
60
+ export type DefinitionParser<$> = <const def>(def: type.validate<def, $>) => def;
61
+ export type SchemaParser<$> = (schema: RootSchema, opts?: BaseParseOptions) => Type<unknown, $>;
35
62
  export type Type<t = unknown, $ = {}> = instantiateType<t, $>;
36
- export declare namespace Type {
37
- type Any<t = any> = BaseType<t, any>;
38
- }
39
63
  export type TypeConstructor<t = unknown, $ = {}> = new (def: unknown, $: Scope<$>) => Type<t, $>;
40
64
  export declare const Type: TypeConstructor;
package/out/type.js CHANGED
@@ -1,23 +1,29 @@
1
1
  import { ArkErrors, BaseRoot, GenericRoot } from "@ark/schema";
2
- import { Callable } from "@ark/util";
3
- import { parseGenericParams } from "./generic.js";
2
+ import { Callable, Hkt } from "@ark/util";
4
3
  export class InternalTypeParser extends Callable {
5
4
  constructor($) {
6
5
  const attach = Object.assign({
7
6
  errors: ArkErrors,
8
- raw: $.parseRoot,
7
+ hkt: Hkt,
8
+ $: $,
9
+ raw: $.parse,
9
10
  module: $.constructor.module,
10
11
  scope: $.constructor.scope,
12
+ define: $.define,
11
13
  generic: $.generic,
14
+ schema: $.schema,
12
15
  // this won't be defined during bootstrapping, but externally always will be
13
- ark: $.ambient
16
+ keywords: $.ambient,
17
+ unit: $.unit,
18
+ enumerated: $.enumerated,
19
+ instanceOf: $.instanceOf
14
20
  },
15
21
  // also won't be defined during bootstrapping
16
22
  $.ambientAttachments);
17
23
  super((...args) => {
18
24
  if (args.length === 1) {
19
25
  // treat as a simple definition
20
- return $.parseRoot(args[0]);
26
+ return $.parse(args[0]);
21
27
  }
22
28
  if (args.length === 2 &&
23
29
  typeof args[0] === "string" &&
@@ -25,16 +31,14 @@ export class InternalTypeParser extends Callable {
25
31
  args[0].at(-1) === ">") {
26
32
  // if there are exactly two args, the first of which looks like <${string}>,
27
33
  // treat as a generic
28
- const params = parseGenericParams(args[0].slice(1, -1), {
29
- $,
30
- args: {}
31
- });
34
+ const paramString = args[0].slice(1, -1);
35
+ const params = $.parseGenericParams(paramString, {});
32
36
  return new GenericRoot(params, args[1], $, $);
33
37
  }
34
38
  // otherwise, treat as a tuple expression. technically, this also allows
35
39
  // non-expression tuple definitions to be parsed, but it's not a supported
36
40
  // part of the API as specified by the associated types
37
- return $.parseRoot(args);
41
+ return $.parse(args);
38
42
  }, {
39
43
  bind: $,
40
44
  attach
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "arktype",
3
3
  "description": "TypeScript's 1:1 validator, optimized from editor to runtime",
4
- "version": "2.0.0-rc.2",
4
+ "version": "2.0.0-rc.22",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "David Blass",
@@ -34,8 +34,8 @@
34
34
  "out"
35
35
  ],
36
36
  "dependencies": {
37
- "@ark/util": "0.5.0",
38
- "@ark/schema": "0.5.0"
37
+ "@ark/util": "0.22.0",
38
+ "@ark/schema": "0.22.0"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"
@@ -1,28 +0,0 @@
1
- import type { anyOrNever, array, Hkt, intersectArrays, show } from "@ark/util";
2
- import type { constrain, Constraints, MorphAst, parseConstraints, To } from "./keywords/ast.ts";
3
- export type inferIntersection<l, r> = _inferIntersection<l, r, false>;
4
- export type inferPipe<l, r> = _inferIntersection<l, r, true>;
5
- type _inferIntersection<l, r, piped extends boolean> = [
6
- l & r
7
- ] extends [infer t extends anyOrNever] ? t : l extends MorphAst<infer lIn, infer lOut> ? r extends MorphAst<any, infer rOut> ? piped extends true ? (In: lIn) => rOut : never : piped extends true ? (In: lIn) => To<r> : (In: _inferIntersection<lIn, r, false>) => lOut : r extends MorphAst<infer rIn, infer rOut> ? (In: _inferIntersection<rIn, l, false>) => rOut : parseConstraints<l> extends ([
8
- infer lBase,
9
- infer lConstraints extends Constraints
10
- ]) ? parseConstraints<r> extends ([
11
- infer rBase,
12
- infer rConstraints extends Constraints
13
- ]) ? constrain<_inferIntersection<lBase, rBase, piped>, lConstraints & rConstraints> : constrain<_inferIntersection<lBase, r, piped>, lConstraints> : parseConstraints<r> extends ([
14
- infer rBase,
15
- infer rConstraints extends Constraints
16
- ]) ? constrain<_inferIntersection<l, rBase, piped>, rConstraints> : [l, r] extends [object, object] ? intersectObjects<l, r, piped> extends infer result ? result : never : l & r;
17
- interface MorphableIntersection<piped extends boolean> extends Hkt<[unknown, unknown]> {
18
- body: _inferIntersection<this[0], this[1], piped>;
19
- }
20
- type intersectObjects<l, r, piped extends boolean> = [
21
- l,
22
- r
23
- ] extends [infer lList extends array, infer rList extends array] ? intersectArrays<lList, rList, MorphableIntersection<piped>> : show<{
24
- [k in keyof l]: k extends keyof r ? _inferIntersection<l[k], r[k], piped> : l[k];
25
- } & {
26
- [k in keyof r]: k extends keyof l ? _inferIntersection<l[k], r[k], piped> : r[k];
27
- }>;
28
- export {};
package/out/keys.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { array, join, Key, NonNegativeIntegerLiteral, typeToString } from "@ark/util";
2
- import type { type } from "./keywords/ark.ts";
3
- import type { termOrType } from "./keywords/ast.ts";
4
- export type toArkKey<o, k extends keyof o> = k extends number ? [
5
- o,
6
- number
7
- ] extends [array, k] ? NonNegativeIntegerLiteral : `${k}` : k;
8
- export type arkKeyOf<o> = o extends array ? (number extends o["length"] ? NonNegativeIntegerLiteral : never) | {
9
- [k in keyof o]-?: k extends `${infer index extends number}` ? index | k : never;
10
- }[keyof o & `${number}`] : {
11
- [k in keyof o]: k extends number ? k | `${k}` : k;
12
- }[keyof o];
13
- export type getArkKey<o, k extends arkKeyOf<o>> = o[Extract<k extends NonNegativeIntegerLiteral ? number : k, keyof o>];
14
- export type TypeKey = termOrType<Key>;
15
- export type typeKeyToString<k extends TypeKey> = typeToString<k extends type.cast<infer t> ? t : k>;
16
- export type writeInvalidKeysMessage<o extends string, keys extends array<string>> = `Key${keys["length"] extends 1 ? "" : "s"} ${join<keys, ", ">} ${keys["length"] extends 1 ? "does" : "do"} not exist on ${o}`;