arktype 2.0.0-rc.7 → 2.0.0-rc.9

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 (98) hide show
  1. package/out/config.d.ts +1 -1
  2. package/out/generic.d.ts +9 -9
  3. package/out/generic.js +2 -2
  4. package/out/index.d.ts +1 -1
  5. package/out/index.js +1 -1
  6. package/out/keywords/constructors/Array.d.ts +1 -1
  7. package/out/keywords/constructors/Date.d.ts +1 -1
  8. package/out/keywords/constructors/FormData.d.ts +1 -1
  9. package/out/keywords/{ast.d.ts → inference.d.ts} +42 -20
  10. package/out/keywords/{ark.d.ts → keywords.d.ts} +7 -3
  11. package/out/keywords/{ark.js → keywords.js} +7 -7
  12. package/out/keywords/number/number.d.ts +1 -1
  13. package/out/keywords/string/alpha.d.ts +1 -1
  14. package/out/keywords/string/alphanumeric.d.ts +1 -1
  15. package/out/keywords/string/capitalize.d.ts +1 -1
  16. package/out/keywords/string/creditCard.d.ts +1 -1
  17. package/out/keywords/string/date.d.ts +1 -1
  18. package/out/keywords/string/date.js +6 -6
  19. package/out/keywords/string/digits.d.ts +1 -1
  20. package/out/keywords/string/email.d.ts +1 -1
  21. package/out/keywords/string/integer.d.ts +1 -1
  22. package/out/keywords/string/ip.d.ts +1 -1
  23. package/out/keywords/string/json.d.ts +2 -1
  24. package/out/keywords/string/json.js +18 -10
  25. package/out/keywords/string/lower.d.ts +1 -1
  26. package/out/keywords/string/normalize.d.ts +1 -1
  27. package/out/keywords/string/numeric.d.ts +1 -1
  28. package/out/keywords/string/semver.d.ts +1 -1
  29. package/out/keywords/string/string.d.ts +7 -7
  30. package/out/keywords/string/trim.d.ts +1 -1
  31. package/out/keywords/string/upper.d.ts +1 -1
  32. package/out/keywords/string/url.d.ts +1 -1
  33. package/out/keywords/string/uuid.d.ts +1 -1
  34. package/out/methods/array.d.ts +1 -1
  35. package/out/methods/base.d.ts +26 -23
  36. package/out/methods/date.d.ts +1 -1
  37. package/out/methods/instantiate.d.ts +5 -4
  38. package/out/methods/morph.d.ts +3 -4
  39. package/out/methods/number.d.ts +1 -1
  40. package/out/methods/object.d.ts +55 -30
  41. package/out/methods/string.d.ts +1 -1
  42. package/out/methods/validator.d.ts +1 -1
  43. package/out/parser/{semantic → ast}/bounds.d.ts +3 -3
  44. package/out/parser/{semantic → ast}/default.d.ts +2 -2
  45. package/out/parser/{semantic → ast}/infer.d.ts +6 -8
  46. package/out/parser/{semantic → ast}/utils.d.ts +1 -1
  47. package/out/parser/{semantic → ast}/validate.d.ts +5 -5
  48. package/out/parser/definition.d.ts +5 -5
  49. package/out/parser/objectLiteral.d.ts +3 -3
  50. package/out/parser/{string/reduce → reduce}/dynamic.d.ts +2 -2
  51. package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
  52. package/out/parser/{string/reduce → reduce}/static.d.ts +1 -1
  53. package/out/parser/{string/shift → shift}/operand/date.d.ts +1 -1
  54. package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +3 -3
  55. package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +3 -3
  56. package/out/parser/{string/shift → shift}/operand/unenclosed.js +1 -1
  57. package/out/parser/{string/shift → shift}/operator/bounds.d.ts +3 -3
  58. package/out/parser/{string/shift → shift}/operator/default.d.ts +1 -1
  59. package/out/parser/{string/shift → shift}/scanner.d.ts +3 -1
  60. package/out/parser/{string/shift → shift}/scanner.js +5 -3
  61. package/out/parser/{string/string.d.ts → string.d.ts} +4 -4
  62. package/out/parser/{string/string.js → string.js} +2 -0
  63. package/out/parser/tuple.d.ts +47 -36
  64. package/out/parser/tuple.js +5 -10
  65. package/out/scope.d.ts +4 -4
  66. package/out/scope.js +4 -4
  67. package/out/type.d.ts +8 -10
  68. package/out/type.js +2 -1
  69. package/package.json +3 -3
  70. package/out/intersect.d.ts +0 -26
  71. package/out/intersect.js +0 -1
  72. package/out/keys.d.ts +0 -18
  73. package/out/keys.js +0 -1
  74. /package/out/keywords/{ast.js → inference.js} +0 -0
  75. /package/out/parser/{semantic → ast}/bounds.js +0 -0
  76. /package/out/parser/{semantic → ast}/default.js +0 -0
  77. /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
  78. /package/out/parser/{semantic → ast}/divisor.js +0 -0
  79. /package/out/parser/{semantic → ast}/infer.js +0 -0
  80. /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
  81. /package/out/parser/{semantic → ast}/keyof.js +0 -0
  82. /package/out/parser/{semantic → ast}/utils.js +0 -0
  83. /package/out/parser/{semantic → ast}/validate.js +0 -0
  84. /package/out/parser/{string/reduce → reduce}/dynamic.js +0 -0
  85. /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
  86. /package/out/parser/{string/reduce → reduce}/static.js +0 -0
  87. /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
  88. /package/out/parser/{string/shift → shift}/operand/enclosed.js +0 -0
  89. /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
  90. /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
  91. /package/out/parser/{string/shift → shift}/operand/operand.d.ts +0 -0
  92. /package/out/parser/{string/shift → shift}/operand/operand.js +0 -0
  93. /package/out/parser/{string/shift → shift}/operator/bounds.js +0 -0
  94. /package/out/parser/{string/shift → shift}/operator/default.js +0 -0
  95. /package/out/parser/{string/shift → shift}/operator/divisor.d.ts +0 -0
  96. /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
  97. /package/out/parser/{string/shift → shift}/operator/operator.d.ts +0 -0
  98. /package/out/parser/{string/shift → shift}/operator/operator.js +0 -0
@@ -1,8 +1,8 @@
1
1
  import type { BaseRoot, resolvableReferenceIn } from "@ark/schema";
2
2
  import { type ErrorMessage } from "@ark/util";
3
- import type { ArkAmbient } from "../../config.ts";
4
- import type { resolutionToAst } from "../../scope.ts";
5
- import type { inferAstRoot } from "../semantic/infer.ts";
3
+ import type { ArkAmbient } from "../config.ts";
4
+ import type { resolutionToAst } from "../scope.ts";
5
+ import type { inferAstRoot } from "./ast/infer.ts";
6
6
  import type { DynamicState, DynamicStateWithRoot } from "./reduce/dynamic.ts";
7
7
  import type { StringifiablePrefixOperator } from "./reduce/shared.ts";
8
8
  import type { state, StaticState } from "./reduce/static.ts";
@@ -25,5 +25,5 @@ export declare const parseUntilFinalizer: (s: DynamicState) => DynamicStateWithR
25
25
  export type parseUntilFinalizer<s extends StaticState, $, args> = s["finalizer"] extends undefined ? parseUntilFinalizer<next<s, $, args>, $, args> : s;
26
26
  declare const next: (s: DynamicState) => void;
27
27
  type next<s extends StaticState, $, args> = s["root"] extends undefined ? parseOperand<s, $, args> : parseOperator<s, $, args>;
28
- export type extractFinalizedResult<s extends StaticState> = s["finalizer"] extends "" ? s["root"] : s["finalizer"] extends ErrorMessage ? s["finalizer"] : s["finalizer"] extends "=" ? parseDefault<s["root"], s["unscanned"]> : state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>;
28
+ export type extractFinalizedResult<s extends StaticState> = s["finalizer"] extends "" ? s["root"] : s["finalizer"] extends ErrorMessage ? s["finalizer"] : s["finalizer"] extends "?" ? [s["root"], "?"] : s["finalizer"] extends "=" ? parseDefault<s["root"], s["unscanned"]> : state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>;
29
29
  export {};
@@ -9,6 +9,8 @@ export const fullStringParse = (s) => {
9
9
  }
10
10
  if (s.finalizer === "=")
11
11
  result = parseDefault(s);
12
+ else if (s.finalizer === "?")
13
+ result = result.optional();
12
14
  s.scanner.shiftUntilNonWhitespace();
13
15
  if (s.scanner.lookahead) {
14
16
  // throw a parse error if non-whitespace characters made it here without being parsed
@@ -1,18 +1,17 @@
1
1
  import { type BaseParseContext, type BaseRoot, type MetaSchema, type Morph, type Predicate } from "@ark/schema";
2
- import { type array, type BuiltinObjectKind, type conform, type Constructor, type Domain, type ErrorMessage, type show } from "@ark/util";
3
- import type { inferIntersection } from "../intersect.ts";
4
- import type { type } from "../keywords/ark.ts";
5
- import type { Default, distill, inferMorphOut, inferPredicate, Out } from "../keywords/ast.ts";
2
+ import { type anyOrNever, type array, type BuiltinObjectKind, type conform, type Constructor, type Domain, type ErrorMessage, type show } from "@ark/util";
3
+ import type { applyConstraint, Default, distill, inferIntersection, inferMorphOut, inferPredicate, InferredOptional, Optional, Out } from "../keywords/inference.ts";
4
+ import type { type } from "../keywords/keywords.ts";
5
+ import type { InfixOperator, PostfixExpression } from "./ast/infer.ts";
6
6
  import type { inferDefinition, validateDefinition } from "./definition.ts";
7
- import type { InfixOperator, PostfixExpression } from "./semantic/infer.ts";
8
- import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.ts";
9
- import type { BaseCompletions } from "./string/string.ts";
7
+ import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.ts";
8
+ import type { BaseCompletions } from "./string.ts";
10
9
  export declare const parseTuple: (def: array, ctx: BaseParseContext) => BaseRoot;
11
10
  export declare const parseTupleLiteral: (def: array, ctx: BaseParseContext) => BaseRoot;
12
11
  type InfixExpression = readonly [unknown, InfixOperator, ...unknown[]];
13
12
  export type validateTuple<def extends array, $, args> = def extends IndexZeroExpression ? validatePrefixExpression<def, $, args> : def extends PostfixExpression ? validatePostfixExpression<def, $, args> : def extends InfixExpression ? validateInfixExpression<def, $, args> : def extends (readonly ["", ...unknown[]] | readonly [unknown, "", ...unknown[]]) ? readonly [
14
13
  def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator | "..."> : def[0],
15
- def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator | "..." | "?"> : def[1]
14
+ def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator | "..."> : def[1]
16
15
  ] : validateTupleLiteral<def, $, args>;
17
16
  export type validateTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? Readonly<s["validated"]> : never;
18
17
  type inferTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? s["inferred"] : never;
@@ -20,55 +19,75 @@ type SequenceParseState = {
20
19
  unscanned: array;
21
20
  inferred: array;
22
21
  validated: array;
22
+ includesOptional: boolean;
23
23
  };
24
24
  type parseSequence<def extends array, $, args> = parseNextElement<{
25
25
  unscanned: def;
26
26
  inferred: [];
27
27
  validated: [];
28
+ includesOptional: false;
28
29
  }, $, args>;
29
30
  type PreparsedElement = {
30
31
  head: unknown;
31
32
  tail: array;
33
+ inferred: unknown;
32
34
  optional: boolean;
33
35
  spread: boolean;
34
36
  };
35
37
  declare namespace PreparsedElement {
36
38
  type from<result extends PreparsedElement> = result;
37
39
  }
38
- type preparseNextElement<s extends SequenceParseState> = s["unscanned"] extends readonly ["...", infer head, ...infer tail] ? tail extends readonly ["?", ...infer postOptionalTail] ? PreparsedElement.from<{
40
+ type preparseNextElement<s extends SequenceParseState, $, args> = s["unscanned"] extends readonly ["...", infer head, ...infer tail] ? inferDefinition<head, $, args> extends infer t ? [
41
+ t
42
+ ] extends [anyOrNever] ? PreparsedElement.from<{
39
43
  head: head;
40
- tail: postOptionalTail;
44
+ tail: tail;
45
+ inferred: t;
46
+ optional: false;
47
+ spread: true;
48
+ }> : [t] extends [InferredOptional<infer base>] ? PreparsedElement.from<{
49
+ head: head;
50
+ tail: tail;
51
+ inferred: base;
41
52
  optional: true;
42
53
  spread: true;
43
54
  }> : PreparsedElement.from<{
44
55
  head: head;
45
56
  tail: tail;
57
+ inferred: t;
46
58
  optional: false;
47
59
  spread: true;
48
- }> : s["unscanned"] extends readonly [infer head, "?", ...infer tail] ? PreparsedElement.from<{
60
+ }> : never : s["unscanned"] extends readonly [infer head, ...infer tail] ? inferDefinition<head, $, args> extends infer t ? [
61
+ t
62
+ ] extends [anyOrNever] ? PreparsedElement.from<{
49
63
  head: head;
50
64
  tail: tail;
65
+ inferred: t;
66
+ optional: false;
67
+ spread: false;
68
+ }> : [t] extends [InferredOptional<infer base>] ? PreparsedElement.from<{
69
+ head: head;
70
+ tail: tail;
71
+ inferred: base;
51
72
  optional: true;
52
73
  spread: false;
53
- }> : s["unscanned"] extends readonly [infer head, ...infer tail] ? PreparsedElement.from<{
74
+ }> : PreparsedElement.from<{
54
75
  head: head;
55
76
  tail: tail;
77
+ inferred: t;
56
78
  optional: false;
57
79
  spread: false;
58
- }> : null;
59
- type parseNextElement<s extends SequenceParseState, $, args> = preparseNextElement<s> extends infer next extends PreparsedElement ? parseNextElement<{
80
+ }> : never : null;
81
+ type parseNextElement<s extends SequenceParseState, $, args> = preparseNextElement<s, $, args> extends infer next extends PreparsedElement ? parseNextElement<{
60
82
  unscanned: next["tail"];
61
83
  inferred: next["spread"] extends true ? [
62
84
  ...s["inferred"],
63
- ...conform<inferDefinition<next["head"], $, args>, array>
64
- ] : next["optional"] extends true ? [
65
- ...s["inferred"],
66
- inferDefinition<next["head"], $, args>?
67
- ] : [...s["inferred"], inferDefinition<next["head"], $, args>];
85
+ ...conform<next["inferred"], array>
86
+ ] : next["optional"] extends true ? [...s["inferred"], next["inferred"]?] : [...s["inferred"], next["inferred"]];
68
87
  validated: [
69
88
  ...s["validated"],
70
89
  ...(next["spread"] extends true ? [
71
- inferDefinition<next["head"], $, args> extends (infer spreadOperand extends array) ? [
90
+ next["inferred"] extends infer spreadOperand extends array ? [
72
91
  number,
73
92
  number
74
93
  ] extends ([
@@ -76,17 +95,9 @@ type parseNextElement<s extends SequenceParseState, $, args> = preparseNextEleme
76
95
  spreadOperand["length"]
77
96
  ]) ? ErrorMessage<multipleVariadicMessage> : "..." : ErrorMessage<writeNonArraySpreadMessage<next["head"]>>
78
97
  ] : []),
79
- [
80
- next["optional"] | next["spread"],
81
- "?"
82
- ] extends ([
83
- false,
84
- s["validated"][number]
85
- ]) ? ErrorMessage<requiredPostOptionalMessage> : validateDefinition<next["head"], $, args>,
86
- ...(next["optional"] extends true ? [
87
- next["spread"] extends true ? ErrorMessage<spreadOptionalMessage> : number extends s["inferred"]["length"] ? ErrorMessage<optionalPostVariadicMessage> : "?"
88
- ] : [])
98
+ next["optional"] extends true ? next["spread"] extends true ? ErrorMessage<spreadOptionalMessage> : number extends s["inferred"]["length"] ? ErrorMessage<optionalPostVariadicMessage> : validateDefinition<next["head"], $, args> : [s["includesOptional"], next["spread"]] extends [true, false] ? ErrorMessage<requiredPostOptionalMessage> : validateDefinition<next["head"], $, args>
89
99
  ];
100
+ includesOptional: s["includesOptional"] | next["optional"] extends (false) ? false : true;
90
101
  }, $, args> : s;
91
102
  export declare const writeNonArraySpreadMessage: <operand extends string>(operand: operand) => writeNonArraySpreadMessage<operand>;
92
103
  type writeNonArraySpreadMessage<operand> = `Spread element must be an array${operand extends string ? ` (was ${operand})` : ""}`;
@@ -99,9 +110,9 @@ type optionalPostVariadicMessage = typeof optionalPostVariadicMessage;
99
110
  export declare const spreadOptionalMessage = "A spread element cannot be optional";
100
111
  type spreadOptionalMessage = typeof optionalPostVariadicMessage;
101
112
  export type inferTuple<def extends array, $, args> = def extends TupleExpression ? inferTupleExpression<def, $, args> : inferTupleLiteral<def, $, args>;
102
- export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, args>[] : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $, args>, inferDefinition<def[2], $, args>> : def[1] extends "|" ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args> : def[1] extends ":" ? inferPredicate<inferDefinition<def[0], $, args>, def[2]> : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args> : def[1] extends "@" ? inferDefinition<def[0], $, args> : def[1] extends "=" ? (In?: inferDefinition<def[0], $, args>) => Default<def[2]> : def extends readonly ["===", ...infer values] ? values[number] : def extends (readonly ["instanceof", ...infer constructors extends Constructor[]]) ? InstanceType<constructors[number]> : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args> : never;
113
+ export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, args>[] : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $, args>, inferDefinition<def[2], $, args>> : def[1] extends "|" ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args> : def[1] extends ":" ? inferPredicate<inferDefinition<def[0], $, args>, def[2]> : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args> : def[1] extends "@" ? inferDefinition<def[0], $, args> : def[1] extends "=" ? (In?: inferDefinition<def[0], $, args>) => Default<def[2]> : def[1] extends "?" ? applyConstraint<inferDefinition<def[0], $, args>, Optional> : def extends readonly ["===", ...infer values] ? values[number] : def extends (readonly ["instanceof", ...infer constructors extends Constructor[]]) ? InstanceType<constructors[number]> : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args> : never;
103
114
  export type validatePrefixExpression<def extends IndexZeroExpression, $, args> = def["length"] extends 1 ? readonly [writeMissingRightOperandMessage<def[0]>] : def[0] extends "keyof" ? readonly [def[0], validateDefinition<def[1], $, args>] : def[0] extends "===" ? readonly [def[0], ...unknown[]] : def[0] extends "instanceof" ? readonly [def[0], ...Constructor[]] : never;
104
- export type validatePostfixExpression<def extends PostfixExpression, $, args> = conform<def, readonly [validateDefinition<def[0], $, args>, "[]"]>;
115
+ export type validatePostfixExpression<def extends PostfixExpression, $, args> = conform<def, readonly [validateDefinition<def[0], $, args>, "[]" | "?"]>;
105
116
  export type validateInfixExpression<def extends InfixExpression, $, args> = def["length"] extends 2 ? readonly [def[0], writeMissingRightOperandMessage<def[1]>] : readonly [
106
117
  validateDefinition<def[0], $, args>,
107
118
  def[1],
@@ -114,18 +125,18 @@ export type UnparsedTupleExpressionInput = {
114
125
  export type UnparsedTupleOperator = show<keyof UnparsedTupleExpressionInput>;
115
126
  export declare const parseKeyOfTuple: PrefixParser<"keyof">;
116
127
  export type inferKeyOfExpression<operandDef, $, args> = show<keyof inferDefinition<operandDef, $, args>>;
117
- export type PostfixParser<token extends IndexOneOperator> = (def: IndexOneExpression<token>, ctx: BaseParseContext) => BaseRoot;
128
+ export type IndexOneParser<token extends IndexOneOperator> = (def: IndexOneExpression<token>, ctx: BaseParseContext) => BaseRoot;
118
129
  export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpression<token>, ctx: BaseParseContext) => BaseRoot;
119
130
  export type TupleExpression = IndexZeroExpression | IndexOneExpression;
120
131
  export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator;
121
132
  export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator;
122
- export type TuplePostfixOperator = "[]";
133
+ export type TuplePostfixOperator = "[]" | "?";
123
134
  export type TupleInfixOperator = "&" | "|" | "=>" | ":" | "@" | "=";
124
135
  export type IndexOneExpression<token extends IndexOneOperator = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
125
- export declare const parseMorphTuple: PostfixParser<"=>">;
136
+ export declare const parseMorphTuple: IndexOneParser<"=>">;
126
137
  export declare const writeMalformedFunctionalExpressionMessage: (operator: ":" | "=>", value: unknown) => string;
127
138
  export type parseMorph<inDef, morph, $, args> = morph extends Morph ? inferMorphOut<morph> extends infer out ? (In: distill.brandable.In<inferDefinition<inDef, $, args>>) => Out<out> : never : never;
128
- export declare const parseNarrowTuple: PostfixParser<":">;
139
+ export declare const parseNarrowTuple: IndexOneParser<":">;
129
140
  export type IndexZeroOperator = "keyof" | "instanceof" | "===";
130
141
  export type IndexZeroExpression<token extends IndexZeroOperator = IndexZeroOperator> = readonly [token, ...unknown[]];
131
142
  export declare const writeInvalidConstructorMessage: <actual extends Domain | BuiltinObjectKind>(actual: actual) => string;
@@ -1,25 +1,18 @@
1
1
  import { $ark, Disjoint, intersectNodesRoot, makeRootAndArrayPropertiesMutable } from "@ark/schema";
2
2
  import { append, isEmptyObject, objectKindOrDomainOf, throwParseError } from "@ark/util";
3
- import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
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
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,7 +24,7 @@ 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
30
  return ctx.$.parseSchema(sequences.map(sequence => isEmptyObject(sequence) ?
@@ -139,10 +132,12 @@ export const parseNarrowTuple = (def, ctx) => {
139
132
  };
140
133
  const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
141
134
  const parseDefaultTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).default(def[2]);
135
+ const parseOptionalTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).optional();
142
136
  const indexOneParsers = {
137
+ "[]": parseArrayTuple,
138
+ "?": parseOptionalTuple,
143
139
  "|": parseBranchTuple,
144
140
  "&": parseBranchTuple,
145
- "[]": parseArrayTuple,
146
141
  ":": parseNarrowTuple,
147
142
  "=>": parseMorphTuple,
148
143
  "@": parseAttributeTuple,
package/out/scope.d.ts CHANGED
@@ -2,11 +2,11 @@ import { BaseScope, type AliasDefEntry, type ArkScopeConfig, type BaseNode, type
2
2
  import { type ErrorType, type Json, type anyOrNever, type array, type flattenListable, type noSuggest, type nominal } from "@ark/util";
3
3
  import type { ArkSchemaRegistry } from "./config.ts";
4
4
  import { type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseGenericParams, type parseValidGenericParams } from "./generic.ts";
5
- import type { Ark } from "./keywords/ark.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 { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type SchemaParser, type Type, type TypeParser, type UnitTypeParser } from "./type.ts";
9
+ import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser } from "./type.ts";
10
10
  export type ScopeParser = <const def>(def: scope.validate<def>, config?: ArkScopeConfig) => Scope<scope.infer<def>>;
11
11
  export type ModuleParser = <const def>(def: scope.validate<def>, config?: ArkScopeConfig) => scope.infer<def> extends infer $ ? Module<{
12
12
  [k in exportedNameOf<$>]: $[k];
@@ -80,7 +80,7 @@ export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
80
80
  export declare const scope: ScopeParser;
81
81
  export declare namespace scope {
82
82
  type validate<def> = {
83
- [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;
83
+ [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;
84
84
  };
85
85
  type infer<def> = inferBootstrapped<bootstrapAliases<def>>;
86
86
  }
package/out/scope.js CHANGED
@@ -2,10 +2,10 @@ import { $ark, BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
2
2
  import { domainOf, flatMorph, hasDomain, isThunk, throwParseError } from "@ark/util";
3
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 { Scanner } 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 {
package/out/type.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { ArkErrors, BaseRoot, type BaseParseOptions, type MetaSchema, type Morph, type Predicate, type RootSchema } from "@ark/schema";
2
2
  import { Callable, Hkt, type Constructor, type array, type conform } from "@ark/util";
3
3
  import type { Generic, GenericParser, ParameterString, baseGenericConstraints, parseValidGenericParams, validateParameterString } from "./generic.ts";
4
- import type { Ark, ark, type } from "./keywords/ark.ts";
5
- import type { distill } from "./keywords/ast.ts";
4
+ import type { distill } from "./keywords/inference.ts";
5
+ import type { Ark, keywords, type } from "./keywords/keywords.ts";
6
6
  import type { BaseType } from "./methods/base.ts";
7
7
  import type { instantiateType } from "./methods/instantiate.ts";
8
8
  import type { validateDeclared, validateDefinition } from "./parser/definition.ts";
@@ -12,17 +12,18 @@ import type { InternalScope, ModuleParser, Scope, ScopeParser, bindThis } from "
12
12
  export type TypeParserAttachments = Omit<TypeParser, never>;
13
13
  export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
14
14
  <const def, r = Type<type.infer<def, $>, $>>(def: type.validate<def, $>): r;
15
- <params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
15
+ <const params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
16
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] : [type.validate<rest[0], $>] : []): r;
17
- $: Scope<$>;
18
- raw(def: unknown): BaseType<any, $>;
19
17
  errors: typeof ArkErrors;
20
18
  hkt: typeof Hkt;
19
+ keywords: typeof keywords;
20
+ $: Scope<$>;
21
+ raw(def: unknown): BaseType<any, $>;
21
22
  module: ModuleParser;
22
23
  scope: ScopeParser;
24
+ define: DefinitionParser<$>;
23
25
  generic: GenericParser<$>;
24
26
  schema: SchemaParser<$>;
25
- ark: typeof ark;
26
27
  unit: UnitTypeParser<$>;
27
28
  enumerated: EnumeratedTypeParser<$>;
28
29
  }
@@ -34,11 +35,8 @@ export type DeclarationParser<$> = <preinferred>() => {
34
35
  };
35
36
  export type UnitTypeParser<$> = <const t>(value: t) => Type<t, $>;
36
37
  export type EnumeratedTypeParser<$> = <const values extends readonly unknown[]>(...values: values) => Type<values[number], $>;
37
- export type DefinitionParser<$> = <def>(def: type.validate<def, $>) => def;
38
+ export type DefinitionParser<$> = <const def>(def: type.validate<def, $>) => def;
38
39
  export type SchemaParser<$> = (schema: RootSchema, opts?: BaseParseOptions) => Type<unknown, $>;
39
40
  export type Type<t = unknown, $ = {}> = instantiateType<t, $>;
40
- export declare namespace Type {
41
- type Any<t = any> = BaseType<t, any>;
42
- }
43
41
  export type TypeConstructor<t = unknown, $ = {}> = new (def: unknown, $: Scope<$>) => Type<t, $>;
44
42
  export declare const Type: TypeConstructor;
package/out/type.js CHANGED
@@ -9,10 +9,11 @@ export class InternalTypeParser extends Callable {
9
9
  raw: $.parse,
10
10
  module: $.constructor.module,
11
11
  scope: $.constructor.scope,
12
+ define: $.define,
12
13
  generic: $.generic,
13
14
  schema: $.schema,
14
15
  // this won't be defined during bootstrapping, but externally always will be
15
- ark: $.ambient,
16
+ keywords: $.ambient,
16
17
  unit: $.unit,
17
18
  enumerated: $.enumerated
18
19
  },
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.7",
4
+ "version": "2.0.0-rc.9",
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.9.0",
38
- "@ark/schema": "0.9.0"
37
+ "@ark/schema": "0.11.0",
38
+ "@ark/util": "0.11.0"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"
@@ -1,26 +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> = l extends array ? r extends array ? intersectArrays<l, r, MorphableIntersection<piped>> : // for an intersection with exactly one array operand like { name: string } & string[],
21
- l & r : r extends array ? l & r : show<{
22
- [k in keyof l]: k extends keyof r ? _inferIntersection<l[k], r[k], piped> : l[k];
23
- } & {
24
- [k in keyof r]: k extends keyof l ? _inferIntersection<l[k], r[k], piped> : r[k];
25
- }>;
26
- export {};
package/out/intersect.js DELETED
@@ -1 +0,0 @@
1
- export {};
package/out/keys.d.ts DELETED
@@ -1,18 +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> = [
9
- o
10
- ] extends [array] ? (number extends o["length"] ? NonNegativeIntegerLiteral : never) | {
11
- [k in keyof o]-?: k extends `${infer index extends number}` ? index | k : never;
12
- }[keyof o & `${number}`] : {
13
- [k in keyof o]-?: k extends number ? k | `${k}` : k;
14
- }[keyof o];
15
- export type arkGet<o, k extends arkKeyOf<o>> = o[Extract<k extends NonNegativeIntegerLiteral ? number : k, keyof o>];
16
- export type TypeKey = termOrType<Key>;
17
- export type typeKeyToString<k extends TypeKey> = typeToString<k extends type.cast<infer t> ? t : k>;
18
- 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}`;
package/out/keys.js DELETED
@@ -1 +0,0 @@
1
- export {};
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes