arktype 1.1.0-dev-2 → 2.0.0-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/array.test.ts +272 -0
- package/__tests__/astUtils.test.ts +17 -0
- package/__tests__/badDefinitionType.test.ts +51 -0
- package/__tests__/basis.test.ts +46 -0
- package/__tests__/bounds.test.ts +388 -0
- package/__tests__/cast.test.ts +32 -0
- package/__tests__/completions.test.ts +46 -0
- package/__tests__/config.test.ts +168 -0
- package/__tests__/cyclic.test.ts +144 -0
- package/__tests__/dateLiteral.test.ts +32 -0
- package/__tests__/declared.test.ts +123 -0
- package/__tests__/discrimination.test.ts +74 -0
- package/__tests__/divisor.test.ts +90 -0
- package/__tests__/enclosed.test.ts +93 -0
- package/__tests__/examples.test.ts +56 -0
- package/__tests__/expressions.test.ts +138 -0
- package/__tests__/generateBenchData.ts +89 -0
- package/__tests__/generated/cyclic.ts +3058 -0
- package/__tests__/generic.test.ts +294 -0
- package/__tests__/group.test.ts +72 -0
- package/__tests__/hkt.test.ts +22 -0
- package/__tests__/imports.test.ts +113 -0
- package/__tests__/instanceof.test.ts +102 -0
- package/__tests__/intersection.test.ts +104 -0
- package/__tests__/jsdoc.test.ts +64 -0
- package/__tests__/keyTraversal.test.ts +73 -0
- package/__tests__/keyof.test.ts +134 -0
- package/__tests__/keywords.test.ts +188 -0
- package/__tests__/literal.test.ts +51 -0
- package/__tests__/match.test.ts +34 -0
- package/__tests__/methods.bench.ts +22 -0
- package/__tests__/morph.test.ts +409 -0
- package/__tests__/narrow.test.ts +102 -0
- package/__tests__/nodeDefinitions.test.ts +190 -0
- package/__tests__/object.bench.ts +28 -0
- package/__tests__/objectLiteral.test.ts +300 -0
- package/__tests__/operand.bench.ts +45 -0
- package/__tests__/operator.bench.ts +130 -0
- package/__tests__/regex.test.ts +58 -0
- package/__tests__/scope.bench.ts +39 -0
- package/__tests__/scope.test.ts +140 -0
- package/__tests__/string.test.ts +48 -0
- package/__tests__/submodule.test.ts +99 -0
- package/__tests__/this.test.ts +57 -0
- package/__tests__/thunk.test.ts +80 -0
- package/__tests__/traverse.test.ts +119 -0
- package/__tests__/tsGenerics.test.ts +12 -0
- package/__tests__/typeMethods.test.ts +69 -0
- package/__tests__/typeReference.test.ts +39 -0
- package/__tests__/unenclosed.test.ts +120 -0
- package/__tests__/union.test.ts +147 -0
- package/ark.ts +91 -0
- package/main.ts +7 -9
- package/match.ts +47 -68
- package/out/ark.d.ts +52 -0
- package/out/ark.d.ts.map +1 -0
- package/out/ark.js +19 -0
- package/out/ark.js.map +1 -0
- package/out/arkConfig.d.ts +2 -0
- package/out/arkConfig.d.ts.map +1 -0
- package/out/arkConfig.js +10 -0
- package/out/arkConfig.js.map +1 -0
- package/out/main.d.ts +7 -0
- package/out/main.d.ts.map +1 -0
- package/out/main.js +4 -0
- package/out/main.js.map +1 -0
- package/out/match.d.ts +48 -0
- package/out/match.d.ts.map +1 -0
- package/{dist → out}/match.js +0 -5
- package/out/match.js.map +1 -0
- package/{dist → out}/parser/definition.d.ts +3 -3
- package/out/parser/definition.d.ts.map +1 -0
- package/{dist → out}/parser/definition.js +8 -5
- package/out/parser/definition.js.map +1 -0
- package/out/parser/generic.d.ts +18 -0
- package/out/parser/generic.d.ts.map +1 -0
- package/out/parser/generic.js +19 -0
- package/out/parser/generic.js.map +1 -0
- package/{dist → out}/parser/objectLiteral.d.ts +14 -5
- package/out/parser/objectLiteral.d.ts.map +1 -0
- package/out/parser/objectLiteral.js +66 -0
- package/out/parser/objectLiteral.js.map +1 -0
- package/out/parser/semantic/bounds.d.ts +13 -0
- package/out/parser/semantic/bounds.d.ts.map +1 -0
- package/{dist → out}/parser/semantic/bounds.js.map +1 -1
- package/{dist → out}/parser/semantic/divisor.d.ts +2 -2
- package/{dist → out}/parser/semantic/divisor.d.ts.map +1 -1
- package/{dist → out}/parser/semantic/intersections.d.ts +1 -2
- package/out/parser/semantic/intersections.d.ts.map +1 -0
- package/out/parser/semantic/semantic.d.ts +33 -0
- package/out/parser/semantic/semantic.d.ts.map +1 -0
- package/{dist → out}/parser/semantic/utils.d.ts +1 -1
- package/{dist → out}/parser/semantic/utils.d.ts.map +1 -1
- package/{dist → out}/parser/semantic/validate.d.ts +3 -3
- package/out/parser/semantic/validate.d.ts.map +1 -0
- package/{dist → out}/parser/semantic/validate.js.map +1 -1
- package/{dist → out}/parser/shared.d.ts +7 -1
- package/out/parser/shared.d.ts.map +1 -0
- package/{dist → out}/parser/shared.js +7 -1
- package/out/parser/shared.js.map +1 -0
- package/{dist → out}/parser/string/reduce/dynamic.d.ts +11 -9
- package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
- package/{dist → out}/parser/string/reduce/dynamic.js +13 -2
- package/out/parser/string/reduce/dynamic.js.map +1 -0
- package/{dist → out}/parser/string/reduce/shared.d.ts +29 -2
- package/{dist → out}/parser/string/reduce/shared.d.ts.map +1 -1
- package/{dist → out}/parser/string/reduce/shared.js +20 -1
- package/out/parser/string/reduce/shared.js.map +1 -0
- package/{dist → out}/parser/string/reduce/static.d.ts +1 -2
- package/out/parser/string/reduce/static.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operand/date.js.map +1 -1
- package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +2 -2
- package/{dist → out}/parser/string/shift/operand/enclosed.d.ts.map +1 -1
- package/{dist → out}/parser/string/shift/operand/enclosed.js +4 -4
- package/out/parser/string/shift/operand/enclosed.js.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.js +25 -0
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
- package/{dist → out}/parser/string/shift/operand/operand.d.ts.map +1 -1
- package/{dist → out}/parser/string/shift/operand/operand.js.map +1 -1
- package/{dist → out}/parser/string/shift/operand/unenclosed.d.ts +7 -9
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operand/unenclosed.js +10 -10
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
- package/{dist → out}/parser/string/shift/operator/bounds.d.ts +11 -33
- package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
- package/out/parser/string/shift/operator/bounds.js +106 -0
- package/out/parser/string/shift/operator/bounds.js.map +1 -0
- package/{dist → out}/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operator/operator.js.map +1 -1
- package/{dist → out}/parser/string/shift/scanner.d.ts +1 -1
- package/{dist → out}/parser/string/shift/scanner.d.ts.map +1 -1
- package/out/parser/string/shift/scanner.js.map +1 -0
- package/{dist → out}/parser/string/string.d.ts +9 -11
- package/out/parser/string/string.d.ts.map +1 -0
- package/out/parser/string/string.js +23 -0
- package/out/parser/string/string.js.map +1 -0
- package/{dist → out}/parser/tuple.d.ts +9 -11
- package/out/parser/tuple.d.ts.map +1 -0
- package/{dist → out}/parser/tuple.js +17 -14
- package/out/parser/tuple.js.map +1 -0
- package/{dist → out}/scope.d.ts +16 -16
- package/out/scope.d.ts.map +1 -0
- package/{dist → out}/scope.js +27 -22
- package/out/scope.js.map +1 -0
- package/out/test.d.ts +2 -0
- package/out/test.d.ts.map +1 -0
- package/out/test.js +7 -0
- package/out/test.js.map +1 -0
- package/out/tsconfig.build.tsbuildinfo +1 -0
- package/{dist → out}/type.d.ts +23 -47
- package/out/type.d.ts.map +1 -0
- package/{dist → out}/type.js +27 -31
- package/out/type.js.map +1 -0
- package/out/util.d.ts +19 -0
- package/out/util.d.ts.map +1 -0
- package/out/util.js +7 -0
- package/out/util.js.map +1 -0
- package/package.json +13 -15
- package/parser/definition.ts +58 -55
- package/parser/generic.ts +17 -157
- package/parser/objectLiteral.ts +107 -30
- package/parser/semantic/bounds.ts +22 -24
- package/parser/semantic/divisor.ts +2 -2
- package/parser/semantic/intersections.ts +17 -18
- package/parser/semantic/semantic.ts +80 -43
- package/parser/semantic/utils.ts +7 -7
- package/parser/semantic/validate.ts +40 -36
- package/parser/shared.ts +28 -17
- package/parser/string/reduce/dynamic.ts +26 -11
- package/parser/string/reduce/shared.ts +35 -7
- package/parser/string/reduce/static.ts +20 -22
- package/parser/string/shift/operand/date.ts +1 -4
- package/parser/string/shift/operand/enclosed.ts +5 -6
- package/parser/string/shift/operand/genericArgs.ts +127 -0
- package/parser/string/shift/operand/operand.ts +21 -21
- package/parser/string/shift/operand/unenclosed.ts +54 -68
- package/parser/string/shift/operator/bounds.ts +99 -64
- package/parser/string/shift/operator/operator.ts +31 -31
- package/parser/string/shift/scanner.ts +7 -7
- package/parser/string/string.ts +29 -32
- package/parser/tuple.ts +87 -88
- package/scope.ts +81 -80
- package/type.ts +76 -132
- package/util.ts +36 -0
- package/dist/main.d.ts +0 -11
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -8
- package/dist/main.js.map +0 -1
- package/dist/match.d.ts +0 -46
- package/dist/match.d.ts.map +0 -1
- package/dist/match.js.map +0 -1
- package/dist/parser/definition.d.ts.map +0 -1
- package/dist/parser/definition.js.map +0 -1
- package/dist/parser/generic.d.ts +0 -44
- package/dist/parser/generic.d.ts.map +0 -1
- package/dist/parser/generic.js +0 -46
- package/dist/parser/generic.js.map +0 -1
- package/dist/parser/objectLiteral.d.ts.map +0 -1
- package/dist/parser/objectLiteral.js +0 -38
- package/dist/parser/objectLiteral.js.map +0 -1
- package/dist/parser/semantic/bounds.d.ts +0 -13
- package/dist/parser/semantic/bounds.d.ts.map +0 -1
- package/dist/parser/semantic/intersections.d.ts.map +0 -1
- package/dist/parser/semantic/semantic.d.ts +0 -22
- package/dist/parser/semantic/semantic.d.ts.map +0 -1
- package/dist/parser/semantic/validate.d.ts.map +0 -1
- package/dist/parser/shared.d.ts.map +0 -1
- package/dist/parser/shared.js.map +0 -1
- package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/dist/parser/string/reduce/dynamic.js.map +0 -1
- package/dist/parser/string/reduce/shared.js.map +0 -1
- package/dist/parser/string/reduce/static.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/dist/parser/string/shift/operator/bounds.js +0 -95
- package/dist/parser/string/shift/operator/bounds.js.map +0 -1
- package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/dist/parser/string/shift/scanner.js.map +0 -1
- package/dist/parser/string/string.d.ts.map +0 -1
- package/dist/parser/string/string.js +0 -31
- package/dist/parser/string/string.js.map +0 -1
- package/dist/parser/tuple.d.ts.map +0 -1
- package/dist/parser/tuple.js.map +0 -1
- package/dist/scope.d.ts.map +0 -1
- package/dist/scope.js.map +0 -1
- package/dist/scopes/ark.d.ts +0 -38
- package/dist/scopes/ark.d.ts.map +0 -1
- package/dist/scopes/ark.js +0 -24
- package/dist/scopes/ark.js.map +0 -1
- package/dist/scopes/jsObjects.d.ts +0 -19
- package/dist/scopes/jsObjects.d.ts.map +0 -1
- package/dist/scopes/jsObjects.js +0 -15
- package/dist/scopes/jsObjects.js.map +0 -1
- package/dist/scopes/parsing.d.ts +0 -18
- package/dist/scopes/parsing.d.ts.map +0 -1
- package/dist/scopes/parsing.js +0 -61
- package/dist/scopes/parsing.js.map +0 -1
- package/dist/scopes/tsGenerics.d.ts +0 -17
- package/dist/scopes/tsGenerics.d.ts.map +0 -1
- package/dist/scopes/tsGenerics.js +0 -13
- package/dist/scopes/tsGenerics.js.map +0 -1
- package/dist/scopes/tsKeywords.d.ts +0 -24
- package/dist/scopes/tsKeywords.d.ts.map +0 -1
- package/dist/scopes/tsKeywords.js +0 -20
- package/dist/scopes/tsKeywords.js.map +0 -1
- package/dist/scopes/utils/creditCard.d.ts +0 -3
- package/dist/scopes/utils/creditCard.d.ts.map +0 -1
- package/dist/scopes/utils/creditCard.js +0 -36
- package/dist/scopes/utils/creditCard.js.map +0 -1
- package/dist/scopes/utils/date.d.ts +0 -24
- package/dist/scopes/utils/date.d.ts.map +0 -1
- package/dist/scopes/utils/date.js +0 -53
- package/dist/scopes/utils/date.js.map +0 -1
- package/dist/scopes/validation.d.ts +0 -20
- package/dist/scopes/validation.d.ts.map +0 -1
- package/dist/scopes/validation.js +0 -57
- package/dist/scopes/validation.js.map +0 -1
- package/dist/type.d.ts.map +0 -1
- package/dist/type.js.map +0 -1
- package/scopes/ark.ts +0 -64
- package/scopes/jsObjects.ts +0 -29
- package/scopes/parsing.ts +0 -81
- package/scopes/tsGenerics.ts +0 -28
- package/scopes/tsKeywords.ts +0 -39
- package/scopes/utils/creditCard.ts +0 -37
- package/scopes/utils/date.ts +0 -108
- package/scopes/validation.ts +0 -81
- /package/{dist → out}/parser/semantic/bounds.js +0 -0
- /package/{dist → out}/parser/semantic/divisor.js +0 -0
- /package/{dist → out}/parser/semantic/divisor.js.map +0 -0
- /package/{dist → out}/parser/semantic/intersections.js +0 -0
- /package/{dist → out}/parser/semantic/intersections.js.map +0 -0
- /package/{dist → out}/parser/semantic/semantic.js +0 -0
- /package/{dist → out}/parser/semantic/semantic.js.map +0 -0
- /package/{dist → out}/parser/semantic/utils.js +0 -0
- /package/{dist → out}/parser/semantic/utils.js.map +0 -0
- /package/{dist → out}/parser/semantic/validate.js +0 -0
- /package/{dist → out}/parser/string/reduce/static.js +0 -0
- /package/{dist → out}/parser/string/reduce/static.js.map +0 -0
- /package/{dist → out}/parser/string/shift/operand/date.js +0 -0
- /package/{dist → out}/parser/string/shift/operand/operand.d.ts +0 -0
- /package/{dist → out}/parser/string/shift/operand/operand.js +0 -0
- /package/{dist → out}/parser/string/shift/operator/divisor.d.ts +0 -0
- /package/{dist → out}/parser/string/shift/operator/divisor.d.ts.map +0 -0
- /package/{dist → out}/parser/string/shift/operator/divisor.js +0 -0
- /package/{dist → out}/parser/string/shift/operator/operator.d.ts +0 -0
- /package/{dist → out}/parser/string/shift/operator/operator.js +0 -0
- /package/{dist → out}/parser/string/shift/scanner.js +0 -0
package/out/util.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Module, Scope } from "./scope.js";
|
|
2
|
+
import type { Generic, Type } from "./type.js";
|
|
3
|
+
export declare const arkKind: unique symbol;
|
|
4
|
+
export type ArkKinds = {
|
|
5
|
+
type: Type;
|
|
6
|
+
scope: Scope;
|
|
7
|
+
generic: Generic;
|
|
8
|
+
module: Module;
|
|
9
|
+
};
|
|
10
|
+
export type ArkKind = keyof ArkKinds;
|
|
11
|
+
export declare const addArkKind: <kind extends keyof ArkKinds>(value: Omit<ArkKinds[kind], typeof arkKind> & {
|
|
12
|
+
[arkKind]?: kind;
|
|
13
|
+
}, kind: kind) => ArkKinds[kind];
|
|
14
|
+
export type addArkKind<kind extends ArkKind, t extends Omit<ArkKinds[kind], arkKind>> = t & {
|
|
15
|
+
[arkKind]: kind;
|
|
16
|
+
};
|
|
17
|
+
export type arkKind = typeof arkKind;
|
|
18
|
+
export declare const hasArkKind: <kind extends keyof ArkKinds>(value: unknown, kind: kind) => value is ArkKinds[kind];
|
|
19
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAC/C,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAE9C,eAAO,MAAM,OAAO,eAAgC,CAAA;AAEpD,MAAM,MAAM,QAAQ,GAAG;IACtB,IAAI,EAAE,IAAI,CAAA;IACV,KAAK,EAAE,KAAK,CAAA;IACZ,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;CACd,CAAA;AAED,MAAM,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAA;AAEpC,eAAO,MAAM,UAAU;;gCASX,CAAA;AAEZ,MAAM,MAAM,UAAU,CACrB,IAAI,SAAS,OAAO,EACpB,CAAC,SAAS,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,IACpC,CAAC,GAAG;IAAE,CAAC,OAAO,CAAC,EAAE,IAAI,CAAA;CAAE,CAAA;AAE3B,MAAM,MAAM,OAAO,GAAG,OAAO,OAAO,CAAA;AAEpC,eAAO,MAAM,UAAU,uCACf,OAAO,wCAEiD,CAAA"}
|
package/out/util.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const arkKind = Symbol("ArkTypeInternalKind");
|
|
2
|
+
export const addArkKind = (value, kind) => Object.defineProperty(value, arkKind, {
|
|
3
|
+
value: kind,
|
|
4
|
+
enumerable: false
|
|
5
|
+
});
|
|
6
|
+
export const hasArkKind = (value, kind) => value?.[arkKind] === kind;
|
|
7
|
+
//# sourceMappingURL=util.js.map
|
package/out/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../util.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,OAAO,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAA;AAWpD,MAAM,CAAC,MAAM,UAAU,GAAG,CACzB,KAEC,EACD,IAAU,EACO,EAAE,CACnB,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE;IACrC,KAAK,EAAE,IAAI;IACX,UAAU,EAAE,KAAK;CACjB,CAAU,CAAA;AASZ,MAAM,CAAC,MAAM,UAAU,GAAG,CACzB,KAAc,EACd,IAAU,EACgB,EAAE,CAAE,KAAa,EAAE,CAAC,OAAO,CAAC,KAAK,IAAI,CAAA"}
|
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": "
|
|
4
|
+
"version": "2.0.0-dev.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "David Blass",
|
|
@@ -13,30 +13,28 @@
|
|
|
13
13
|
"url": "https://github.com/arktypeio/arktype.git"
|
|
14
14
|
},
|
|
15
15
|
"type": "module",
|
|
16
|
+
"main": "./out/main.js",
|
|
17
|
+
"types": "./out/main.d.ts",
|
|
16
18
|
"exports": {
|
|
17
19
|
".": {
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"default": "./dist/main.js"
|
|
20
|
+
"types": "./out/main.d.ts",
|
|
21
|
+
"default": "./out/main.js"
|
|
21
22
|
},
|
|
22
23
|
"./internal/*": {
|
|
23
|
-
"
|
|
24
|
-
"types": "./dist/*",
|
|
25
|
-
"default": "./dist/*"
|
|
24
|
+
"default": "./out/*"
|
|
26
25
|
}
|
|
27
26
|
},
|
|
28
27
|
"files": [
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"!__tests__"
|
|
28
|
+
"out",
|
|
29
|
+
"!__tests__",
|
|
30
|
+
"**/*.ts"
|
|
33
31
|
],
|
|
34
32
|
"scripts": {
|
|
35
|
-
"build": "
|
|
36
|
-
"test": "
|
|
33
|
+
"build": "tsx ../repo/build.ts",
|
|
34
|
+
"test": "tsx ../repo/testPackage.ts"
|
|
37
35
|
},
|
|
38
36
|
"dependencies": {
|
|
39
|
-
"@arktype/util": "0.0.
|
|
40
|
-
"@arktype/schema": "0.0.
|
|
37
|
+
"@arktype/util": "0.0.17",
|
|
38
|
+
"@arktype/schema": "0.0.3"
|
|
41
39
|
}
|
|
42
40
|
}
|
package/parser/definition.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isNode,
|
|
1
|
+
import { isNode, schema, type TypeNode } from "@arktype/schema"
|
|
2
2
|
import {
|
|
3
3
|
isThunk,
|
|
4
4
|
objectKindOf,
|
|
5
|
-
|
|
5
|
+
printable,
|
|
6
6
|
throwParseError,
|
|
7
7
|
type defined,
|
|
8
8
|
type Dict,
|
|
@@ -17,8 +17,8 @@ import {
|
|
|
17
17
|
type Primitive,
|
|
18
18
|
type requiredKeyOf
|
|
19
19
|
} from "@arktype/util"
|
|
20
|
+
import type { type } from "../ark.js"
|
|
20
21
|
import type { ParseContext } from "../scope.js"
|
|
21
|
-
import type { type } from "../scopes/ark.js"
|
|
22
22
|
import { Type } from "../type.js"
|
|
23
23
|
import {
|
|
24
24
|
parseObjectLiteral,
|
|
@@ -34,18 +34,21 @@ import {
|
|
|
34
34
|
type validateTuple
|
|
35
35
|
} from "./tuple.js"
|
|
36
36
|
|
|
37
|
-
export const parseObject = (def: object, ctx: ParseContext):
|
|
37
|
+
export const parseObject = (def: object, ctx: ParseContext): TypeNode => {
|
|
38
38
|
const objectKind = objectKindOf(def)
|
|
39
39
|
switch (objectKind) {
|
|
40
40
|
case undefined:
|
|
41
|
-
if (
|
|
42
|
-
return def
|
|
41
|
+
if (def instanceof Type) {
|
|
42
|
+
return def.root
|
|
43
|
+
}
|
|
44
|
+
if (isNode(def) && def.isType()) {
|
|
45
|
+
return def
|
|
43
46
|
}
|
|
44
47
|
return parseObjectLiteral(def as Dict, ctx)
|
|
45
48
|
case "Array":
|
|
46
49
|
return parseTuple(def as List, ctx)
|
|
47
50
|
case "RegExp":
|
|
48
|
-
return
|
|
51
|
+
return schema({ basis: "string", pattern: def as RegExp })
|
|
49
52
|
case "Function":
|
|
50
53
|
const resolvedDef = isThunk(def) ? def() : def
|
|
51
54
|
if (resolvedDef instanceof Type) {
|
|
@@ -54,7 +57,7 @@ export const parseObject = (def: object, ctx: ParseContext): Root => {
|
|
|
54
57
|
return throwParseError(writeBadDefinitionTypeMessage("Function"))
|
|
55
58
|
default:
|
|
56
59
|
return throwParseError(
|
|
57
|
-
writeBadDefinitionTypeMessage(objectKind ??
|
|
60
|
+
writeBadDefinitionTypeMessage(objectKind ?? printable(def))
|
|
58
61
|
)
|
|
59
62
|
}
|
|
60
63
|
}
|
|
@@ -62,36 +65,36 @@ export const parseObject = (def: object, ctx: ParseContext): Root => {
|
|
|
62
65
|
export type inferDefinition<def, $, args> = isAny<def> extends true
|
|
63
66
|
? never
|
|
64
67
|
: def extends type.cast<infer t> | ThunkCast<infer t>
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
68
|
+
? t
|
|
69
|
+
: def extends string
|
|
70
|
+
? inferString<def, $, args>
|
|
71
|
+
: def extends readonly unknown[]
|
|
72
|
+
? inferTuple<def, $, args>
|
|
73
|
+
: def extends RegExp
|
|
74
|
+
? string
|
|
75
|
+
: def extends object
|
|
76
|
+
? inferObjectLiteral<def, $, args>
|
|
77
|
+
: never
|
|
75
78
|
|
|
76
79
|
export type validateDefinition<def, $, args> = null extends undefined
|
|
77
80
|
? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`>
|
|
78
81
|
: [def] extends [Terminal]
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
82
|
+
? unknown extends def
|
|
83
|
+
? // if def is any, never is the only way we can make validation fail
|
|
84
|
+
// since any would be assignable to a standard error message
|
|
85
|
+
never
|
|
86
|
+
: def
|
|
87
|
+
: def extends string
|
|
88
|
+
? validateString<def, $, args>
|
|
89
|
+
: def extends readonly unknown[]
|
|
90
|
+
? validateTuple<def, $, args>
|
|
91
|
+
: def extends BadDefinitionType
|
|
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>
|
|
95
98
|
|
|
96
99
|
export type validateDeclared<declared, def, $, args> =
|
|
97
100
|
def extends validateDefinition<def, $, args>
|
|
@@ -105,27 +108,27 @@ type validateInference<def, declared, $, args> = def extends
|
|
|
105
108
|
| TupleExpression
|
|
106
109
|
? validateShallowInference<def, declared, $, args>
|
|
107
110
|
: def extends readonly unknown[]
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
111
|
+
? declared extends readonly unknown[]
|
|
112
|
+
? {
|
|
113
|
+
[i in keyof declared]: i extends keyof def
|
|
114
|
+
? validateInference<def[i], declared[i], $, args>
|
|
115
|
+
: unknown
|
|
116
|
+
}
|
|
117
|
+
: evaluate<declarationMismatch<def, declared, $, args>>
|
|
118
|
+
: def extends object
|
|
119
|
+
? evaluate<
|
|
120
|
+
{
|
|
121
|
+
[k in requiredKeyOf<declared>]: k extends keyof def
|
|
122
|
+
? validateInference<def[k], declared[k], $, args>
|
|
123
|
+
: unknown
|
|
124
|
+
} & {
|
|
125
|
+
[k in optionalKeyOf<declared> &
|
|
126
|
+
string as `${k}?`]: `${k}?` extends keyof def
|
|
127
|
+
? validateInference<def[`${k}?`], defined<declared[k]>, $, args>
|
|
128
|
+
: unknown
|
|
129
|
+
}
|
|
130
|
+
>
|
|
131
|
+
: validateShallowInference<def, declared, $, args>
|
|
129
132
|
|
|
130
133
|
type validateShallowInference<def, declared, $, args> = equals<
|
|
131
134
|
inferDefinition<def, $, args>,
|
package/parser/generic.ts
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {
|
|
3
|
-
throwParseError,
|
|
4
|
-
type ErrorMessage,
|
|
5
|
-
type join,
|
|
6
|
-
type nominal
|
|
7
|
-
} from "@arktype/util"
|
|
8
|
-
import type { ParseContext } from "../scope.js"
|
|
9
|
-
import { DynamicState } from "./string/reduce/dynamic.js"
|
|
10
|
-
import { writeUnclosedGroupMessage } from "./string/reduce/shared.js"
|
|
11
|
-
import type { StaticState, state } from "./string/reduce/static.js"
|
|
1
|
+
import { throwParseError, type nominal } from "@arktype/util"
|
|
12
2
|
import { writeUnexpectedCharacterMessage } from "./string/shift/operator/operator.js"
|
|
13
3
|
import { Scanner } from "./string/shift/scanner.js"
|
|
14
|
-
import { parseUntilFinalizer } from "./string/string.js"
|
|
15
4
|
|
|
16
5
|
export type GenericDeclaration<
|
|
17
6
|
name extends string = string,
|
|
@@ -50,8 +39,8 @@ const parseGenericParamsRecurse = (scanner: Scanner): string[] => {
|
|
|
50
39
|
return nextNonWhitespace === ""
|
|
51
40
|
? [param]
|
|
52
41
|
: nextNonWhitespace === ","
|
|
53
|
-
|
|
54
|
-
|
|
42
|
+
? [param, ...parseGenericParamsRecurse(scanner)]
|
|
43
|
+
: throwParseError(writeUnexpectedCharacterMessage(nextNonWhitespace, ","))
|
|
55
44
|
}
|
|
56
45
|
|
|
57
46
|
type parseParamsRecurse<
|
|
@@ -62,147 +51,18 @@ type parseParamsRecurse<
|
|
|
62
51
|
? lookahead extends ","
|
|
63
52
|
? parseParamsRecurse<nextUnscanned, "", [...result, param]>
|
|
64
53
|
: lookahead extends Scanner.WhiteSpaceToken
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
54
|
+
? param extends ""
|
|
55
|
+
? // if the next char is whitespace and we aren't in the middle of a param, skip to the next one
|
|
56
|
+
parseParamsRecurse<Scanner.skipWhitespace<nextUnscanned>, "", result>
|
|
57
|
+
: Scanner.skipWhitespace<nextUnscanned> extends `${infer nextNonWhitespace}${infer rest}`
|
|
58
|
+
? nextNonWhitespace extends ","
|
|
59
|
+
? parseParamsRecurse<rest, "", [...result, param]>
|
|
60
|
+
: GenericParamsParseError<
|
|
61
|
+
writeUnexpectedCharacterMessage<nextNonWhitespace, ",">
|
|
62
|
+
>
|
|
63
|
+
: // params end with a single whitespace character, add the current token
|
|
64
|
+
[...result, param]
|
|
65
|
+
: parseParamsRecurse<nextUnscanned, `${param}${lookahead}`, result>
|
|
77
66
|
: param extends ""
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
export type ParsedArgs<
|
|
82
|
-
result extends unknown[] = unknown[],
|
|
83
|
-
unscanned extends string = string
|
|
84
|
-
> = {
|
|
85
|
-
result: result
|
|
86
|
-
unscanned: unscanned
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export const parseGenericArgs = (
|
|
90
|
-
name: string,
|
|
91
|
-
params: string[],
|
|
92
|
-
unscanned: string,
|
|
93
|
-
ctx: ParseContext
|
|
94
|
-
) => parseGenericArgsRecurse(name, params, unscanned, ctx, [], [])
|
|
95
|
-
|
|
96
|
-
export type parseGenericArgs<
|
|
97
|
-
name extends string,
|
|
98
|
-
params extends string[],
|
|
99
|
-
unscanned extends string,
|
|
100
|
-
$,
|
|
101
|
-
args
|
|
102
|
-
> = parseGenericArgsRecurse<name, params, unscanned, $, args, [], []>
|
|
103
|
-
|
|
104
|
-
const parseGenericArgsRecurse = (
|
|
105
|
-
name: string,
|
|
106
|
-
params: string[],
|
|
107
|
-
unscanned: string,
|
|
108
|
-
ctx: ParseContext,
|
|
109
|
-
argDefs: string[],
|
|
110
|
-
argNodes: Root[]
|
|
111
|
-
): ParsedArgs<Root[]> => {
|
|
112
|
-
const s = parseUntilFinalizer(new DynamicState(unscanned, ctx))
|
|
113
|
-
// remove the finalizing token from the argDef
|
|
114
|
-
argDefs.push(s.scanner.scanned.slice(0, -1))
|
|
115
|
-
argNodes.push(s.root)
|
|
116
|
-
const nextUnscanned = s.scanner.unscanned
|
|
117
|
-
if (s.finalizer === ">") {
|
|
118
|
-
if (argNodes.length === params.length) {
|
|
119
|
-
return {
|
|
120
|
-
result: argNodes,
|
|
121
|
-
unscanned: nextUnscanned
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
return s.error(writeInvalidGenericArgsMessage(name, params, argDefs))
|
|
125
|
-
}
|
|
126
|
-
} else if (s.finalizer === ",") {
|
|
127
|
-
return parseGenericArgsRecurse(
|
|
128
|
-
name,
|
|
129
|
-
params,
|
|
130
|
-
nextUnscanned,
|
|
131
|
-
ctx,
|
|
132
|
-
argDefs,
|
|
133
|
-
argNodes
|
|
134
|
-
)
|
|
135
|
-
}
|
|
136
|
-
return s.error(writeUnclosedGroupMessage(">"))
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
type parseGenericArgsRecurse<
|
|
140
|
-
name extends string,
|
|
141
|
-
params extends string[],
|
|
142
|
-
unscanned extends string,
|
|
143
|
-
$,
|
|
144
|
-
args,
|
|
145
|
-
argDefs extends string[],
|
|
146
|
-
argAsts extends unknown[]
|
|
147
|
-
> = parseUntilFinalizer<
|
|
148
|
-
state.initialize<unscanned>,
|
|
149
|
-
$,
|
|
150
|
-
args
|
|
151
|
-
> extends infer finalArgState extends StaticState
|
|
152
|
-
? {
|
|
153
|
-
defs: [
|
|
154
|
-
...argDefs,
|
|
155
|
-
finalArgState["scanned"] extends `${infer def}${"," | ">"}`
|
|
156
|
-
? def
|
|
157
|
-
: finalArgState["scanned"]
|
|
158
|
-
]
|
|
159
|
-
asts: [...argAsts, finalArgState["root"]]
|
|
160
|
-
unscanned: finalArgState["unscanned"]
|
|
161
|
-
} extends {
|
|
162
|
-
defs: infer nextDefs extends string[]
|
|
163
|
-
asts: infer nextAsts extends unknown[]
|
|
164
|
-
unscanned: infer nextUnscanned extends string
|
|
165
|
-
}
|
|
166
|
-
? finalArgState["finalizer"] extends ">"
|
|
167
|
-
? nextAsts["length"] extends params["length"]
|
|
168
|
-
? ParsedArgs<nextAsts, nextUnscanned>
|
|
169
|
-
: state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>>
|
|
170
|
-
: finalArgState["finalizer"] extends ","
|
|
171
|
-
? parseGenericArgsRecurse<
|
|
172
|
-
name,
|
|
173
|
-
params,
|
|
174
|
-
nextUnscanned,
|
|
175
|
-
$,
|
|
176
|
-
args,
|
|
177
|
-
nextDefs,
|
|
178
|
-
nextAsts
|
|
179
|
-
>
|
|
180
|
-
: finalArgState["finalizer"] extends ErrorMessage
|
|
181
|
-
? finalArgState
|
|
182
|
-
: state.error<writeUnclosedGroupMessage<">">>
|
|
183
|
-
: never
|
|
184
|
-
: never
|
|
185
|
-
|
|
186
|
-
export const writeInvalidGenericArgsMessage = <
|
|
187
|
-
name extends string,
|
|
188
|
-
params extends string[],
|
|
189
|
-
argDefs extends string[]
|
|
190
|
-
>(
|
|
191
|
-
name: name,
|
|
192
|
-
params: params,
|
|
193
|
-
argDefs: argDefs
|
|
194
|
-
) =>
|
|
195
|
-
`${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${
|
|
196
|
-
argDefs.length
|
|
197
|
-
}${argDefs.length === 0 ? "" : ": " + argDefs.join(", ")})`
|
|
198
|
-
|
|
199
|
-
export type writeInvalidGenericArgsMessage<
|
|
200
|
-
name extends string,
|
|
201
|
-
params extends string[],
|
|
202
|
-
argDefs extends string[]
|
|
203
|
-
> = `${name}<${join<
|
|
204
|
-
params,
|
|
205
|
-
", "
|
|
206
|
-
>}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends 0
|
|
207
|
-
? ""
|
|
208
|
-
: `: ${join<argDefs, ",">}`})`
|
|
67
|
+
? result
|
|
68
|
+
: [...result, param]
|
package/parser/objectLiteral.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { keywords, schema, type Inner, type TypeNode } from "@arktype/schema"
|
|
2
2
|
import {
|
|
3
|
-
|
|
3
|
+
printable,
|
|
4
|
+
throwParseError,
|
|
4
5
|
type Dict,
|
|
5
6
|
type ErrorMessage,
|
|
6
|
-
type evaluate
|
|
7
|
+
type evaluate,
|
|
8
|
+
type merge
|
|
7
9
|
} from "@arktype/util"
|
|
10
|
+
|
|
8
11
|
import type { ParseContext } from "../scope.js"
|
|
9
12
|
import type { inferDefinition, validateDefinition } from "./definition.js"
|
|
13
|
+
import type { astToString } from "./semantic/utils.js"
|
|
10
14
|
import type { validateString } from "./semantic/validate.js"
|
|
11
15
|
import {
|
|
12
16
|
parseEntry,
|
|
@@ -21,19 +25,66 @@ const stringAndSymbolicEntriesOf = (o: Record<string | symbol, unknown>) => [
|
|
|
21
25
|
...Object.getOwnPropertySymbols(o).map((k) => [k, o[k]] as const)
|
|
22
26
|
]
|
|
23
27
|
|
|
24
|
-
export const parseObjectLiteral = (def: Dict, ctx: ParseContext) => {
|
|
28
|
+
export const parseObjectLiteral = (def: Dict, ctx: ParseContext): TypeNode => {
|
|
25
29
|
const required: Inner<"required">[] = []
|
|
26
30
|
const optional: Inner<"optional">[] = []
|
|
27
|
-
|
|
31
|
+
|
|
32
|
+
// We only allow a spread operator to be used as the first key in an object
|
|
33
|
+
// because to match JS behavior any keys before the spread are overwritten
|
|
34
|
+
// by the values in the target object, so there'd be no useful purpose in having it
|
|
35
|
+
// anywhere except for the beginning.
|
|
36
|
+
// Discussion in ArkType Discord:
|
|
37
|
+
// https://discord.com/channels/957797212103016458/1103023445035462678/1182814502471860334
|
|
38
|
+
let hasSeenFirstKey = false
|
|
39
|
+
|
|
40
|
+
const entries = stringAndSymbolicEntriesOf(def)
|
|
41
|
+
for (const entry of entries) {
|
|
28
42
|
const result = parseEntry(entry)
|
|
43
|
+
|
|
44
|
+
if (result.kind === "spread") {
|
|
45
|
+
if (hasSeenFirstKey) {
|
|
46
|
+
return throwParseError(
|
|
47
|
+
"Spread operator may only be used as the first key in an object"
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const spreadNode = ctx.scope.parse(result.innerValue, ctx)
|
|
52
|
+
|
|
53
|
+
if (
|
|
54
|
+
spreadNode.kind !== "intersection" ||
|
|
55
|
+
!spreadNode.extends(keywords.object)
|
|
56
|
+
) {
|
|
57
|
+
return throwParseError(
|
|
58
|
+
writeInvalidSpreadTypeMessage(printable(result.innerValue))
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// For each key on spreadNode, add it to our object.
|
|
63
|
+
// We filter out keys from the spreadNode that will be defined later on this same object
|
|
64
|
+
// because the currently parsed definition will overwrite them.
|
|
65
|
+
const requiredEntriesFromSpread = (spreadNode.required ?? []).filter(
|
|
66
|
+
(e) => !entries.some(([k]) => k === e.key)
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
const optionalEntriesFromSpread = (spreadNode.optional ?? []).filter(
|
|
70
|
+
(e) => !entries.some(([k]) => k === e.key)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
required.push(...requiredEntriesFromSpread)
|
|
74
|
+
optional.push(...optionalEntriesFromSpread)
|
|
75
|
+
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
|
|
29
79
|
ctx.path.push(
|
|
30
80
|
`${
|
|
31
81
|
typeof result.innerKey === "symbol"
|
|
32
|
-
? `[${
|
|
82
|
+
? `[${printable(result.innerKey)}]`
|
|
33
83
|
: result.innerKey
|
|
34
84
|
}`
|
|
35
85
|
)
|
|
36
86
|
const valueNode = ctx.scope.parse(result.innerValue, ctx)
|
|
87
|
+
|
|
37
88
|
if (result.kind === "optional") {
|
|
38
89
|
optional.push({
|
|
39
90
|
key: result.innerKey,
|
|
@@ -46,32 +97,46 @@ export const parseObjectLiteral = (def: Dict, ctx: ParseContext) => {
|
|
|
46
97
|
})
|
|
47
98
|
}
|
|
48
99
|
ctx.path.pop()
|
|
100
|
+
|
|
101
|
+
hasSeenFirstKey ||= true
|
|
49
102
|
}
|
|
50
|
-
|
|
103
|
+
|
|
104
|
+
return schema({
|
|
51
105
|
basis: "object",
|
|
52
106
|
required,
|
|
53
107
|
optional
|
|
54
108
|
})
|
|
55
109
|
}
|
|
56
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Infers the contents of an object literal, ignoring a spread definition
|
|
113
|
+
* You probably want to use {@link inferObjectLiteral} instead.
|
|
114
|
+
*/
|
|
115
|
+
type inferObjectLiteralInner<def extends object, $, args> = {
|
|
116
|
+
// since def is a const parameter, we remove the readonly modifier here
|
|
117
|
+
// support for builtin readonly tracked here:
|
|
118
|
+
// https://github.com/arktypeio/arktype/issues/808
|
|
119
|
+
-readonly [k in keyof def as nonOptionalKeyFrom<
|
|
120
|
+
k,
|
|
121
|
+
def[k],
|
|
122
|
+
$,
|
|
123
|
+
args
|
|
124
|
+
>]: inferDefinition<def[k], $, args>
|
|
125
|
+
} & {
|
|
126
|
+
-readonly [k in keyof def as optionalKeyFrom<k, def[k]>]?: inferDefinition<
|
|
127
|
+
def[k] extends OptionalValue<infer inner> ? inner : def[k],
|
|
128
|
+
$,
|
|
129
|
+
args
|
|
130
|
+
>
|
|
131
|
+
}
|
|
132
|
+
|
|
57
133
|
export type inferObjectLiteral<def extends object, $, args> = evaluate<
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def[k],
|
|
65
|
-
$,
|
|
66
|
-
args
|
|
67
|
-
>]: inferDefinition<def[k], $, args>
|
|
68
|
-
} & {
|
|
69
|
-
-readonly [k in keyof def as optionalKeyFrom<k, def[k]>]?: inferDefinition<
|
|
70
|
-
def[k] extends OptionalValue<infer inner> ? inner : def[k],
|
|
71
|
-
$,
|
|
72
|
-
args
|
|
73
|
-
>
|
|
74
|
-
}
|
|
134
|
+
"..." extends keyof def
|
|
135
|
+
? merge<
|
|
136
|
+
inferDefinition<def["..."], $, args>,
|
|
137
|
+
inferObjectLiteralInner<def, $, args>
|
|
138
|
+
>
|
|
139
|
+
: inferObjectLiteralInner<def, $, args>
|
|
75
140
|
>
|
|
76
141
|
|
|
77
142
|
export type validateObjectLiteral<def, $, args> = {
|
|
@@ -80,10 +145,14 @@ export type validateObjectLiteral<def, $, args> = {
|
|
|
80
145
|
? // add a nominal type here to avoid allowing the error message as input
|
|
81
146
|
indexParseError<message>
|
|
82
147
|
: inferDefinition<indexDef, $, args> extends PropertyKey
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
148
|
+
? // if the indexDef is syntactically and semantically valid,
|
|
149
|
+
// move on to the validating the value definition
|
|
150
|
+
validateDefinition<def[k], $, args>
|
|
151
|
+
: indexParseError<writeInvalidPropertyKeyMessage<indexDef>>
|
|
152
|
+
: k extends "..."
|
|
153
|
+
? inferDefinition<def[k], $, args> extends object
|
|
154
|
+
? validateDefinition<def[k], $, args>
|
|
155
|
+
: indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>>
|
|
87
156
|
: validateObjectValue<def[k], $, args>
|
|
88
157
|
}
|
|
89
158
|
|
|
@@ -94,8 +163,8 @@ type nonOptionalKeyFrom<k extends PropertyKey, valueDef, $, args> = parseEntry<
|
|
|
94
163
|
? result["kind"] extends "required"
|
|
95
164
|
? result["innerKey"]
|
|
96
165
|
: result["kind"] extends "indexed"
|
|
97
|
-
|
|
98
|
-
|
|
166
|
+
? inferDefinition<result["innerKey"], $, args> & PropertyKey
|
|
167
|
+
: never
|
|
99
168
|
: never
|
|
100
169
|
|
|
101
170
|
type optionalKeyFrom<k extends PropertyKey, valueDef> = parseEntry<
|
|
@@ -118,3 +187,11 @@ export const writeInvalidPropertyKeyMessage = <indexDef extends string>(
|
|
|
118
187
|
|
|
119
188
|
type writeInvalidPropertyKeyMessage<indexDef extends string> =
|
|
120
189
|
`Indexed key definition '${indexDef}' must be a string, number or symbol`
|
|
190
|
+
|
|
191
|
+
export const writeInvalidSpreadTypeMessage = <def extends string>(
|
|
192
|
+
def: def
|
|
193
|
+
): writeInvalidSpreadTypeMessage<def> =>
|
|
194
|
+
`Spread operand must resolve to an object literal type (was ${def})`
|
|
195
|
+
|
|
196
|
+
type writeInvalidSpreadTypeMessage<def extends string> =
|
|
197
|
+
`Spread operand must resolve to an object literal type (was ${def})`
|