arktype 2.0.0-dev.4 → 2.0.0-dev.6
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.
- package/__tests__/array.test.ts +176 -236
- package/__tests__/badDefinitionType.test.ts +10 -3
- package/__tests__/basis.test.ts +6 -2
- package/__tests__/bounds.test.ts +282 -352
- package/__tests__/cast.test.ts +7 -2
- package/__tests__/completions.test.ts +8 -3
- package/__tests__/config.test.ts +16 -8
- package/__tests__/cyclic.test.ts +141 -137
- package/__tests__/dateLiteral.test.ts +8 -4
- package/__tests__/declared.test.ts +2 -2
- package/__tests__/discrimination.test.ts +1 -1
- package/__tests__/divisor.test.ts +45 -25
- package/__tests__/enclosed.test.ts +39 -25
- package/__tests__/examples.test.ts +56 -56
- package/__tests__/expressions.test.ts +184 -112
- package/__tests__/generic.test.ts +307 -288
- package/__tests__/group.test.ts +67 -54
- package/__tests__/imports.test.ts +44 -37
- package/__tests__/instanceof.test.ts +17 -15
- package/__tests__/intersection.test.ts +50 -46
- package/__tests__/keyTraversal.test.ts +6 -3
- package/__tests__/keyof.test.ts +68 -52
- package/__tests__/keywords.test.ts +149 -136
- package/__tests__/literal.test.ts +38 -15
- package/__tests__/match.test.ts +234 -224
- package/__tests__/morph.test.ts +300 -269
- package/__tests__/narrow.test.ts +46 -42
- package/__tests__/objectLiteral.test.ts +228 -210
- package/__tests__/realWorld.test.ts +63 -0
- package/__tests__/regex.test.ts +17 -7
- package/__tests__/scope.bench.ts +1 -1
- package/__tests__/scope.test.ts +65 -18
- package/__tests__/serialization.test.ts +19 -0
- package/__tests__/string.test.ts +11 -7
- package/__tests__/submodule.test.ts +26 -39
- package/__tests__/this.test.ts +83 -57
- package/__tests__/thunk.test.ts +13 -7
- package/__tests__/traverse.test.ts +50 -37
- package/__tests__/tsGenerics.test.ts +12 -12
- package/__tests__/type.test.ts +38 -0
- package/__tests__/typeReference.test.ts +7 -7
- package/__tests__/unenclosed.test.ts +116 -100
- package/__tests__/union.test.ts +171 -104
- package/ark.ts +18 -65
- package/generic.ts +67 -0
- package/main.ts +6 -18
- package/match.ts +53 -67
- package/module.ts +15 -0
- package/out/ark.d.ts +12 -36
- package/out/ark.d.ts.map +1 -1
- package/out/ark.js +9 -17
- package/out/ark.js.map +1 -1
- package/out/base.d.ts +205 -0
- package/out/base.d.ts.map +1 -0
- package/out/base.js +320 -0
- package/out/base.js.map +1 -0
- package/out/builtins/builtins.d.ts +23 -0
- package/out/builtins/builtins.d.ts.map +1 -0
- package/out/builtins/builtins.js +15 -0
- package/out/builtins/builtins.js.map +1 -0
- package/out/builtins/jsObjects.d.ts +19 -0
- package/out/builtins/jsObjects.d.ts.map +1 -0
- package/out/builtins/jsObjects.js +18 -0
- package/out/builtins/jsObjects.js.map +1 -0
- package/out/builtins/parsing.d.ts +16 -0
- package/out/builtins/parsing.d.ts.map +1 -0
- package/out/builtins/parsing.js +62 -0
- package/out/builtins/parsing.js.map +1 -0
- package/out/builtins/tsGenerics.d.ts +2 -0
- package/out/builtins/tsGenerics.d.ts.map +1 -0
- package/out/builtins/tsGenerics.js +26 -0
- package/out/builtins/tsGenerics.js.map +1 -0
- package/out/builtins/tsKeywords.d.ts +23 -0
- package/out/builtins/tsKeywords.d.ts.map +1 -0
- package/out/builtins/tsKeywords.js +23 -0
- package/out/builtins/tsKeywords.js.map +1 -0
- package/out/builtins/utils/creditCard.d.ts +3 -0
- package/out/builtins/utils/creditCard.d.ts.map +1 -0
- package/out/builtins/utils/creditCard.js +35 -0
- package/out/builtins/utils/creditCard.js.map +1 -0
- package/out/builtins/utils/date.d.ts +41 -0
- package/out/builtins/utils/date.d.ts.map +1 -0
- package/out/builtins/utils/date.js +56 -0
- package/out/builtins/utils/date.js.map +1 -0
- package/out/builtins/validation.d.ts +19 -0
- package/out/builtins/validation.d.ts.map +1 -0
- package/out/builtins/validation.js +74 -0
- package/out/builtins/validation.js.map +1 -0
- package/out/config.d.ts +9 -0
- package/out/config.d.ts.map +1 -0
- package/out/config.js +18 -0
- package/out/config.js.map +1 -0
- package/out/constraints/ast.d.ts +287 -0
- package/out/constraints/ast.d.ts.map +1 -0
- package/out/constraints/ast.js +2 -0
- package/out/constraints/ast.js.map +1 -0
- package/out/constraints/constraint.d.ts +200 -0
- package/out/constraints/constraint.d.ts.map +1 -0
- package/out/constraints/constraint.js +25 -0
- package/out/constraints/constraint.js.map +1 -0
- package/out/constraints/predicate.d.ts +64 -0
- package/out/constraints/predicate.d.ts.map +1 -0
- package/out/constraints/predicate.js +52 -0
- package/out/constraints/predicate.js.map +1 -0
- package/out/constraints/props/index.d.ts +42 -0
- package/out/constraints/props/index.d.ts.map +1 -0
- package/out/constraints/props/index.js +56 -0
- package/out/constraints/props/index.js.map +1 -0
- package/out/constraints/props/optional.d.ts +37 -0
- package/out/constraints/props/optional.d.ts.map +1 -0
- package/out/constraints/props/optional.js +56 -0
- package/out/constraints/props/optional.js.map +1 -0
- package/out/constraints/props/prop.d.ts +49 -0
- package/out/constraints/props/prop.d.ts.map +1 -0
- package/out/constraints/props/prop.js +106 -0
- package/out/constraints/props/prop.js.map +1 -0
- package/out/constraints/props/props.d.ts +37 -0
- package/out/constraints/props/props.d.ts.map +1 -0
- package/out/constraints/props/props.js +121 -0
- package/out/constraints/props/props.js.map +1 -0
- package/out/constraints/props/required.d.ts +46 -0
- package/out/constraints/props/required.d.ts.map +1 -0
- package/out/constraints/props/required.js +81 -0
- package/out/constraints/props/required.js.map +1 -0
- package/out/constraints/props/sequence.d.ts +82 -0
- package/out/constraints/props/sequence.d.ts.map +1 -0
- package/out/constraints/props/sequence.js +376 -0
- package/out/constraints/props/sequence.js.map +1 -0
- package/out/constraints/props/shared.d.ts +3 -0
- package/out/constraints/props/shared.d.ts.map +1 -0
- package/out/constraints/props/shared.js +4 -0
- package/out/constraints/props/shared.js.map +1 -0
- package/out/constraints/refinements/after.d.ts +33 -0
- package/out/constraints/refinements/after.d.ts.map +1 -0
- package/out/constraints/refinements/after.js +36 -0
- package/out/constraints/refinements/after.js.map +1 -0
- package/out/constraints/refinements/before.d.ts +33 -0
- package/out/constraints/refinements/before.d.ts.map +1 -0
- package/out/constraints/refinements/before.js +43 -0
- package/out/constraints/refinements/before.js.map +1 -0
- package/out/constraints/refinements/divisor.d.ts +52 -0
- package/out/constraints/refinements/divisor.d.ts.map +1 -0
- package/out/constraints/refinements/divisor.js +52 -0
- package/out/constraints/refinements/divisor.js.map +1 -0
- package/out/constraints/refinements/exactLength.d.ts +45 -0
- package/out/constraints/refinements/exactLength.d.ts.map +1 -0
- package/out/constraints/refinements/exactLength.js +52 -0
- package/out/constraints/refinements/exactLength.js.map +1 -0
- package/out/constraints/refinements/kinds.d.ts +29 -0
- package/out/constraints/refinements/kinds.d.ts.map +1 -0
- package/out/constraints/refinements/kinds.js +17 -0
- package/out/constraints/refinements/kinds.js.map +1 -0
- package/out/constraints/refinements/length.d.ts +48 -0
- package/out/constraints/refinements/length.d.ts.map +1 -0
- package/out/constraints/refinements/length.js +53 -0
- package/out/constraints/refinements/length.js.map +1 -0
- package/out/constraints/refinements/max.d.ts +31 -0
- package/out/constraints/refinements/max.d.ts.map +1 -0
- package/out/constraints/refinements/max.js +35 -0
- package/out/constraints/refinements/max.js.map +1 -0
- package/out/constraints/refinements/maxLength.d.ts +32 -0
- package/out/constraints/refinements/maxLength.d.ts.map +1 -0
- package/out/constraints/refinements/maxLength.js +36 -0
- package/out/constraints/refinements/maxLength.js.map +1 -0
- package/out/constraints/refinements/min.d.ts +31 -0
- package/out/constraints/refinements/min.d.ts.map +1 -0
- package/out/constraints/refinements/min.js +28 -0
- package/out/constraints/refinements/min.js.map +1 -0
- package/out/constraints/refinements/minLength.d.ts +32 -0
- package/out/constraints/refinements/minLength.d.ts.map +1 -0
- package/out/constraints/refinements/minLength.js +33 -0
- package/out/constraints/refinements/minLength.js.map +1 -0
- package/out/constraints/refinements/range.d.ts +119 -0
- package/out/constraints/refinements/range.d.ts.map +1 -0
- package/out/constraints/refinements/range.js +89 -0
- package/out/constraints/refinements/range.js.map +1 -0
- package/out/constraints/refinements/regex.d.ts +47 -0
- package/out/constraints/refinements/regex.d.ts.map +1 -0
- package/out/constraints/refinements/regex.js +37 -0
- package/out/constraints/refinements/regex.js.map +1 -0
- package/out/constraints/refinements/shared.d.ts +20 -0
- package/out/constraints/refinements/shared.d.ts.map +1 -0
- package/out/constraints/refinements/shared.js +10 -0
- package/out/constraints/refinements/shared.js.map +1 -0
- package/out/generic.d.ts +22 -0
- package/out/generic.d.ts.map +1 -0
- package/out/generic.js +23 -0
- package/out/generic.js.map +1 -0
- package/out/inference.d.ts +137 -0
- package/out/inference.d.ts.map +1 -0
- package/out/inference.js +73 -0
- package/out/inference.js.map +1 -0
- package/out/keywords/ark.d.ts +70 -0
- package/out/keywords/ark.d.ts.map +1 -0
- package/out/keywords/ark.js +29 -0
- package/out/keywords/ark.js.map +1 -0
- package/out/keywords/internal.d.ts +13 -0
- package/out/keywords/internal.d.ts.map +1 -0
- package/out/keywords/internal.js +13 -0
- package/out/keywords/internal.js.map +1 -0
- package/out/keywords/jsObject.d.ts +20 -0
- package/out/keywords/jsObject.d.ts.map +1 -0
- package/out/keywords/jsObject.js +18 -0
- package/out/keywords/jsObject.js.map +1 -0
- package/out/keywords/parsing.d.ts +17 -0
- package/out/keywords/parsing.d.ts.map +1 -0
- package/out/keywords/parsing.js +64 -0
- package/out/keywords/parsing.js.map +1 -0
- package/out/keywords/root.d.ts +7 -0
- package/out/keywords/root.d.ts.map +1 -0
- package/out/keywords/root.js +4 -0
- package/out/keywords/root.js.map +1 -0
- package/out/keywords/tsGenerics.d.ts +2 -0
- package/out/keywords/tsGenerics.d.ts.map +1 -0
- package/out/keywords/tsGenerics.js +26 -0
- package/out/keywords/tsGenerics.js.map +1 -0
- package/out/keywords/tsPrimitive.d.ts +24 -0
- package/out/keywords/tsPrimitive.d.ts.map +1 -0
- package/out/keywords/tsPrimitive.js +25 -0
- package/out/keywords/tsPrimitive.js.map +1 -0
- package/out/keywords/utils/creditCard.d.ts +11 -0
- package/out/keywords/utils/creditCard.d.ts.map +1 -0
- package/out/keywords/utils/creditCard.js +35 -0
- package/out/keywords/utils/creditCard.js.map +1 -0
- package/out/keywords/utils/date.d.ts +45 -0
- package/out/keywords/utils/date.d.ts.map +1 -0
- package/out/keywords/utils/date.js +56 -0
- package/out/keywords/utils/date.js.map +1 -0
- package/out/keywords/validation.d.ts +20 -0
- package/out/keywords/validation.d.ts.map +1 -0
- package/out/keywords/validation.js +80 -0
- package/out/keywords/validation.js.map +1 -0
- package/out/kinds.d.ts +102 -0
- package/out/kinds.d.ts.map +1 -0
- package/out/kinds.js +29 -0
- package/out/kinds.js.map +1 -0
- package/out/main.d.ts +6 -6
- package/out/main.d.ts.map +1 -1
- package/out/main.js +4 -3
- package/out/main.js.map +1 -1
- package/out/match.d.ts +18 -23
- package/out/match.d.ts.map +1 -1
- package/out/match.js +3 -4
- package/out/match.js.map +1 -1
- package/out/module.d.ts +11 -0
- package/out/module.d.ts.map +1 -0
- package/out/module.js +4 -0
- package/out/module.js.map +1 -0
- package/out/parse.d.ts +29 -0
- package/out/parse.d.ts.map +1 -0
- package/out/parse.js +161 -0
- package/out/parse.js.map +1 -0
- package/out/parser/definition.d.ts +9 -12
- package/out/parser/definition.d.ts.map +1 -1
- package/out/parser/definition.js +10 -12
- package/out/parser/definition.js.map +1 -1
- package/out/parser/generic.d.ts +3 -4
- package/out/parser/generic.d.ts.map +1 -1
- package/out/parser/generic.js +6 -8
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts +44 -12
- package/out/parser/objectLiteral.d.ts.map +1 -1
- package/out/parser/objectLiteral.js +43 -36
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/bounds.d.ts +9 -6
- package/out/parser/semantic/bounds.d.ts.map +1 -1
- package/out/parser/semantic/bounds.js.map +1 -1
- package/out/parser/semantic/divisor.d.ts +5 -6
- package/out/parser/semantic/divisor.d.ts.map +1 -1
- package/out/parser/semantic/infer.d.ts +25 -0
- package/out/parser/semantic/infer.d.ts.map +1 -0
- package/out/parser/semantic/infer.js +2 -0
- package/out/parser/semantic/infer.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +3 -6
- package/out/parser/semantic/utils.d.ts.map +1 -1
- package/out/parser/semantic/validate.d.ts +14 -12
- package/out/parser/semantic/validate.d.ts.map +1 -1
- package/out/parser/semantic/validate.js +1 -0
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +13 -12
- package/out/parser/string/reduce/dynamic.d.ts.map +1 -1
- package/out/parser/string/reduce/dynamic.js +27 -17
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/reduce/shared.d.ts +1 -2
- package/out/parser/string/reduce/shared.d.ts.map +1 -1
- package/out/parser/string/reduce/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +26 -22
- package/out/parser/string/reduce/static.d.ts.map +1 -1
- package/out/parser/string/shift/operand/date.d.ts +2 -1
- package/out/parser/string/shift/operand/date.d.ts.map +1 -1
- package/out/parser/string/shift/operand/date.js +2 -2
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +2 -2
- package/out/parser/string/shift/operand/enclosed.d.ts.map +1 -1
- package/out/parser/string/shift/operand/enclosed.js +6 -4
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +9 -9
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.js +6 -8
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +3 -3
- package/out/parser/string/shift/operand/operand.d.ts.map +1 -1
- package/out/parser/string/shift/operand/operand.js +8 -11
- package/out/parser/string/shift/operand/operand.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +13 -20
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.js +21 -26
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts +15 -12
- package/out/parser/string/shift/operator/bounds.d.ts.map +1 -1
- package/out/parser/string/shift/operator/bounds.js +42 -48
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts +1 -2
- package/out/parser/string/shift/operator/divisor.d.ts.map +1 -1
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +1 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +1 -1
- package/out/parser/string/shift/operator/operator.js +12 -18
- package/out/parser/string/shift/operator/operator.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts +2 -1
- package/out/parser/string/shift/scanner.d.ts.map +1 -1
- package/out/parser/string/shift/scanner.js +12 -11
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts +8 -5
- package/out/parser/string/string.d.ts.map +1 -1
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts +96 -37
- package/out/parser/tuple.d.ts.map +1 -1
- package/out/parser/tuple.js +61 -66
- package/out/parser/tuple.js.map +1 -1
- package/out/schema.d.ts +175 -0
- package/out/schema.d.ts.map +1 -0
- package/out/schema.js +73 -0
- package/out/schema.js.map +1 -0
- package/out/schemaScope.d.ts +180 -0
- package/out/schemaScope.d.ts.map +1 -0
- package/out/schemaScope.js +235 -0
- package/out/schemaScope.js.map +1 -0
- package/out/scope.d.ts +42 -114
- package/out/scope.d.ts.map +1 -1
- package/out/scope.js +51 -181
- package/out/scope.js.map +1 -1
- package/out/shared/compile.d.ts +22 -0
- package/out/shared/compile.d.ts.map +1 -0
- package/out/shared/compile.js +79 -0
- package/out/shared/compile.js.map +1 -0
- package/out/shared/context.d.ts +54 -0
- package/out/shared/context.d.ts.map +1 -0
- package/out/shared/context.js +53 -0
- package/out/shared/context.js.map +1 -0
- package/out/shared/declare.d.ts +69 -0
- package/out/shared/declare.d.ts.map +1 -0
- package/out/shared/declare.js +2 -0
- package/out/shared/declare.js.map +1 -0
- package/out/shared/disjoint.d.ts +91 -0
- package/out/shared/disjoint.d.ts.map +1 -0
- package/out/shared/disjoint.js +110 -0
- package/out/shared/disjoint.js.map +1 -0
- package/out/shared/errors.d.ts +99 -0
- package/out/shared/errors.d.ts.map +1 -0
- package/out/shared/errors.js +108 -0
- package/out/shared/errors.js.map +1 -0
- package/out/shared/implement.d.ts +363 -0
- package/out/shared/implement.d.ts.map +1 -0
- package/out/shared/implement.js +68 -0
- package/out/shared/implement.js.map +1 -0
- package/out/shared/inference.d.ts +6 -0
- package/out/shared/inference.d.ts.map +1 -0
- package/out/shared/inference.js +5 -0
- package/out/shared/inference.js.map +1 -0
- package/out/shared/intersections.d.ts +60 -0
- package/out/shared/intersections.d.ts.map +1 -0
- package/out/shared/intersections.js +2 -0
- package/out/shared/intersections.js.map +1 -0
- package/out/shared/traversal.d.ts +62 -0
- package/out/shared/traversal.d.ts.map +1 -0
- package/out/shared/traversal.js +97 -0
- package/out/shared/traversal.js.map +1 -0
- package/out/shared/utils.d.ts +24 -0
- package/out/shared/utils.d.ts.map +1 -0
- package/out/shared/utils.js +15 -0
- package/out/shared/utils.js.map +1 -0
- package/out/type.d.ts +23 -59
- package/out/type.d.ts.map +1 -1
- package/out/type.js +2 -137
- package/out/type.js.map +1 -1
- package/out/types/basis.d.ts +47 -0
- package/out/types/basis.d.ts.map +1 -0
- package/out/types/basis.js +15 -0
- package/out/types/basis.js.map +1 -0
- package/out/types/discriminate.d.ts +34 -0
- package/out/types/discriminate.d.ts.map +1 -0
- package/out/types/discriminate.js +120 -0
- package/out/types/discriminate.js.map +1 -0
- package/out/types/domain.d.ts +57 -0
- package/out/types/domain.d.ts.map +1 -0
- package/out/types/domain.js +41 -0
- package/out/types/domain.js.map +1 -0
- package/out/types/intersection.d.ts +127 -0
- package/out/types/intersection.d.ts.map +1 -0
- package/out/types/intersection.js +379 -0
- package/out/types/intersection.js.map +1 -0
- package/out/types/morph.d.ts +157 -0
- package/out/types/morph.d.ts.map +1 -0
- package/out/types/morph.js +99 -0
- package/out/types/morph.js.map +1 -0
- package/out/types/proto.d.ts +51 -0
- package/out/types/proto.d.ts.map +1 -0
- package/out/types/proto.js +60 -0
- package/out/types/proto.js.map +1 -0
- package/out/types/type.d.ts +143 -0
- package/out/types/type.d.ts.map +1 -0
- package/out/types/type.js +123 -0
- package/out/types/type.js.map +1 -0
- package/out/types/union.d.ts +71 -0
- package/out/types/union.d.ts.map +1 -0
- package/out/types/union.js +379 -0
- package/out/types/union.js.map +1 -0
- package/out/types/unit.d.ts +45 -0
- package/out/types/unit.d.ts.map +1 -0
- package/out/types/unit.js +56 -0
- package/out/types/unit.js.map +1 -0
- package/out/util.d.ts +30 -18
- package/out/util.d.ts.map +1 -1
- package/out/util.js +8 -7
- package/out/util.js.map +1 -1
- package/package.json +7 -3
- package/parser/definition.ts +73 -89
- package/parser/generic.ts +35 -36
- package/parser/objectLiteral.ts +143 -117
- package/parser/semantic/bounds.ts +33 -32
- package/parser/semantic/divisor.ts +8 -10
- package/parser/semantic/infer.ts +141 -0
- package/parser/semantic/utils.ts +12 -23
- package/parser/semantic/validate.ts +82 -73
- package/parser/string/reduce/dynamic.ts +61 -46
- package/parser/string/reduce/shared.ts +1 -3
- package/parser/string/reduce/static.ts +65 -67
- package/parser/string/shift/operand/date.ts +12 -8
- package/parser/string/shift/operand/enclosed.ts +22 -17
- package/parser/string/shift/operand/genericArgs.ts +46 -45
- package/parser/string/shift/operand/operand.ts +32 -38
- package/parser/string/shift/operand/unenclosed.ts +106 -147
- package/parser/string/shift/operator/bounds.ts +114 -106
- package/parser/string/shift/operator/divisor.ts +12 -14
- package/parser/string/shift/operator/operator.ts +35 -46
- package/parser/string/shift/scanner.ts +49 -46
- package/parser/string/string.ts +28 -32
- package/parser/tuple.ts +288 -308
- package/scope.ts +183 -425
- package/type.ts +82 -281
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/typeMethods.test.ts +0 -69
- package/out/arkConfig.d.ts +0 -2
- package/out/arkConfig.d.ts.map +0 -1
- package/out/arkConfig.js +0 -10
- package/out/arkConfig.js.map +0 -1
- package/out/parser/semantic/intersections.d.ts +0 -14
- package/out/parser/semantic/intersections.d.ts.map +0 -1
- package/out/parser/semantic/intersections.js +0 -2
- package/out/parser/semantic/intersections.js.map +0 -1
- package/out/parser/semantic/semantic.d.ts +0 -32
- package/out/parser/semantic/semantic.d.ts.map +0 -1
- package/out/parser/semantic/semantic.js +0 -2
- package/out/parser/semantic/semantic.js.map +0 -1
- package/out/parser/shared.d.ts +0 -62
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -56
- package/out/parser/shared.js.map +0 -1
- package/out/test.d.ts +0 -2
- package/out/test.d.ts.map +0 -1
- package/out/test.js +0 -7
- package/out/test.js.map +0 -1
- package/out/tsconfig.build.tsbuildinfo +0 -1
- package/parser/semantic/semantic.ts +0 -143
- package/parser/shared.ts +0 -170
- package/util.ts +0 -36
package/parser/definition.ts
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type RawSchema, hasArkKind, type string } from "@arktype/schema"
|
|
2
2
|
import {
|
|
3
|
-
isThunk,
|
|
4
|
-
objectKindOf,
|
|
5
|
-
printable,
|
|
6
|
-
throwParseError,
|
|
7
3
|
type Dict,
|
|
8
4
|
type ErrorMessage,
|
|
9
|
-
type List,
|
|
10
5
|
type Primitive,
|
|
6
|
+
type array,
|
|
11
7
|
type defined,
|
|
12
8
|
type equals,
|
|
13
|
-
type evaluate,
|
|
14
9
|
type isAny,
|
|
10
|
+
isThunk,
|
|
15
11
|
type isUnknown,
|
|
12
|
+
objectKindOf,
|
|
16
13
|
type objectKindOrDomainOf,
|
|
17
14
|
type optionalKeyOf,
|
|
18
|
-
|
|
15
|
+
printable,
|
|
16
|
+
type requiredKeyOf,
|
|
17
|
+
type show,
|
|
18
|
+
throwParseError
|
|
19
19
|
} from "@arktype/util"
|
|
20
20
|
import type { type } from "../ark.js"
|
|
21
21
|
import type { ParseContext } from "../scope.js"
|
|
22
|
-
import { Type } from "../type.js"
|
|
23
22
|
import {
|
|
24
|
-
parseObjectLiteral,
|
|
25
23
|
type inferObjectLiteral,
|
|
24
|
+
parseObjectLiteral,
|
|
26
25
|
type validateObjectLiteral
|
|
27
26
|
} from "./objectLiteral.js"
|
|
28
27
|
import type { validateString } from "./semantic/validate.js"
|
|
29
28
|
import type { BaseCompletions, inferString } from "./string/string.js"
|
|
30
29
|
import {
|
|
31
|
-
parseTuple,
|
|
32
30
|
type TupleExpression,
|
|
33
31
|
type inferTuple,
|
|
32
|
+
parseTuple,
|
|
34
33
|
type validateTuple
|
|
35
34
|
} from "./tuple.js"
|
|
36
35
|
|
|
37
|
-
export const parseObject = (def: object, ctx: ParseContext):
|
|
36
|
+
export const parseObject = (def: object, ctx: ParseContext): RawSchema => {
|
|
38
37
|
const objectKind = objectKindOf(def)
|
|
39
38
|
switch (objectKind) {
|
|
40
39
|
case undefined:
|
|
41
|
-
if (def
|
|
42
|
-
return def.root
|
|
43
|
-
}
|
|
44
|
-
if (isNode(def) && def.isType()) {
|
|
45
|
-
return def
|
|
46
|
-
}
|
|
40
|
+
if (hasArkKind(def, "schema")) return def
|
|
47
41
|
return parseObjectLiteral(def as Dict, ctx)
|
|
48
42
|
case "Array":
|
|
49
|
-
return parseTuple(def as
|
|
43
|
+
return parseTuple(def as array, ctx)
|
|
50
44
|
case "RegExp":
|
|
51
|
-
return
|
|
52
|
-
|
|
45
|
+
return ctx.$.node(
|
|
46
|
+
"intersection",
|
|
47
|
+
{
|
|
48
|
+
domain: "string",
|
|
49
|
+
regex: def as RegExp
|
|
50
|
+
},
|
|
51
|
+
{ prereduced: true }
|
|
52
|
+
)
|
|
53
|
+
case "Function": {
|
|
53
54
|
const resolvedDef = isThunk(def) ? def() : def
|
|
54
|
-
if (resolvedDef
|
|
55
|
-
return resolvedDef.root
|
|
56
|
-
}
|
|
55
|
+
if (hasArkKind(resolvedDef, "schema")) return resolvedDef
|
|
57
56
|
return throwParseError(writeBadDefinitionTypeMessage("Function"))
|
|
57
|
+
}
|
|
58
58
|
default:
|
|
59
59
|
return throwParseError(
|
|
60
60
|
writeBadDefinitionTypeMessage(objectKind ?? printable(def))
|
|
@@ -62,80 +62,64 @@ export const parseObject = (def: object, ctx: ParseContext): TypeNode => {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export type inferDefinition<def, $, args> =
|
|
66
|
-
? never
|
|
67
|
-
: def extends type.cast<infer t> | ThunkCast<infer t>
|
|
68
|
-
?
|
|
69
|
-
: def extends
|
|
70
|
-
|
|
71
|
-
: def extends
|
|
72
|
-
? inferTuple<def, $, args>
|
|
73
|
-
: def extends RegExp
|
|
74
|
-
? is<string, { anonymousPattern: true }>
|
|
75
|
-
: def extends object
|
|
76
|
-
? inferObjectLiteral<def, $, args>
|
|
65
|
+
export type inferDefinition<def, $, args> =
|
|
66
|
+
isAny<def> extends true ? never
|
|
67
|
+
: def extends type.cast<infer t> | ThunkCast<infer t> ? t
|
|
68
|
+
: def extends string ? inferString<def, $, args>
|
|
69
|
+
: def extends array ? inferTuple<def, $, args>
|
|
70
|
+
: def extends RegExp ? string.matching<string>
|
|
71
|
+
: def extends object ? inferObjectLiteral<def, $, args>
|
|
77
72
|
: never
|
|
78
73
|
|
|
79
|
-
export type validateDefinition<def, $, args> =
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
: def extends
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
:
|
|
92
|
-
? writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>
|
|
93
|
-
: isUnknown<def> extends true
|
|
94
|
-
? // this allows the initial list of autocompletions to be populated when a user writes "type()",
|
|
95
|
-
// before having specified a definition
|
|
96
|
-
BaseCompletions<$, args> | {}
|
|
97
|
-
: validateObjectLiteral<def, $, args>
|
|
74
|
+
export type validateDefinition<def, $, args> =
|
|
75
|
+
null extends undefined ?
|
|
76
|
+
ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`>
|
|
77
|
+
: [def] extends [Terminal] ? def
|
|
78
|
+
: def extends string ? validateString<def, $, args>
|
|
79
|
+
: def extends array ? validateTuple<def, $, args>
|
|
80
|
+
: def extends BadDefinitionType ?
|
|
81
|
+
writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>
|
|
82
|
+
: isUnknown<def> extends true ?
|
|
83
|
+
// this allows the initial list of autocompletions to be populated when a user writes "type()",
|
|
84
|
+
// before having specified a definition
|
|
85
|
+
BaseCompletions<$, args> | {}
|
|
86
|
+
: validateObjectLiteral<def, $, args>
|
|
98
87
|
|
|
99
88
|
export type validateDeclared<declared, def, $, args> =
|
|
100
|
-
def extends validateDefinition<def, $, args>
|
|
101
|
-
|
|
102
|
-
|
|
89
|
+
def extends validateDefinition<def, $, args> ?
|
|
90
|
+
validateInference<def, declared, $, args>
|
|
91
|
+
: validateDefinition<def, $, args>
|
|
103
92
|
|
|
104
|
-
type validateInference<def, declared, $, args> =
|
|
105
|
-
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
: evaluate<declarationMismatch<def, declared, $, args>>
|
|
118
|
-
: def extends object
|
|
119
|
-
? evaluate<
|
|
93
|
+
type validateInference<def, declared, $, args> =
|
|
94
|
+
def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ?
|
|
95
|
+
validateShallowInference<def, declared, $, args>
|
|
96
|
+
: def extends array ?
|
|
97
|
+
declared extends array ?
|
|
98
|
+
{
|
|
99
|
+
[i in keyof declared]: i extends keyof def ?
|
|
100
|
+
validateInference<def[i], declared[i], $, args>
|
|
101
|
+
: unknown
|
|
102
|
+
}
|
|
103
|
+
: show<declarationMismatch<def, declared, $, args>>
|
|
104
|
+
: def extends object ?
|
|
105
|
+
show<
|
|
120
106
|
{
|
|
121
|
-
[k in requiredKeyOf<declared>]: k extends keyof def
|
|
122
|
-
|
|
123
|
-
|
|
107
|
+
[k in requiredKeyOf<declared>]: k extends keyof def ?
|
|
108
|
+
validateInference<def[k], declared[k], $, args>
|
|
109
|
+
: unknown
|
|
124
110
|
} & {
|
|
125
|
-
[k in optionalKeyOf<declared> &
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
111
|
+
[k in optionalKeyOf<declared> & string as `${k}?`]: `${k}?` extends (
|
|
112
|
+
keyof def
|
|
113
|
+
) ?
|
|
114
|
+
validateInference<def[`${k}?`], defined<declared[k]>, $, args>
|
|
115
|
+
: unknown
|
|
129
116
|
}
|
|
130
|
-
|
|
131
|
-
:
|
|
117
|
+
>
|
|
118
|
+
: validateShallowInference<def, declared, $, args>
|
|
132
119
|
|
|
133
|
-
type validateShallowInference<def, declared, $, args> =
|
|
134
|
-
inferDefinition<def, $, args>,
|
|
135
|
-
declared
|
|
136
|
-
> extends true
|
|
137
|
-
? def
|
|
138
|
-
: evaluate<declarationMismatch<def, declared, $, args>>
|
|
120
|
+
type validateShallowInference<def, declared, $, args> =
|
|
121
|
+
equals<inferDefinition<def, $, args>, declared> extends true ? def
|
|
122
|
+
: show<declarationMismatch<def, declared, $, args>>
|
|
139
123
|
|
|
140
124
|
type declarationMismatch<def, declared, $, args> = {
|
|
141
125
|
declared: declared
|
package/parser/generic.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type nominal, throwParseError } from "@arktype/util"
|
|
2
2
|
import { writeUnexpectedCharacterMessage } from "./string/shift/operator/operator.js"
|
|
3
3
|
import { Scanner } from "./string/shift/scanner.js"
|
|
4
4
|
|
|
@@ -12,57 +12,56 @@ export type GenericParamsParseError<message extends string = string> = [
|
|
|
12
12
|
nominal<message, "InvalidGenericParameters">
|
|
13
13
|
]
|
|
14
14
|
|
|
15
|
-
export const parseGenericParams = (def: string) =>
|
|
16
|
-
|
|
15
|
+
export const parseGenericParams = (def: string): string[] =>
|
|
16
|
+
$parseGenericParams(new Scanner(def))
|
|
17
17
|
|
|
18
|
-
export type parseGenericParams<def extends string> =
|
|
19
|
-
def,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
? GenericParamsParseError<emptyGenericParameterMessage>
|
|
25
|
-
: result
|
|
26
|
-
: never
|
|
18
|
+
export type parseGenericParams<def extends string> =
|
|
19
|
+
$parseParams<def, "", []> extends infer result extends string[] ?
|
|
20
|
+
"" extends result[number] ?
|
|
21
|
+
GenericParamsParseError<emptyGenericParameterMessage>
|
|
22
|
+
: result
|
|
23
|
+
: never
|
|
27
24
|
|
|
28
|
-
export const emptyGenericParameterMessage =
|
|
25
|
+
export const emptyGenericParameterMessage =
|
|
26
|
+
"An empty string is not a valid generic parameter name"
|
|
29
27
|
|
|
30
28
|
export type emptyGenericParameterMessage = typeof emptyGenericParameterMessage
|
|
31
29
|
|
|
32
|
-
const
|
|
30
|
+
const $parseGenericParams = (scanner: Scanner): string[] => {
|
|
33
31
|
const param = scanner.shiftUntilNextTerminator()
|
|
34
32
|
if (param === "") {
|
|
35
33
|
throwParseError(emptyGenericParameterMessage)
|
|
36
34
|
}
|
|
37
35
|
scanner.shiftUntilNonWhitespace()
|
|
38
36
|
const nextNonWhitespace = scanner.shift()
|
|
39
|
-
return
|
|
40
|
-
? [param]
|
|
41
|
-
: nextNonWhitespace === ","
|
|
42
|
-
? [param, ...parseGenericParamsRecurse(scanner)]
|
|
37
|
+
return (
|
|
38
|
+
nextNonWhitespace === "" ? [param]
|
|
39
|
+
: nextNonWhitespace === "," ? [param, ...$parseGenericParams(scanner)]
|
|
43
40
|
: throwParseError(writeUnexpectedCharacterMessage(nextNonWhitespace, ","))
|
|
41
|
+
)
|
|
44
42
|
}
|
|
45
43
|
|
|
46
|
-
type
|
|
44
|
+
type $parseParams<
|
|
47
45
|
unscanned extends string,
|
|
48
46
|
param extends string,
|
|
49
47
|
result extends string[]
|
|
50
|
-
> =
|
|
51
|
-
|
|
52
|
-
?
|
|
53
|
-
: lookahead extends Scanner.WhiteSpaceToken
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
: Scanner.skipWhitespace<nextUnscanned> extends
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
48
|
+
> =
|
|
49
|
+
unscanned extends `${infer lookahead}${infer nextUnscanned}` ?
|
|
50
|
+
lookahead extends "," ? $parseParams<nextUnscanned, "", [...result, param]>
|
|
51
|
+
: lookahead extends Scanner.WhiteSpaceToken ?
|
|
52
|
+
param extends "" ?
|
|
53
|
+
// if the next char is whitespace and we aren't in the middle of a param, skip to the next one
|
|
54
|
+
$parseParams<Scanner.skipWhitespace<nextUnscanned>, "", result>
|
|
55
|
+
: Scanner.skipWhitespace<nextUnscanned> extends (
|
|
56
|
+
`${infer nextNonWhitespace}${infer rest}`
|
|
57
|
+
) ?
|
|
58
|
+
nextNonWhitespace extends "," ?
|
|
59
|
+
$parseParams<rest, "", [...result, param]>
|
|
60
|
+
: GenericParamsParseError<
|
|
61
61
|
writeUnexpectedCharacterMessage<nextNonWhitespace, ",">
|
|
62
|
-
|
|
63
|
-
:
|
|
64
|
-
|
|
65
|
-
:
|
|
66
|
-
: param extends ""
|
|
67
|
-
? result
|
|
62
|
+
>
|
|
63
|
+
: // params end with a single whitespace character, add the current token
|
|
64
|
+
[...result, param]
|
|
65
|
+
: $parseParams<nextUnscanned, `${param}${lookahead}`, result>
|
|
66
|
+
: param extends "" ? result
|
|
68
67
|
: [...result, param]
|
package/parser/objectLiteral.ts
CHANGED
|
@@ -1,109 +1,88 @@
|
|
|
1
|
-
import { keywords, schema, type Inner, type TypeNode } from "@arktype/schema"
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
type Node,
|
|
3
|
+
type RawSchema,
|
|
4
|
+
tsKeywords,
|
|
5
|
+
type writeInvalidPropertyKeyMessage
|
|
6
|
+
} from "@arktype/schema"
|
|
7
|
+
import {
|
|
5
8
|
type Dict,
|
|
6
9
|
type ErrorMessage,
|
|
7
|
-
type
|
|
8
|
-
type merge
|
|
10
|
+
type Key,
|
|
11
|
+
type merge,
|
|
12
|
+
printable,
|
|
13
|
+
type show,
|
|
14
|
+
stringAndSymbolicEntriesOf,
|
|
15
|
+
throwParseError
|
|
9
16
|
} from "@arktype/util"
|
|
10
|
-
|
|
11
17
|
import type { ParseContext } from "../scope.js"
|
|
12
18
|
import type { inferDefinition, validateDefinition } from "./definition.js"
|
|
13
19
|
import type { astToString } from "./semantic/utils.js"
|
|
14
20
|
import type { validateString } from "./semantic/validate.js"
|
|
15
|
-
import {
|
|
16
|
-
parseEntry,
|
|
17
|
-
type EntryParseResult,
|
|
18
|
-
type IndexedKey,
|
|
19
|
-
type OptionalValue,
|
|
20
|
-
type validateObjectValue
|
|
21
|
-
} from "./shared.js"
|
|
22
|
-
|
|
23
|
-
const stringAndSymbolicEntriesOf = (o: Record<string | symbol, unknown>) => [
|
|
24
|
-
...Object.entries(o),
|
|
25
|
-
...Object.getOwnPropertySymbols(o).map((k) => [k, o[k]] as const)
|
|
26
|
-
]
|
|
27
|
-
|
|
28
|
-
export const parseObjectLiteral = (def: Dict, ctx: ParseContext): TypeNode => {
|
|
29
|
-
const required: Inner<"required">[] = []
|
|
30
|
-
const optional: Inner<"optional">[] = []
|
|
21
|
+
import { Scanner } from "./string/shift/scanner.js"
|
|
31
22
|
|
|
23
|
+
export const parseObjectLiteral = (def: Dict, ctx: ParseContext): RawSchema => {
|
|
24
|
+
const propNodes: Node<"prop">[] = []
|
|
25
|
+
const indexNodes: Node<"index">[] = []
|
|
32
26
|
// We only allow a spread operator to be used as the first key in an object
|
|
33
27
|
// because to match JS behavior any keys before the spread are overwritten
|
|
34
28
|
// by the values in the target object, so there'd be no useful purpose in having it
|
|
35
29
|
// anywhere except for the beginning.
|
|
36
|
-
// Discussion in ArkType Discord:
|
|
37
|
-
// https://discord.com/channels/957797212103016458/1103023445035462678/1182814502471860334
|
|
38
|
-
|
|
39
30
|
const parsedEntries = stringAndSymbolicEntriesOf(def).map(parseEntry)
|
|
40
|
-
if (parsedEntries[0]
|
|
31
|
+
if (parsedEntries[0]?.kind === "spread") {
|
|
41
32
|
// remove the spread entry so we can iterate over the remaining entries
|
|
42
33
|
// expecting non-spread entries
|
|
43
34
|
const spreadEntry = parsedEntries.shift()!
|
|
44
|
-
const spreadNode = ctx
|
|
45
|
-
|
|
35
|
+
const spreadNode = ctx.$.parse(spreadEntry.value, ctx)
|
|
46
36
|
if (
|
|
47
|
-
spreadNode.
|
|
48
|
-
!spreadNode.extends(
|
|
37
|
+
!spreadNode.hasKind("intersection") ||
|
|
38
|
+
!spreadNode.extends(tsKeywords.object)
|
|
49
39
|
) {
|
|
50
40
|
return throwParseError(
|
|
51
|
-
writeInvalidSpreadTypeMessage(printable(spreadEntry.
|
|
41
|
+
writeInvalidSpreadTypeMessage(printable(spreadEntry.value))
|
|
52
42
|
)
|
|
53
43
|
}
|
|
54
|
-
|
|
55
44
|
// TODO: move to props group merge in schema
|
|
56
45
|
// For each key on spreadNode, add it to our object.
|
|
57
46
|
// We filter out keys from the spreadNode that will be defined later on this same object
|
|
58
47
|
// because the currently parsed definition will overwrite them.
|
|
59
|
-
spreadNode.
|
|
48
|
+
spreadNode.prop?.forEach(
|
|
60
49
|
(spreadRequired) =>
|
|
61
50
|
!parsedEntries.some(
|
|
62
|
-
({ innerKey }) => innerKey === spreadRequired.key
|
|
63
|
-
) &&
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
spreadNode.optional?.forEach(
|
|
67
|
-
(spreadOptional) =>
|
|
68
|
-
!parsedEntries.some(
|
|
69
|
-
({ innerKey }) => innerKey === spreadOptional.key
|
|
70
|
-
) && optional.push(spreadOptional)
|
|
51
|
+
({ inner: innerKey }) => innerKey === spreadRequired.key
|
|
52
|
+
) && propNodes.push(spreadRequired)
|
|
71
53
|
)
|
|
72
54
|
}
|
|
73
55
|
for (const entry of parsedEntries) {
|
|
74
56
|
if (entry.kind === "spread") {
|
|
75
|
-
return throwParseError(
|
|
76
|
-
"Spread operator may only be used as the first key in an object"
|
|
77
|
-
)
|
|
57
|
+
return throwParseError(nonLeadingSpreadError)
|
|
78
58
|
}
|
|
79
59
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
)
|
|
87
|
-
const value = ctx.scope.parse(entry.innerValue, ctx)
|
|
88
|
-
const inner: Inner<"required" | "optional"> = {
|
|
89
|
-
key: entry.innerKey,
|
|
90
|
-
value
|
|
91
|
-
}
|
|
92
|
-
if (entry.kind === "optional") {
|
|
93
|
-
optional.push(inner)
|
|
60
|
+
if (entry.kind === "index") {
|
|
61
|
+
// handle key parsing first to match type behavior
|
|
62
|
+
const key = ctx.$.parse(entry.inner, ctx)
|
|
63
|
+
const value = ctx.$.parse(entry.value, ctx)
|
|
64
|
+
const indexNode = ctx.$.node("index", { key, value })
|
|
65
|
+
indexNodes.push(indexNode)
|
|
94
66
|
} else {
|
|
95
|
-
|
|
67
|
+
const value = ctx.$.parse(entry.value, ctx)
|
|
68
|
+
const propNode = ctx.$.node("prop", {
|
|
69
|
+
key: entry.inner,
|
|
70
|
+
value,
|
|
71
|
+
optional: entry.kind === "optional"
|
|
72
|
+
})
|
|
73
|
+
propNodes.push(propNode)
|
|
96
74
|
}
|
|
97
|
-
ctx.path.pop()
|
|
98
75
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
optional
|
|
76
|
+
return ctx.$.schema({
|
|
77
|
+
domain: "object",
|
|
78
|
+
prop: propNodes,
|
|
79
|
+
index: indexNodes
|
|
104
80
|
})
|
|
105
81
|
}
|
|
106
82
|
|
|
83
|
+
export const nonLeadingSpreadError =
|
|
84
|
+
"Spread operator may only be used as the first key in an object"
|
|
85
|
+
|
|
107
86
|
/**
|
|
108
87
|
* Infers the contents of an object literal, ignoring a spread definition
|
|
109
88
|
* You probably want to use {@link inferObjectLiteral} instead.
|
|
@@ -112,63 +91,118 @@ type inferObjectLiteralInner<def extends object, $, args> = {
|
|
|
112
91
|
// since def is a const parameter, we remove the readonly modifier here
|
|
113
92
|
// support for builtin readonly tracked here:
|
|
114
93
|
// https://github.com/arktypeio/arktype/issues/808
|
|
115
|
-
-readonly [k in keyof def as nonOptionalKeyFrom<
|
|
116
|
-
k,
|
|
94
|
+
-readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: inferDefinition<
|
|
117
95
|
def[k],
|
|
118
96
|
$,
|
|
119
97
|
args
|
|
120
|
-
>
|
|
98
|
+
>
|
|
121
99
|
} & {
|
|
122
|
-
-readonly [k in keyof def as optionalKeyFrom<k
|
|
123
|
-
def[k]
|
|
100
|
+
-readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<
|
|
101
|
+
def[k],
|
|
124
102
|
$,
|
|
125
103
|
args
|
|
126
104
|
>
|
|
127
105
|
}
|
|
128
106
|
|
|
129
|
-
export type inferObjectLiteral<def extends object, $, args> =
|
|
130
|
-
"..." extends keyof def
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
107
|
+
export type inferObjectLiteral<def extends object, $, args> = show<
|
|
108
|
+
"..." extends keyof def ?
|
|
109
|
+
merge<
|
|
110
|
+
inferDefinition<def["..."], $, args>,
|
|
111
|
+
inferObjectLiteralInner<def, $, args>
|
|
112
|
+
>
|
|
113
|
+
: inferObjectLiteralInner<def, $, args>
|
|
136
114
|
>
|
|
137
115
|
|
|
138
116
|
export type validateObjectLiteral<def, $, args> = {
|
|
139
|
-
[k in keyof def]: k extends
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
117
|
+
[k in keyof def]: k extends IndexKey<infer indexDef> ?
|
|
118
|
+
validateString<indexDef, $, args> extends ErrorMessage<infer message> ?
|
|
119
|
+
// add a nominal type here to avoid allowing the error message as input
|
|
120
|
+
indexParseError<message>
|
|
121
|
+
: inferDefinition<indexDef, $, args> extends PropertyKey ?
|
|
122
|
+
// if the indexDef is syntactically and semantically valid,
|
|
123
|
+
// move on to the validating the value definition
|
|
124
|
+
validateDefinition<def[k], $, args>
|
|
125
|
+
: indexParseError<writeInvalidPropertyKeyMessage<indexDef>>
|
|
126
|
+
: k extends "..." ?
|
|
127
|
+
inferDefinition<def[k], $, args> extends object ?
|
|
128
|
+
validateDefinition<def[k], $, args>
|
|
129
|
+
: indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>>
|
|
130
|
+
: validateDefinition<def[k], $, args>
|
|
153
131
|
}
|
|
154
132
|
|
|
155
|
-
type nonOptionalKeyFrom<k
|
|
156
|
-
k,
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
133
|
+
type nonOptionalKeyFrom<k, $, args> =
|
|
134
|
+
parseKey<k> extends PreparsedKey<"required", infer inner> ? inner
|
|
135
|
+
: parseKey<k> extends PreparsedKey<"index", infer inner> ?
|
|
136
|
+
inferDefinition<inner, $, args> & Key
|
|
137
|
+
: // spread key is handled at the type root so is handled neither here nor in optionalKeyFrom
|
|
138
|
+
never
|
|
139
|
+
|
|
140
|
+
type optionalKeyFrom<k> =
|
|
141
|
+
parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never
|
|
142
|
+
|
|
143
|
+
type PreparsedKey<
|
|
144
|
+
kind extends ParsedKeyKind = ParsedKeyKind,
|
|
145
|
+
inner extends Key = Key
|
|
146
|
+
> = {
|
|
147
|
+
kind: kind
|
|
148
|
+
inner: inner
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
namespace PreparsedKey {
|
|
152
|
+
export type from<t extends PreparsedKey> = t
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
type ParsedKeyKind = "required" | "optional" | "index" | "spread"
|
|
156
|
+
|
|
157
|
+
export type IndexKey<def extends string = string> = `[${def}]`
|
|
158
|
+
|
|
159
|
+
type PreparsedEntry = PreparsedKey & { value: unknown }
|
|
160
|
+
|
|
161
|
+
export const parseEntry = (entry: readonly [Key, unknown]): PreparsedEntry =>
|
|
162
|
+
Object.assign(parseKey(entry[0]), { value: entry[1] })
|
|
163
|
+
|
|
164
|
+
const parseKey = (key: Key): PreparsedKey =>
|
|
165
|
+
typeof key === "symbol" ? { inner: key, kind: "required" }
|
|
166
|
+
: key.at(-1) === "?" ?
|
|
167
|
+
key.at(-2) === Scanner.escapeToken ?
|
|
168
|
+
{ inner: `${key.slice(0, -2)}?`, kind: "required" }
|
|
169
|
+
: {
|
|
170
|
+
inner: key.slice(0, -1),
|
|
171
|
+
kind: "optional"
|
|
172
|
+
}
|
|
173
|
+
: key[0] === "[" && key.at(-1) === "]" ?
|
|
174
|
+
{ inner: key.slice(1, -1), kind: "index" }
|
|
175
|
+
: key === "..." ? { inner: "...", kind: "spread" }
|
|
176
|
+
: { inner: key === "\\..." ? "..." : key, kind: "required" }
|
|
177
|
+
|
|
178
|
+
type parseKey<k> =
|
|
179
|
+
k extends `${infer inner}?` ?
|
|
180
|
+
inner extends `${infer baseName}${Scanner.EscapeToken}` ?
|
|
181
|
+
PreparsedKey.from<{
|
|
182
|
+
kind: "required"
|
|
183
|
+
inner: `${baseName}?`
|
|
184
|
+
}>
|
|
185
|
+
: PreparsedKey.from<{
|
|
186
|
+
kind: "optional"
|
|
187
|
+
inner: inner
|
|
188
|
+
}>
|
|
189
|
+
: k extends "..." ? PreparsedKey.from<{ kind: "spread"; inner: "..." }>
|
|
190
|
+
: k extends `${Scanner.EscapeToken}...` ?
|
|
191
|
+
PreparsedKey.from<{ kind: "required"; inner: "..." }>
|
|
192
|
+
: k extends IndexKey<infer def> ?
|
|
193
|
+
PreparsedKey.from<{
|
|
194
|
+
kind: "index"
|
|
195
|
+
inner: def
|
|
196
|
+
}>
|
|
197
|
+
: PreparsedKey.from<{
|
|
198
|
+
kind: "required"
|
|
199
|
+
inner: k extends (
|
|
200
|
+
`${Scanner.EscapeToken}${infer escapedIndexKey extends IndexKey}`
|
|
201
|
+
) ?
|
|
202
|
+
escapedIndexKey
|
|
203
|
+
: k extends Key ? k
|
|
204
|
+
: `${k & number}`
|
|
205
|
+
}>
|
|
172
206
|
|
|
173
207
|
declare const indexParseSymbol: unique symbol
|
|
174
208
|
|
|
@@ -176,14 +210,6 @@ export type indexParseError<message extends string = string> = {
|
|
|
176
210
|
[indexParseSymbol]: message
|
|
177
211
|
}
|
|
178
212
|
|
|
179
|
-
export const writeInvalidPropertyKeyMessage = <indexDef extends string>(
|
|
180
|
-
indexDef: indexDef
|
|
181
|
-
): writeInvalidPropertyKeyMessage<indexDef> =>
|
|
182
|
-
`Indexed key definition '${indexDef}' must be a string, number or symbol`
|
|
183
|
-
|
|
184
|
-
type writeInvalidPropertyKeyMessage<indexDef extends string> =
|
|
185
|
-
`Indexed key definition '${indexDef}' must be a string, number or symbol`
|
|
186
|
-
|
|
187
213
|
export const writeInvalidSpreadTypeMessage = <def extends string>(
|
|
188
214
|
def: def
|
|
189
215
|
): writeInvalidSpreadTypeMessage<def> =>
|