arktype 2.0.0-beta.2 → 2.0.0-beta.3

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 (88) hide show
  1. package/out/ark.d.ts +18 -4
  2. package/out/ark.js +22 -3
  3. package/out/ast.d.ts +213 -0
  4. package/out/ast.js +3 -0
  5. package/out/config.d.ts +13 -0
  6. package/out/generic.d.ts +104 -18
  7. package/out/index.d.ts +4 -4
  8. package/out/index.js +1 -1
  9. package/out/intersect.d.ts +28 -0
  10. package/out/intersect.js +1 -0
  11. package/out/keys.d.ts +16 -0
  12. package/out/keys.js +1 -0
  13. package/out/keywords/arkGenerics.d.ts +17 -0
  14. package/out/keywords/arkGenerics.js +16 -0
  15. package/out/keywords/format.d.ts +12 -0
  16. package/out/keywords/format.js +36 -0
  17. package/out/keywords/internal.d.ts +11 -0
  18. package/out/keywords/internal.js +25 -0
  19. package/out/keywords/jsObjects.d.ts +15 -0
  20. package/out/keywords/jsObjects.js +13 -0
  21. package/out/keywords/parsing.d.ts +16 -0
  22. package/out/keywords/parsing.js +77 -0
  23. package/out/keywords/platformObjects.d.ts +13 -0
  24. package/out/keywords/platformObjects.js +12 -0
  25. package/out/keywords/tsGenerics.d.ts +37 -0
  26. package/out/keywords/tsGenerics.js +37 -0
  27. package/out/keywords/tsKeywords.d.ts +18 -0
  28. package/out/keywords/tsKeywords.js +17 -0
  29. package/out/keywords/typedArray.d.ts +16 -0
  30. package/out/keywords/typedArray.js +14 -0
  31. package/out/keywords/utils/creditCard.d.ts +2 -0
  32. package/out/keywords/utils/creditCard.js +25 -0
  33. package/out/keywords/utils/date.d.ts +20 -0
  34. package/out/keywords/utils/date.js +36 -0
  35. package/out/keywords/utils/ip.d.ts +3 -0
  36. package/out/keywords/utils/ip.js +20 -0
  37. package/out/keywords/utils/regex.d.ts +2 -0
  38. package/out/keywords/utils/regex.js +9 -0
  39. package/out/keywords/validation.d.ts +19 -0
  40. package/out/keywords/validation.js +84 -0
  41. package/out/module.d.ts +19 -11
  42. package/out/module.js +1 -0
  43. package/out/parser/definition.d.ts +5 -4
  44. package/out/parser/objectLiteral.d.ts +6 -5
  45. package/out/parser/objectLiteral.js +2 -2
  46. package/out/parser/semantic/bounds.d.ts +2 -1
  47. package/out/parser/semantic/divisor.d.ts +2 -2
  48. package/out/parser/semantic/infer.d.ts +13 -8
  49. package/out/parser/semantic/keyof.d.ts +7 -0
  50. package/out/parser/semantic/keyof.js +1 -0
  51. package/out/parser/semantic/validate.d.ts +4 -5
  52. package/out/parser/semantic/validate.js +0 -1
  53. package/out/parser/string/reduce/dynamic.d.ts +2 -1
  54. package/out/parser/string/reduce/shared.d.ts +1 -1
  55. package/out/parser/string/reduce/static.d.ts +1 -1
  56. package/out/parser/string/shift/operand/date.d.ts +1 -1
  57. package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
  58. package/out/parser/string/shift/operand/unenclosed.d.ts +13 -8
  59. package/out/parser/string/shift/operator/bounds.d.ts +2 -1
  60. package/out/parser/string/shift/operator/bounds.js +5 -5
  61. package/out/parser/string/shift/operator/default.d.ts +2 -1
  62. package/out/parser/string/string.d.ts +2 -1
  63. package/out/parser/tuple.d.ts +4 -2
  64. package/out/parser/tuple.js +6 -6
  65. package/out/scope.d.ts +47 -26
  66. package/out/scope.js +2 -7
  67. package/out/subtypes/array.d.ts +13 -0
  68. package/out/subtypes/array.js +1 -0
  69. package/out/subtypes/base.d.ts +76 -0
  70. package/out/subtypes/base.js +1 -0
  71. package/out/subtypes/date.d.ts +11 -0
  72. package/out/subtypes/date.js +1 -0
  73. package/out/subtypes/instantiate.d.ts +16 -0
  74. package/out/subtypes/instantiate.js +1 -0
  75. package/out/subtypes/morph.d.ts +10 -0
  76. package/out/subtypes/morph.js +1 -0
  77. package/out/subtypes/number.d.ts +12 -0
  78. package/out/subtypes/number.js +1 -0
  79. package/out/subtypes/object.d.ts +27 -0
  80. package/out/subtypes/object.js +1 -0
  81. package/out/subtypes/string.d.ts +13 -0
  82. package/out/subtypes/string.js +1 -0
  83. package/out/subtypes/validator.d.ts +11 -0
  84. package/out/subtypes/validator.js +1 -0
  85. package/out/type.d.ts +17 -71
  86. package/package.json +3 -3
  87. package/out/match.d.ts +0 -71
  88. package/out/match.js +0 -50
@@ -0,0 +1,76 @@
1
+ import type { ArkErrors, BaseMeta, BaseRoot, Disjoint, Morph, UndeclaredKeyBehavior } from "@ark/schema";
2
+ import type { anyOrNever, Callable, ErrorMessage, Json, unset } from "@ark/util";
3
+ import type { distillConstrainableIn, distillConstrainableOut, distillIn, distillOut, inferMorphOut, inferPipes, inferred } from "../ast.js";
4
+ import type { inferIntersection } from "../intersect.js";
5
+ import type { Scope } from "../scope.js";
6
+ import type { inferTypeRoot, validateTypeRoot } from "../type.js";
7
+ import type { ArrayType } from "./array.js";
8
+ import type { instantiateType } from "./instantiate.js";
9
+ /** @ts-ignore cast variance */
10
+ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => distillOut<t> | ArkErrors> {
11
+ t: t;
12
+ tIn: distillConstrainableIn<t>;
13
+ tOut: distillConstrainableOut<t>;
14
+ infer: distillOut<t>;
15
+ inferIn: distillIn<t>;
16
+ [inferred]: t;
17
+ json: Json;
18
+ description: string;
19
+ expression: string;
20
+ internal: BaseRoot;
21
+ $: Scope<$>;
22
+ assert(data: unknown): this["infer"];
23
+ allows(data: unknown): data is this["inferIn"];
24
+ traverse(data: unknown): distillOut<t> | ArkErrors;
25
+ configure(configOrDescription: BaseMeta | string): this;
26
+ describe(description: string): this;
27
+ onUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
28
+ onDeepUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
29
+ from(literal: this["inferIn"]): this["infer"];
30
+ as<t = unset>(...args: validateChainedAsArgs<t>): instantiateType<t, $>;
31
+ get in(): instantiateType<this["tIn"], $>;
32
+ get out(): instantiateType<this["tOut"], $>;
33
+ intersect<const def, r = inferTypeRoot<def, $>>(def: validateTypeRoot<def, $>): instantiateType<inferIntersection<t, r>, $> | Disjoint;
34
+ and<const def, r = inferTypeRoot<def, $>>(def: validateTypeRoot<def, $>): instantiateType<inferIntersection<t, r>, $>;
35
+ or<const def, r = inferTypeRoot<def, $>>(def: validateTypeRoot<def, $>): instantiateType<t | r, $>;
36
+ array(): ArrayType<t[], $>;
37
+ pipe<a extends Morph<this["infer"]>, r = instantiateType<inferPipes<t, [a]>, $>>(a: a): r;
38
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, r = instantiateType<inferPipes<t, [a, b]>, $>>(a: a, b: b): r;
39
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, r = instantiateType<inferPipes<t, [a, b, c]>, $>>(a: a, b: b, c: c): r;
40
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, r = instantiateType<inferPipes<t, [a, b, c, d]>, $>>(a: a, b: b, c: c, d: d): r;
41
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, r = instantiateType<inferPipes<t, [a, b, c, d, e]>, $>>(a: a, b: b, c: c, d: d, e: e): r;
42
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f): r;
43
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, g extends Morph<inferMorphOut<f>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f, g]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g): r;
44
+ equals<def>(def: validateTypeRoot<def, $>): this is instantiateType<inferTypeRoot<def, $>, $>;
45
+ extract<def>(r: validateTypeRoot<def, $>): instantiateType<Extract<t, inferTypeRoot<def, $>>, $>;
46
+ exclude<def>(r: validateTypeRoot<def, $>): instantiateType<Exclude<t, inferTypeRoot<def, $>>, $>;
47
+ extends<def>(other: validateTypeRoot<def, $>): this is instantiateType<inferTypeRoot<def, $>, $>;
48
+ overlaps<def>(r: validateTypeRoot<def, $>): boolean;
49
+ /** @deprecated */
50
+ apply: Function["apply"];
51
+ /** @deprecated */
52
+ bind: Function["bind"];
53
+ /** @deprecated */
54
+ call: Function["call"];
55
+ /** @deprecated */
56
+ caller: Function;
57
+ /** @deprecated */
58
+ length: number;
59
+ /** @deprecated */
60
+ name: string;
61
+ /** @deprecated */
62
+ prototype: Function["prototype"];
63
+ /** @deprecated */
64
+ arguments: Function["arguments"];
65
+ /** @deprecated */
66
+ Symbol: never;
67
+ }
68
+ type validateChainedAsArgs<t> = [
69
+ t
70
+ ] extends [unset] ? [
71
+ t
72
+ ] extends [anyOrNever] ? [
73
+ ] : [
74
+ ErrorMessage<"as requires an explicit type parameter like myType.as<t>()">
75
+ ] : [];
76
+ export type { Type as BaseType };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import type { ExclusiveDateRangeSchema, exclusivizeRangeSchema, InclusiveDateRangeSchema } from "@ark/schema";
2
+ import type { applyConstraint } from "../ast.js";
3
+ import type { ObjectType } from "./object.js";
4
+ /** @ts-ignore cast variance */
5
+ interface Type<out t extends globalThis.Date = globalThis.Date, $ = {}> extends ObjectType<t, $> {
6
+ atOrAfter<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<applyConstraint<t, "after", schema>, $>;
7
+ atOrBefore<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<applyConstraint<t, "before", schema>, $>;
8
+ laterThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<applyConstraint<t, "after", exclusivizeRangeSchema<schema>>, $>;
9
+ earlierThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<applyConstraint<t, "before", exclusivizeRangeSchema<schema>>, $>;
10
+ }
11
+ export type { Type as DateType };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import type { anyOrNever, array } from "@ark/util";
2
+ import type { MorphAst } from "../ast.js";
3
+ import type { ArrayType } from "./array.js";
4
+ import type { DateType } from "./date.js";
5
+ import type { MorphType } from "./morph.js";
6
+ import type { NumberType } from "./number.js";
7
+ import type { ObjectType } from "./object.js";
8
+ import type { StringType } from "./string.js";
9
+ import type { ValidatorType } from "./validator.js";
10
+ export type instantiateType<t, $> = [
11
+ Extract<t, MorphAst>
12
+ ] extends [anyOrNever] ? [
13
+ t
14
+ ] extends [string] ? StringType<t, $> : [t] extends [number] ? NumberType<t, $> : [t] extends [object] ? [
15
+ t
16
+ ] extends [array] ? ArrayType<t, $> : [t] extends [Date] ? DateType<t, $> : ObjectType<t, $> : ValidatorType<t, $> : MorphType<t, $>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { Predicate, PredicateCast } from "@ark/schema";
2
+ import type { applyConstraint, Out } from "../ast.js";
3
+ import type { BaseType } from "./base.js";
4
+ /** @ts-ignore cast variance */
5
+ interface Type<out t = unknown, $ = {}> extends BaseType<t, $> {
6
+ narrow<narrowed extends this["infer"] = never>(predicate: Predicate<this["infer"]> | PredicateCast<this["infer"], narrowed>): Type<(In: this["tIn"]) => Out<[
7
+ narrowed
8
+ ] extends [never] ? applyConstraint<this["tOut"], "predicate", Predicate> : narrowed>, $>;
9
+ }
10
+ export type { Type as MorphType };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ import type { DivisorSchema, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema } from "@ark/schema";
2
+ import type { applyConstraint } from "../ast.js";
3
+ import type { ValidatorType } from "./validator.js";
4
+ /** @ts-ignore cast variance */
5
+ interface Type<out t extends number = number, $ = {}> extends ValidatorType<t, $> {
6
+ divisibleBy<const schema extends DivisorSchema>(schema: schema): Type<applyConstraint<t, "divisor", schema>, $>;
7
+ atLeast<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "min", schema>, $>;
8
+ atMost<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "max", schema>, $>;
9
+ moreThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "min", exclusivizeRangeSchema<schema>>, $>;
10
+ lessThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "max", exclusivizeRangeSchema<schema>>, $>;
11
+ }
12
+ export type { Type as NumberType };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import type { ErrorType, merge } from "@ark/util";
2
+ import type { type } from "../ark.js";
3
+ import type { arkKeyOf, getArkKey, toArkKey } from "../keys.js";
4
+ import type { inferTypeRoot, validateTypeRoot } from "../type.js";
5
+ import type { instantiateType } from "./instantiate.js";
6
+ import type { ValidatorType } from "./validator.js";
7
+ /** @ts-ignore cast variance */
8
+ interface Type<out t extends object = object, $ = {}> extends ValidatorType<t, $> {
9
+ keyof(): instantiateType<keyof t, $>;
10
+ get<k1 extends arkKeyOf<t>, r = instantiateType<getArkKey<t, k1>, $>>(k1: k1 | type.cast<k1>): r;
11
+ get<k1 extends arkKeyOf<t>, k2 extends arkKeyOf<getArkKey<t, k1>>, r = instantiateType<getArkKey<getArkKey<t, k1>, k2>, $>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>): r;
12
+ get<k1 extends arkKeyOf<t>, k2 extends arkKeyOf<getArkKey<t, k1>>, k3 extends arkKeyOf<getArkKey<getArkKey<t, k1>, k2>>, r = instantiateType<getArkKey<getArkKey<getArkKey<t, k1>, k2>, k3>, $>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>, k3: k3 | type.cast<k3>): r;
13
+ pick<const key extends arkKeyOf<t> = never>(...keys: (key | type.cast<key>)[]): Type<{
14
+ [k in keyof t as Extract<toArkKey<t, k>, key>]: t[k];
15
+ }, $>;
16
+ omit<const key extends arkKeyOf<t> = never>(...keys: (key | type.cast<key>)[]): Type<{
17
+ [k in keyof t as Exclude<toArkKey<t, k>, key>]: t[k];
18
+ }, $>;
19
+ merge<const def, r = inferTypeRoot<def, $>>(def: validateTypeRoot<def, $> & (r extends object ? unknown : ErrorType<"Merged type must be an object", [actual: r]>)): Type<merge<t, r & object>, $>;
20
+ required(): Type<{
21
+ [k in keyof t]-?: t[k];
22
+ }, $>;
23
+ partial(): Type<{
24
+ [k in keyof t]?: t[k];
25
+ }, $>;
26
+ }
27
+ export type { Type as ObjectType };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { ExactLengthSchema, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema, PatternSchema } from "@ark/schema";
2
+ import type { applyConstraint } from "../ast.js";
3
+ import type { ValidatorType } from "./validator.js";
4
+ /** @ts-ignore cast variance */
5
+ interface Type<out t extends string = string, $ = {}> extends ValidatorType<t, $> {
6
+ matching<const schema extends PatternSchema>(schema: schema): Type<applyConstraint<t, "pattern", schema>, $>;
7
+ atLeastLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "minLength", schema>, $>;
8
+ atMostLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "maxLength", schema>, $>;
9
+ moreThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
10
+ lessThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
11
+ exactlyLength<const schema extends ExactLengthSchema>(schema: schema): Type<applyConstraint<t, "exactLength", schema>, $>;
12
+ }
13
+ export type { Type as StringType };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import type { Predicate, PredicateCast } from "@ark/schema";
2
+ import type { applyConstraint } from "../ast.js";
3
+ import type { BaseType } from "./base.js";
4
+ import type { instantiateType } from "./instantiate.js";
5
+ /** @ts-ignore cast variance */
6
+ interface Type<out t = unknown, $ = {}> extends BaseType<t, $> {
7
+ narrow<narrowed extends this["infer"] = never>(predicate: Predicate<this["infer"]> | PredicateCast<this["infer"], narrowed>): instantiateType<[
8
+ narrowed
9
+ ] extends [never] ? applyConstraint<t, "predicate", Predicate> : narrowed, $>;
10
+ }
11
+ export type { Type as ValidatorType };
@@ -0,0 +1 @@
1
+ export {};
package/out/type.d.ts CHANGED
@@ -1,89 +1,35 @@
1
- import { ArkErrors, BaseRoot, type BaseMeta, type Disjoint, type DivisorSchema, type ExactLengthSchema, type ExclusiveDateRangeSchema, type ExclusiveNumericRangeSchema, type InclusiveDateRangeSchema, type InclusiveNumericRangeSchema, type Morph, type MorphAst, type NodeSchema, type Out, type PatternSchema, type Predicate, type PrimitiveConstraintKind, type Root, type arkKeyOf, type constrain, type constraintKindOf, type distillIn, type distillOut, type exclusivizeRangeSchema, type getArkKey, type inferIntersection, type inferMorphOut, type inferPipes, type inferPredicate, type toArkKey, type validateChainedAsArgs, type validateChainedConstraint, type validateStructuralOperand } from "@ark/schema";
2
- import { Callable, type Constructor, type array, type conform, type unset } from "@ark/util";
3
- import type { type } from "./ark.js";
1
+ import { ArkErrors, BaseRoot, type BaseMeta, type Morph, type Predicate } from "@ark/schema";
2
+ import { Callable, type Constructor, type array, type conform } from "@ark/util";
3
+ import type { distillIn, distillOut } from "./ast.js";
4
4
  import { type Generic, type ParameterString, type baseGenericConstraints, type parseValidGenericParams, type validateParameterString } from "./generic.js";
5
5
  import type { inferDefinition, validateDeclared, validateDefinition } from "./parser/definition.js";
6
6
  import type { IndexOneOperator, IndexZeroOperator, TupleInfixOperator } from "./parser/tuple.js";
7
7
  import type { InternalScope, Scope, bindThis } from "./scope.js";
8
+ import type { BaseType } from "./subtypes/base.js";
9
+ import type { instantiateType } from "./subtypes/instantiate.js";
8
10
  /** The convenience properties attached to `type` */
9
11
  export type TypeParserAttachments = Omit<TypeParser, never>;
10
12
  export interface TypeParser<$ = {}> {
11
- <const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>;
12
- <const zero, const one, const rest extends array>(_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 "@" ? [string | BaseMeta] : [validateTypeRoot<rest[0], $>] : []): Type<inferTypeRoot<[zero, one, ...rest], $>, $>;
13
+ <const def, r = Type<inferTypeRoot<def, $>, $>>(def: validateTypeRoot<def, $>): r;
13
14
  <params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
14
- raw(def: unknown): Type<any, $>;
15
+ <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 "@" ? [string | BaseMeta] : [validateTypeRoot<rest[0], $>] : []): r;
16
+ raw(def: unknown): BaseType<any, $>;
15
17
  errors: typeof ArkErrors;
16
18
  }
17
19
  export declare class InternalTypeParser extends Callable<(...args: unknown[]) => BaseRoot | Generic, TypeParserAttachments> {
18
20
  constructor($: InternalScope);
19
21
  }
20
22
  export type DeclarationParser<$> = <preinferred>() => {
21
- type: <def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
23
+ type: <const def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
22
24
  };
23
- declare class _Type<t = unknown, $ = any> extends Root<t, $> {
24
- $: Scope<$>;
25
- as<t = unset>(...args: validateChainedAsArgs<t>): Type<t, $>;
26
- get in(): Type<this["tIn"], $>;
27
- get out(): Type<this["tOut"], $>;
28
- intersect<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint;
29
- and<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>, $>;
30
- or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $>;
31
- array(): Type<t[], $>;
32
- keyof(): Type<keyof this["inferIn"], $>;
33
- pipe<a extends Morph<this["infer"]>>(a: a): Type<inferPipes<t, [a]>, $>;
34
- pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>>(a: a, b: b): Type<inferPipes<t, [a, b]>, $>;
35
- pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>>(a: a, b: b, c: c): Type<inferPipes<t, [a, b, c]>, $>;
36
- pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>>(a: a, b: b, c: c, d: d): Type<inferPipes<t, [a, b, c, d]>, $>;
37
- pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>>(a: a, b: b, c: c, d: d, e: e): Type<inferPipes<t, [a, b, c, d, e]>, $>;
38
- pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>>(a: a, b: b, c: c, d: d, e: e, f: f): Type<inferPipes<t, [a, b, c, d, e, f]>, $>;
39
- pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, g extends Morph<inferMorphOut<f>>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g): Type<inferPipes<t, [a, b, c, d, e, f, g]>, $>;
40
- narrow<predicate extends Predicate<distillOut<t>>>(predicate: predicate): Type<t extends MorphAst ? inferPredicate<this["tOut"], predicate> extends infer narrowed ? (In: this["tIn"]) => Out<narrowed> : never : inferPredicate<t, predicate>, $>;
41
- equals<def>(def: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
42
- extract<def>(r: validateTypeRoot<def, $>): Type<Extract<t, inferTypeRoot<def, $>>, $>;
43
- exclude<def>(r: validateTypeRoot<def, $>): Type<Exclude<t, inferTypeRoot<def, $>>, $>;
44
- extends<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
45
- overlaps<def>(r: validateTypeRoot<def, $>): boolean;
46
- omit<const key extends arkKeyOf<t> = never>(this: validateStructuralOperand<"omit", this>, ...keys: array<key | type.cast<key>>): Type<{
47
- [k in keyof t as Exclude<toArkKey<t, k>, key>]: t[k];
48
- }, $>;
49
- pick<const key extends arkKeyOf<t> = never>(this: validateStructuralOperand<"pick", this>, ...keys: array<key | type.cast<key>>): Type<{
50
- [k in keyof t as Extract<toArkKey<t, k>, key>]: t[k];
51
- }, $>;
52
- required(this: validateStructuralOperand<"required", this>): Type<{
53
- [k in keyof this["inferIn"]]-?: this["inferIn"][k];
54
- }, $>;
55
- partial(this: validateStructuralOperand<"partial", this>): Type<{
56
- [k in keyof this["inferIn"]]?: this["inferIn"][k];
57
- }, $>;
58
- get<k1 extends arkKeyOf<t>>(k1: k1 | type.cast<k1>): Type<getArkKey<t, k1>, $>;
59
- get<k1 extends arkKeyOf<t>, k2 extends arkKeyOf<getArkKey<t, k1>>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>): Type<getArkKey<getArkKey<t, k1>, k2>, $>;
60
- get<k1 extends arkKeyOf<t>, k2 extends arkKeyOf<getArkKey<t, k1>>, k3 extends arkKeyOf<getArkKey<getArkKey<t, k1>, k2>>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>, k3: k3 | type.cast<k3>): Type<getArkKey<getArkKey<getArkKey<t, k1>, k2>, k3>, $>;
61
- constrain<kind extends PrimitiveConstraintKind, const def extends NodeSchema<kind>>(kind: conform<kind, constraintKindOf<this["inferIn"]>>, def: def): Type<constrain<t, kind, def>, $>;
62
- satisfying<predicate extends Predicate<distillIn<t>>>(predicate: predicate): Type<t extends MorphAst ? (In: inferPredicate<this["tIn"], predicate>) => Out<this["tOut"]> : inferPredicate<t, predicate>, $>;
63
- divisibleBy<const schema extends DivisorSchema>(this: validateChainedConstraint<"divisor", this>, schema: schema): Type<constrain<t, "divisor", schema>, $>;
64
- matching<const schema extends PatternSchema>(this: validateChainedConstraint<"pattern", this>, schema: schema): Type<constrain<t, "pattern", schema>, $>;
65
- atLeast<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"min", this>, schema: schema): Type<constrain<t, "min", schema>, $>;
66
- atMost<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"max", this>, schema: schema): Type<constrain<t, "max", schema>, $>;
67
- moreThan<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"min", this>, schema: schema): Type<constrain<t, "min", exclusivizeRangeSchema<schema>>, $>;
68
- lessThan<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"max", this>, schema: schema): Type<constrain<t, "max", exclusivizeRangeSchema<schema>>, $>;
69
- atLeastLength<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"minLength", this>, schema: schema): Type<constrain<t, "minLength", schema>, $>;
70
- atMostLength<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"maxLength", this>, schema: schema): Type<constrain<t, "maxLength", schema>, $>;
71
- moreThanLength<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"minLength", this>, schema: schema): Type<constrain<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
72
- lessThanLength<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"maxLength", this>, schema: schema): Type<constrain<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
73
- exactlyLength<const schema extends ExactLengthSchema>(this: validateChainedConstraint<"exactLength", this>, schema: schema): Type<constrain<t, "exactLength", schema>, $>;
74
- atOrAfter<const schema extends InclusiveDateRangeSchema>(this: validateChainedConstraint<"after", this>, schema: schema): Type<constrain<t, "after", schema>, $>;
75
- atOrBefore<const schema extends InclusiveDateRangeSchema>(this: validateChainedConstraint<"before", this>, schema: schema): Type<constrain<t, "before", schema>, $>;
76
- laterThan<const schema extends ExclusiveDateRangeSchema>(this: validateChainedConstraint<"after", this>, schema: schema): Type<constrain<t, "after", exclusivizeRangeSchema<schema>>, $>;
77
- earlierThan<const schema extends ExclusiveDateRangeSchema>(this: validateChainedConstraint<"before", this>, schema: schema): Type<constrain<t, "before", exclusivizeRangeSchema<schema>>, $>;
78
- }
79
- export interface Type<
80
- /** @ts-expect-error allow instantiation assignment to the base type */
81
- out t = unknown, $ = {}> extends _Type<t, $> {
25
+ export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
26
+ export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $, bindThis<def>>;
27
+ export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>;
28
+ export type validateAmbient<def> = validateTypeRoot<def, {}>;
29
+ export type inferAmbient<def> = inferTypeRoot<def, {}>;
30
+ export type Type<t = unknown, $ = {}> = instantiateType<t, $>;
31
+ export declare namespace Type {
32
+ type Any<t = any> = BaseType<t, any>;
82
33
  }
83
34
  export type TypeConstructor<t = unknown, $ = {}> = new (def: unknown, $: Scope<$>) => Type<t, $>;
84
- export type AnyType<out t = unknown> = Type<t, any>;
85
35
  export declare const Type: TypeConstructor;
86
- export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
87
- export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $, bindThis<def>>;
88
- export type inferTypeRoot<def, $ = {}> = inferDefinition<def, $, bindThis<def>>;
89
- export {};
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-beta.2",
4
+ "version": "2.0.0-beta.3",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "David Blass",
@@ -25,8 +25,8 @@
25
25
  "out"
26
26
  ],
27
27
  "dependencies": {
28
- "@ark/util": "0.1.2",
29
- "@ark/schema": "0.2.2"
28
+ "@ark/util": "0.2.0",
29
+ "@ark/schema": "0.3.0"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"
package/out/match.d.ts DELETED
@@ -1,71 +0,0 @@
1
- import type { Narrowed, distillOut, inferIntersection } from "@ark/schema";
2
- import type { ErrorMessage, isDisjoint, numericStringKeyOf, override, propValueOf, unionToTuple } from "@ark/util";
3
- import type { Scope } from "./scope.js";
4
- import type { inferTypeRoot, validateTypeRoot } from "./type.js";
5
- type MatchParserContext = {
6
- thens: readonly ((In: unknown) => unknown)[];
7
- $: unknown;
8
- exhaustiveOver: unknown;
9
- };
10
- export type MatchParser<$> = CaseMatchParser<{
11
- thens: [];
12
- exhaustiveOver: unknown;
13
- $: $;
14
- }> & {
15
- (): ChainableMatchParser<{
16
- thens: [];
17
- exhaustiveOver: unknown;
18
- $: $;
19
- }>;
20
- only: <inputs>() => ChainableMatchParser<{
21
- thens: [];
22
- exhaustiveOver: inputs;
23
- $: $;
24
- }>;
25
- };
26
- type matcherInputs<ctx extends MatchParserContext> = Parameters<ctx["thens"][number]>[0];
27
- type getHandledBranches<ctx extends MatchParserContext> = Exclude<matcherInputs<ctx>, Narrowed>;
28
- type getUnhandledBranches<ctx extends MatchParserContext> = distillOut<Exclude<ctx["exhaustiveOver"], getHandledBranches<ctx>>>;
29
- type addBranches<ctx extends MatchParserContext, branches extends unknown[]> = override<ctx, {
30
- thens: [...ctx["thens"], ...branches];
31
- }>;
32
- type validateWhenDefinition<def, ctx extends MatchParserContext> = def extends validateTypeRoot<def, ctx["$"]> ? inferMatchBranch<def, ctx> extends getHandledBranches<ctx> ? ErrorMessage<"This branch is redundant and will never be reached"> : def : validateTypeRoot<def, ctx["$"]>;
33
- type inferMatchBranch<def, ctx extends MatchParserContext> = distillOut<inferIntersection<getUnhandledBranches<ctx>, inferTypeRoot<def, ctx["$"]>>>;
34
- export type ChainableMatchParser<ctx extends MatchParserContext> = {
35
- when: <def, ret>(when: validateWhenDefinition<def, ctx>, then: (In: inferMatchBranch<def, ctx>) => ret) => ChainableMatchParser<addBranches<ctx, [(In: inferMatchBranch<def, ctx>) => ret]>>;
36
- cases: CaseMatchParser<ctx>;
37
- orThrow: () => finalizeMatchParser<addBranches<ctx, [(In: getHandledBranches<ctx>) => never]>>;
38
- default: MatchParserDefaultInvocation<ctx>;
39
- finalize: (this: getUnhandledBranches<ctx> extends never ? ChainableMatchParser<ctx> : ErrorMessage<"Cannot manually finalize a non-exhaustive matcher: consider adding a `.default` case, using one of the `.orX` methods, or using `match.only<T>`">) => finalizeMatchParser<ctx>;
40
- };
41
- type MatchParserDefaultInvocation<ctx extends MatchParserContext> = {
42
- <f extends (In: getUnhandledBranches<ctx>) => unknown>(f: f): finalizeWithDefault<ctx, ReturnType<f>>;
43
- <const value>(value: value): finalizeWithDefault<ctx, value>;
44
- };
45
- type validateCases<cases, ctx extends MatchParserContext> = {
46
- [def in keyof cases | keyof ctx["$"] | "default"]?: def extends "default" ? (In: getUnhandledBranches<ctx>) => unknown : def extends validateWhenDefinition<def, ctx> ? (In: inferMatchBranch<def, ctx>) => unknown : validateWhenDefinition<def, ctx>;
47
- };
48
- type errorCases<cases, ctx extends MatchParserContext> = {
49
- [def in keyof cases]?: def extends "default" ? (In: getUnhandledBranches<ctx>) => unknown : def extends validateWhenDefinition<def, ctx> ? (In: inferMatchBranch<def, ctx>) => unknown : validateWhenDefinition<def, ctx>;
50
- } & {
51
- [k in Exclude<keyof ctx["$"], keyof cases>]?: (In: distillOut<inferIntersection<getUnhandledBranches<ctx>, ctx["$"][k]>>) => unknown;
52
- } & {
53
- default?: (In: getUnhandledBranches<ctx>) => unknown;
54
- };
55
- export type CaseMatchParser<ctx extends MatchParserContext> = <cases>(def: cases extends validateCases<cases, ctx> ? cases : errorCases<cases, ctx>) => cases extends {
56
- default: (...args: never[]) => infer defaultReturn;
57
- } ? finalizeWithDefault<addBranches<ctx, unionToTuple<cases[Exclude<keyof cases, "default">]>>, defaultReturn> : ChainableMatchParser<addBranches<ctx, unionToTuple<propValueOf<cases>>>>;
58
- type finalizeWithDefault<ctx extends MatchParserContext, defaultReturn> = finalizeMatchParser<addBranches<ctx, [(_: getUnhandledBranches<ctx>) => defaultReturn]>>;
59
- type finalizeMatchParser<ctx extends MatchParserContext> = MatchInvocation<{
60
- thens: ctx["thens"];
61
- initialInputs: ctx["exhaustiveOver"];
62
- }>;
63
- type MatchInvocationContext = {
64
- thens: readonly ((...args: never[]) => unknown)[];
65
- initialInputs: unknown;
66
- };
67
- export type MatchInvocation<ctx extends MatchInvocationContext> = <const data extends ctx["initialInputs"]>(data: data) => {
68
- [i in numericStringKeyOf<ctx["thens"]>]: isDisjoint<data, Parameters<ctx["thens"][i]>[0]> extends true ? never : ReturnType<ctx["thens"][i]>;
69
- }[numericStringKeyOf<ctx["thens"]>];
70
- export declare const createMatchParser: <$>($: Scope) => MatchParser<$>;
71
- export {};
package/out/match.js DELETED
@@ -1,50 +0,0 @@
1
- export const createMatchParser = ($) => (() => { }).bind($);
2
- // const matchParser = (isRestricted: boolean) => {
3
- // const handledCases: { when: RawRoot; then: Morph }[] = []
4
- // let defaultCase: ((x: unknown) => unknown) | null = null
5
- // const parser = {
6
- // when: (when: unknown, then: Morph) => {
7
- // handledCases.push({ when: $.parseRoot(when, {}), then })
8
- // return parser
9
- // },
10
- // finalize: () => {
11
- // // TODO: exhaustiveness checking
12
- // const branches = handledCases.flatMap(({ when, then }) => {
13
- // if (when.kind === "union") {
14
- // return when.branches.map((branch) => ({
15
- // in: branch,
16
- // morph: then
17
- // }))
18
- // }
19
- // if (when.kind === "morph") {
20
- // return [{ in: when, morph: [when.morph, then] }]
21
- // }
22
- // return [{ in: when, morph: then }]
23
- // })
24
- // if (defaultCase) {
25
- // branches.push({ in: keywordNodes.unknown, morph: defaultCase })
26
- // }
27
- // const matchers = $.node("union", {
28
- // branches,
29
- // ordered: true
30
- // })
31
- // return matchers.assert
32
- // },
33
- // orThrow: () => {
34
- // // implicitly finalize, we don't need to do anything else because we throw either way
35
- // return parser.finalize()
36
- // },
37
- // default: (x: unknown) => {
38
- // if (x instanceof Function) {
39
- // defaultCase = x as never
40
- // } else {
41
- // defaultCase = () => x
42
- // }
43
- // return parser.finalize()
44
- // }
45
- // }
46
- // return parser
47
- // }
48
- // return Object.assign(() => matchParser(false), {
49
- // only: () => matchParser(true)
50
- // }) as never