arktype 2.0.0-dev.1 → 2.0.0-dev.10
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 +270 -151
- 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 +95 -167
- package/__tests__/dateLiteral.test.ts +8 -4
- package/__tests__/declared.test.ts +7 -6
- package/__tests__/discrimination.test.ts +1 -1
- package/__tests__/divisor.test.ts +46 -22
- 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 +62 -49
- package/__tests__/instanceof.test.ts +25 -23
- package/__tests__/intersection.test.ts +52 -48
- package/__tests__/keyTraversal.test.ts +77 -73
- package/__tests__/keyof.test.ts +68 -52
- package/__tests__/keywords.test.ts +164 -138
- package/__tests__/literal.test.ts +60 -21
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +222 -22
- package/__tests__/narrow.test.ts +52 -54
- package/__tests__/objectLiteral.test.ts +347 -205
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/pipe.test.ts +501 -0
- 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 +257 -23
- 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 +76 -54
- package/__tests__/tsGenerics.test.ts +12 -12
- package/__tests__/type.test.ts +36 -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 +21 -65
- package/generic.ts +69 -0
- package/main.ts +6 -18
- package/match.ts +195 -93
- package/module.ts +15 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/generic.js.map +1 -0
- package/out/main.d.ts +6 -7
- package/out/main.js +4 -3
- package/out/main.js.map +1 -1
- package/out/match.d.ts +57 -34
- package/out/match.js +51 -17
- package/out/match.js.map +1 -1
- package/out/module.d.ts +10 -0
- package/out/module.js +3 -0
- package/out/module.js.map +1 -0
- package/out/parser/definition.d.ts +11 -13
- package/out/parser/definition.js +10 -13
- package/out/parser/definition.js.map +1 -1
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts +45 -14
- package/out/parser/objectLiteral.js +61 -52
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/bounds.js.map +1 -1
- package/out/parser/semantic/divisor.d.ts +5 -7
- package/out/parser/semantic/divisor.js +0 -1
- package/out/parser/semantic/infer.d.ts +24 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/infer.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +15 -14
- package/out/parser/semantic/validate.js +1 -1
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +13 -13
- package/out/parser/string/reduce/dynamic.js +31 -26
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/reduce/shared.d.ts +6 -20
- package/out/parser/string/reduce/shared.js +0 -1
- package/out/parser/string/reduce/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +27 -24
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
- package/out/parser/string/shift/operand/enclosed.js +12 -13
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
- package/out/parser/string/shift/operand/genericArgs.js +6 -10
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +8 -12
- package/out/parser/string/shift/operand/operand.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts +9 -13
- package/out/parser/string/shift/operator/bounds.js +43 -54
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts +2 -4
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +2 -3
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/operator/operator.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts +2 -2
- package/out/parser/string/shift/scanner.js +14 -16
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts +8 -6
- package/out/parser/string/string.js +1 -3
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts +102 -43
- package/out/parser/tuple.js +119 -87
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +43 -120
- package/out/scope.js +39 -209
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +43 -64
- package/out/type.js +30 -131
- package/out/type.js.map +1 -1
- package/package.json +7 -3
- package/parser/definition.ts +77 -93
- package/parser/generic.ts +38 -40
- package/parser/objectLiteral.ts +172 -136
- 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 +81 -73
- package/parser/string/reduce/dynamic.ts +67 -55
- package/parser/string/reduce/shared.ts +1 -3
- package/parser/string/reduce/static.ts +67 -69
- package/parser/string/shift/operand/date.ts +15 -14
- package/parser/string/shift/operand/enclosed.ts +30 -25
- 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 +107 -158
- package/parser/string/shift/operator/bounds.ts +118 -112
- package/parser/string/shift/operator/divisor.ts +14 -17
- package/parser/string/shift/operator/operator.ts +35 -46
- package/parser/string/shift/scanner.ts +51 -50
- package/parser/string/string.ts +30 -35
- package/parser/tuple.ts +375 -307
- package/scope.ts +170 -453
- package/type.ts +193 -274
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/typeMethods.test.ts +0 -69
- package/out/ark.d.ts.map +0 -1
- 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/main.d.ts.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/divisor.d.ts.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 -33
- 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/semantic/utils.d.ts.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/shared.d.ts +0 -54
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -47
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/scope.d.ts.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/out/type.d.ts.map +0 -1
- package/out/util.d.ts +0 -19
- package/out/util.d.ts.map +0 -1
- package/out/util.js +0 -7
- package/out/util.js.map +0 -1
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -144
- package/parser/shared.ts +0 -152
- package/util.ts +0 -36
package/parser/generic.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type keyError, 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
|
|
|
@@ -9,60 +9,58 @@ export type GenericDeclaration<
|
|
|
9
9
|
|
|
10
10
|
// we put the error in a tuple so that parseGenericParams always returns a string[]
|
|
11
11
|
export type GenericParamsParseError<message extends string = string> = [
|
|
12
|
-
|
|
12
|
+
keyError<message>
|
|
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
|
-
if (param === "")
|
|
35
|
-
|
|
36
|
-
}
|
|
32
|
+
if (param === "") throwParseError(emptyGenericParameterMessage)
|
|
33
|
+
|
|
37
34
|
scanner.shiftUntilNonWhitespace()
|
|
38
35
|
const nextNonWhitespace = scanner.shift()
|
|
39
|
-
return
|
|
40
|
-
? [param]
|
|
41
|
-
: nextNonWhitespace === ","
|
|
42
|
-
? [param, ...parseGenericParamsRecurse(scanner)]
|
|
36
|
+
return (
|
|
37
|
+
nextNonWhitespace === "" ? [param]
|
|
38
|
+
: nextNonWhitespace === "," ? [param, ..._parseGenericParams(scanner)]
|
|
43
39
|
: throwParseError(writeUnexpectedCharacterMessage(nextNonWhitespace, ","))
|
|
40
|
+
)
|
|
44
41
|
}
|
|
45
42
|
|
|
46
|
-
type
|
|
43
|
+
type _parseParams<
|
|
47
44
|
unscanned extends string,
|
|
48
45
|
param extends string,
|
|
49
46
|
result extends string[]
|
|
50
|
-
> =
|
|
51
|
-
|
|
52
|
-
?
|
|
53
|
-
: lookahead extends Scanner.WhiteSpaceToken
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
: Scanner.skipWhitespace<nextUnscanned> extends
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
> =
|
|
48
|
+
unscanned extends `${infer lookahead}${infer nextUnscanned}` ?
|
|
49
|
+
lookahead extends "," ? _parseParams<nextUnscanned, "", [...result, param]>
|
|
50
|
+
: lookahead extends Scanner.WhiteSpaceToken ?
|
|
51
|
+
param extends "" ?
|
|
52
|
+
// if the next char is whitespace and we aren't in the middle of a param, skip to the next one
|
|
53
|
+
_parseParams<Scanner.skipWhitespace<nextUnscanned>, "", result>
|
|
54
|
+
: Scanner.skipWhitespace<nextUnscanned> extends (
|
|
55
|
+
`${infer nextNonWhitespace}${infer rest}`
|
|
56
|
+
) ?
|
|
57
|
+
nextNonWhitespace extends "," ?
|
|
58
|
+
_parseParams<rest, "", [...result, param]>
|
|
59
|
+
: GenericParamsParseError<
|
|
61
60
|
writeUnexpectedCharacterMessage<nextNonWhitespace, ",">
|
|
62
|
-
|
|
63
|
-
:
|
|
64
|
-
|
|
65
|
-
:
|
|
66
|
-
: param extends ""
|
|
67
|
-
? result
|
|
61
|
+
>
|
|
62
|
+
: // params end with a single whitespace character, add the current token
|
|
63
|
+
[...result, param]
|
|
64
|
+
: _parseParams<nextUnscanned, `${param}${lookahead}`, result>
|
|
65
|
+
: param extends "" ? result
|
|
68
66
|
: [...result, param]
|
package/parser/objectLiteral.ts
CHANGED
|
@@ -1,113 +1,100 @@
|
|
|
1
|
-
import { keywords, schema, type Inner, type TypeNode } from "@arktype/schema"
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
type NodeDef,
|
|
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
|
+
spliterate,
|
|
15
|
+
stringAndSymbolicEntriesOf,
|
|
16
|
+
throwParseError
|
|
9
17
|
} from "@arktype/util"
|
|
10
|
-
|
|
11
18
|
import type { ParseContext } from "../scope.js"
|
|
12
19
|
import type { inferDefinition, validateDefinition } from "./definition.js"
|
|
13
20
|
import type { astToString } from "./semantic/utils.js"
|
|
14
21
|
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">[] = []
|
|
22
|
+
import { Scanner } from "./string/shift/scanner.js"
|
|
31
23
|
|
|
24
|
+
export const parseObjectLiteral = (def: Dict, ctx: ParseContext): RawSchema => {
|
|
25
|
+
const propNodes: NodeDef<"prop">[] = []
|
|
26
|
+
const indexNodes: NodeDef<"index">[] = []
|
|
32
27
|
// We only allow a spread operator to be used as the first key in an object
|
|
33
28
|
// because to match JS behavior any keys before the spread are overwritten
|
|
34
29
|
// by the values in the target object, so there'd be no useful purpose in having it
|
|
35
30
|
// anywhere except for the beginning.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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)
|
|
31
|
+
const parsedEntries = stringAndSymbolicEntriesOf(def).map(parseEntry)
|
|
32
|
+
if (parsedEntries[0]?.kind === "spread") {
|
|
33
|
+
// remove the spread entry so we can iterate over the remaining entries
|
|
34
|
+
// expecting non-spread entries
|
|
35
|
+
const spreadEntry = parsedEntries.shift()!
|
|
36
|
+
const spreadNode = ctx.$.parse(spreadEntry.value, ctx)
|
|
37
|
+
if (
|
|
38
|
+
!spreadNode.hasKind("intersection") ||
|
|
39
|
+
!spreadNode.extends(tsKeywords.object)
|
|
40
|
+
) {
|
|
41
|
+
return throwParseError(
|
|
42
|
+
writeInvalidSpreadTypeMessage(printable(spreadEntry.value))
|
|
67
43
|
)
|
|
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
44
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
45
|
+
// TODO: move to props group merge in schema
|
|
46
|
+
// For each key on spreadNode, add it to our object.
|
|
47
|
+
// We filter out keys from the spreadNode that will be defined later on this same object
|
|
48
|
+
// because the currently parsed definition will overwrite them.
|
|
49
|
+
spreadNode.prop?.forEach(
|
|
50
|
+
spreadRequired =>
|
|
51
|
+
!parsedEntries.some(
|
|
52
|
+
({ inner: innerKey }) => innerKey === spreadRequired.key
|
|
53
|
+
) && propNodes.push(spreadRequired)
|
|
85
54
|
)
|
|
86
|
-
|
|
55
|
+
}
|
|
56
|
+
for (const entry of parsedEntries) {
|
|
57
|
+
if (entry.kind === "spread") return throwParseError(nonLeadingSpreadError)
|
|
87
58
|
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
59
|
+
if (entry.kind === "index") {
|
|
60
|
+
// handle key parsing first to match type behavior
|
|
61
|
+
const key = ctx.$.parse(entry.inner, ctx)
|
|
62
|
+
const value = ctx.$.parse(entry.value, ctx)
|
|
63
|
+
|
|
64
|
+
// extract enumerable named props from the index signature
|
|
65
|
+
const [enumerable, nonEnumerable] = spliterate(key.branches, k =>
|
|
66
|
+
k.hasKind("unit")
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
if (enumerable.length) {
|
|
70
|
+
propNodes.push(
|
|
71
|
+
...enumerable.map(k =>
|
|
72
|
+
ctx.$.node("prop", { key: k.unit as Key, value })
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
if (nonEnumerable.length)
|
|
76
|
+
indexNodes.push(ctx.$.node("index", { key: nonEnumerable, value }))
|
|
77
|
+
} else indexNodes.push(ctx.$.node("index", { key, value }))
|
|
93
78
|
} else {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
79
|
+
const value = ctx.$.parse(entry.value, ctx)
|
|
80
|
+
propNodes.push({
|
|
81
|
+
key: entry.inner,
|
|
82
|
+
value,
|
|
83
|
+
optional: entry.kind === "optional"
|
|
97
84
|
})
|
|
98
85
|
}
|
|
99
|
-
ctx.path.pop()
|
|
100
|
-
|
|
101
|
-
hasSeenFirstKey ||= true
|
|
102
86
|
}
|
|
103
87
|
|
|
104
|
-
return schema({
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
88
|
+
return ctx.$.schema({
|
|
89
|
+
domain: "object",
|
|
90
|
+
prop: propNodes,
|
|
91
|
+
index: indexNodes
|
|
108
92
|
})
|
|
109
93
|
}
|
|
110
94
|
|
|
95
|
+
export const nonLeadingSpreadError =
|
|
96
|
+
"Spread operator may only be used as the first key in an object"
|
|
97
|
+
|
|
111
98
|
/**
|
|
112
99
|
* Infers the contents of an object literal, ignoring a spread definition
|
|
113
100
|
* You probably want to use {@link inferObjectLiteral} instead.
|
|
@@ -116,63 +103,120 @@ type inferObjectLiteralInner<def extends object, $, args> = {
|
|
|
116
103
|
// since def is a const parameter, we remove the readonly modifier here
|
|
117
104
|
// support for builtin readonly tracked here:
|
|
118
105
|
// https://github.com/arktypeio/arktype/issues/808
|
|
119
|
-
-readonly [k in keyof def as nonOptionalKeyFrom<
|
|
120
|
-
k,
|
|
106
|
+
-readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: inferDefinition<
|
|
121
107
|
def[k],
|
|
122
108
|
$,
|
|
123
109
|
args
|
|
124
|
-
>
|
|
110
|
+
>
|
|
125
111
|
} & {
|
|
126
|
-
-readonly [k in keyof def as optionalKeyFrom<k
|
|
127
|
-
def[k]
|
|
112
|
+
-readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<
|
|
113
|
+
def[k],
|
|
128
114
|
$,
|
|
129
115
|
args
|
|
130
116
|
>
|
|
131
117
|
}
|
|
132
118
|
|
|
133
|
-
export type inferObjectLiteral<def extends object, $, args> =
|
|
134
|
-
"..." extends keyof def
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
119
|
+
export type inferObjectLiteral<def extends object, $, args> = show<
|
|
120
|
+
"..." extends keyof def ?
|
|
121
|
+
merge<
|
|
122
|
+
inferDefinition<def["..."], $, args>,
|
|
123
|
+
inferObjectLiteralInner<def, $, args>
|
|
124
|
+
>
|
|
125
|
+
: inferObjectLiteralInner<def, $, args>
|
|
140
126
|
>
|
|
141
127
|
|
|
142
128
|
export type validateObjectLiteral<def, $, args> = {
|
|
143
|
-
[k in keyof def]: k extends
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
129
|
+
[k in keyof def]: k extends IndexKey<infer indexDef> ?
|
|
130
|
+
validateString<indexDef, $, args> extends ErrorMessage<infer message> ?
|
|
131
|
+
// add a nominal type here to avoid allowing the error message as input
|
|
132
|
+
indexParseError<message>
|
|
133
|
+
: inferDefinition<indexDef, $, args> extends PropertyKey ?
|
|
134
|
+
// if the indexDef is syntactically and semantically valid,
|
|
135
|
+
// move on to the validating the value definition
|
|
136
|
+
validateDefinition<def[k], $, args>
|
|
137
|
+
: indexParseError<writeInvalidPropertyKeyMessage<indexDef>>
|
|
138
|
+
: k extends "..." ?
|
|
139
|
+
inferDefinition<def[k], $, args> extends object ?
|
|
140
|
+
validateDefinition<def[k], $, args>
|
|
141
|
+
: indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>>
|
|
142
|
+
: validateDefinition<def[k], $, args>
|
|
157
143
|
}
|
|
158
144
|
|
|
159
|
-
type nonOptionalKeyFrom<k
|
|
160
|
-
k,
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
145
|
+
type nonOptionalKeyFrom<k, $, args> =
|
|
146
|
+
parseKey<k> extends PreparsedKey<"required", infer inner> ? inner
|
|
147
|
+
: parseKey<k> extends PreparsedKey<"index", infer inner> ?
|
|
148
|
+
inferDefinition<inner, $, args> & Key
|
|
149
|
+
: // spread key is handled at the type root so is handled neither here nor in optionalKeyFrom
|
|
150
|
+
never
|
|
151
|
+
|
|
152
|
+
type optionalKeyFrom<k> =
|
|
153
|
+
parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never
|
|
154
|
+
|
|
155
|
+
type PreparsedKey<
|
|
156
|
+
kind extends ParsedKeyKind = ParsedKeyKind,
|
|
157
|
+
inner extends Key = Key
|
|
158
|
+
> = {
|
|
159
|
+
kind: kind
|
|
160
|
+
inner: inner
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
namespace PreparsedKey {
|
|
164
|
+
export type from<t extends PreparsedKey> = t
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
type ParsedKeyKind = "required" | "optional" | "index" | "spread"
|
|
168
|
+
|
|
169
|
+
export type IndexKey<def extends string = string> = `[${def}]`
|
|
170
|
+
|
|
171
|
+
type PreparsedEntry = PreparsedKey & { value: unknown }
|
|
172
|
+
|
|
173
|
+
export const parseEntry = (entry: readonly [Key, unknown]): PreparsedEntry =>
|
|
174
|
+
Object.assign(parseKey(entry[0]), { value: entry[1] })
|
|
175
|
+
|
|
176
|
+
const parseKey = (key: Key): PreparsedKey =>
|
|
177
|
+
typeof key === "symbol" ? { inner: key, kind: "required" }
|
|
178
|
+
: key.at(-1) === "?" ?
|
|
179
|
+
key.at(-2) === Scanner.escapeToken ?
|
|
180
|
+
{ inner: `${key.slice(0, -2)}?`, kind: "required" }
|
|
181
|
+
: {
|
|
182
|
+
inner: key.slice(0, -1),
|
|
183
|
+
kind: "optional"
|
|
184
|
+
}
|
|
185
|
+
: key[0] === "[" && key.at(-1) === "]" ?
|
|
186
|
+
{ inner: key.slice(1, -1), kind: "index" }
|
|
187
|
+
: key[0] === Scanner.escapeToken && key[1] === "[" && key.at(-1) === "]" ?
|
|
188
|
+
{ inner: key.slice(1), kind: "required" }
|
|
189
|
+
: key === "..." ? { inner: "...", kind: "spread" }
|
|
190
|
+
: { inner: key === "\\..." ? "..." : key, kind: "required" }
|
|
191
|
+
|
|
192
|
+
type parseKey<k> =
|
|
193
|
+
k extends `${infer inner}?` ?
|
|
194
|
+
inner extends `${infer baseName}${Scanner.EscapeToken}` ?
|
|
195
|
+
PreparsedKey.from<{
|
|
196
|
+
kind: "required"
|
|
197
|
+
inner: `${baseName}?`
|
|
198
|
+
}>
|
|
199
|
+
: PreparsedKey.from<{
|
|
200
|
+
kind: "optional"
|
|
201
|
+
inner: inner
|
|
202
|
+
}>
|
|
203
|
+
: k extends "..." ? PreparsedKey.from<{ kind: "spread"; inner: "..." }>
|
|
204
|
+
: k extends `${Scanner.EscapeToken}...` ?
|
|
205
|
+
PreparsedKey.from<{ kind: "required"; inner: "..." }>
|
|
206
|
+
: k extends IndexKey<infer def> ?
|
|
207
|
+
PreparsedKey.from<{
|
|
208
|
+
kind: "index"
|
|
209
|
+
inner: def
|
|
210
|
+
}>
|
|
211
|
+
: PreparsedKey.from<{
|
|
212
|
+
kind: "required"
|
|
213
|
+
inner: k extends (
|
|
214
|
+
`${Scanner.EscapeToken}${infer escapedIndexKey extends IndexKey}`
|
|
215
|
+
) ?
|
|
216
|
+
escapedIndexKey
|
|
217
|
+
: k extends Key ? k
|
|
218
|
+
: `${k & number}`
|
|
219
|
+
}>
|
|
176
220
|
|
|
177
221
|
declare const indexParseSymbol: unique symbol
|
|
178
222
|
|
|
@@ -180,14 +224,6 @@ export type indexParseError<message extends string = string> = {
|
|
|
180
224
|
[indexParseSymbol]: message
|
|
181
225
|
}
|
|
182
226
|
|
|
183
|
-
export const writeInvalidPropertyKeyMessage = <indexDef extends string>(
|
|
184
|
-
indexDef: indexDef
|
|
185
|
-
): writeInvalidPropertyKeyMessage<indexDef> =>
|
|
186
|
-
`Indexed key definition '${indexDef}' must be a string, number or symbol`
|
|
187
|
-
|
|
188
|
-
type writeInvalidPropertyKeyMessage<indexDef extends string> =
|
|
189
|
-
`Indexed key definition '${indexDef}' must be a string, number or symbol`
|
|
190
|
-
|
|
191
227
|
export const writeInvalidSpreadTypeMessage = <def extends string>(
|
|
192
228
|
def: def
|
|
193
229
|
): writeInvalidSpreadTypeMessage<def> =>
|
|
@@ -1,30 +1,23 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { ErrorMessage } from "@arktype/util"
|
|
1
|
+
import type { LimitLiteral, writeUnboundableMessage } from "@arktype/schema"
|
|
2
|
+
import type { ErrorMessage, array } from "@arktype/util"
|
|
3
3
|
import type {
|
|
4
4
|
Comparator,
|
|
5
|
-
InvertedComparators
|
|
6
|
-
LimitLiteral
|
|
5
|
+
InvertedComparators
|
|
7
6
|
} from "../string/reduce/shared.js"
|
|
8
7
|
import type {
|
|
9
8
|
BoundExpressionKind,
|
|
10
|
-
writeInvalidLimitMessage
|
|
11
|
-
writeUnboundableMessage
|
|
9
|
+
writeInvalidLimitMessage
|
|
12
10
|
} from "../string/shift/operator/bounds.js"
|
|
13
|
-
import type {
|
|
11
|
+
import type { inferAstIn } from "./infer.js"
|
|
14
12
|
import type { astToString } from "./utils.js"
|
|
15
13
|
import type { validateAst } from "./validate.js"
|
|
16
14
|
|
|
17
|
-
export type validateRange<
|
|
18
|
-
l
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
args
|
|
23
|
-
> = l extends LimitLiteral
|
|
24
|
-
? validateBound<r, InvertedComparators[comparator], l, "left", $, args>
|
|
25
|
-
: l extends [infer leftAst, Comparator, unknown]
|
|
26
|
-
? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>>
|
|
27
|
-
: validateBound<l, comparator, r & LimitLiteral, "right", $, args>
|
|
15
|
+
export type validateRange<l, comparator extends Comparator, r, $, args> =
|
|
16
|
+
l extends LimitLiteral ?
|
|
17
|
+
validateBound<r, InvertedComparators[comparator], l, "left", $, args>
|
|
18
|
+
: l extends [infer leftAst, Comparator, unknown] ?
|
|
19
|
+
ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>>
|
|
20
|
+
: validateBound<l, comparator, r & LimitLiteral, "right", $, args>
|
|
28
21
|
|
|
29
22
|
export type validateBound<
|
|
30
23
|
boundedAst,
|
|
@@ -33,24 +26,32 @@ export type validateBound<
|
|
|
33
26
|
boundKind extends BoundExpressionKind,
|
|
34
27
|
$,
|
|
35
28
|
args
|
|
36
|
-
> =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
29
|
+
> =
|
|
30
|
+
inferAstIn<boundedAst, $, args> extends infer bounded ?
|
|
31
|
+
isNumericallyBoundable<bounded> extends true ?
|
|
32
|
+
limit extends number ?
|
|
33
|
+
validateAst<boundedAst, $, args>
|
|
34
|
+
: ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
|
|
35
|
+
: bounded extends Date ?
|
|
36
|
+
// allow numeric or date literal as a Date limit
|
|
37
|
+
validateAst<boundedAst, $, args>
|
|
38
|
+
: ErrorMessage<
|
|
45
39
|
writeUnboundableMessage<
|
|
46
40
|
astToString<
|
|
47
|
-
boundKind extends "left"
|
|
48
|
-
|
|
49
|
-
: boundedAst
|
|
41
|
+
boundKind extends "left" ? boundedAst[0 & keyof boundedAst]
|
|
42
|
+
: boundedAst
|
|
50
43
|
>
|
|
51
44
|
>
|
|
52
|
-
|
|
53
|
-
:
|
|
45
|
+
>
|
|
46
|
+
: never
|
|
47
|
+
|
|
48
|
+
// Check each numerically boundable type individually so an expression comprised
|
|
49
|
+
// of mixed bound kinds like (string|number)<5 isn't allowed
|
|
50
|
+
type isNumericallyBoundable<bounded> =
|
|
51
|
+
[bounded] extends [number] ? true
|
|
52
|
+
: [bounded] extends [string] ? true
|
|
53
|
+
: [bounded] extends [array] ? true
|
|
54
|
+
: false
|
|
54
55
|
|
|
55
56
|
export const writeDoubleRightBoundMessage = <root extends string>(
|
|
56
57
|
root: root
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import type { writeIndivisibleMessage } from "@arktype/schema"
|
|
1
|
+
import type { Schema, writeIndivisibleMessage } from "@arktype/schema"
|
|
2
2
|
import type { ErrorMessage } from "@arktype/util"
|
|
3
|
-
import type {
|
|
4
|
-
import type { astToString } from "./utils.js"
|
|
3
|
+
import type { inferAstIn } from "./infer.js"
|
|
5
4
|
import type { validateAst } from "./validate.js"
|
|
6
5
|
|
|
7
|
-
export type validateDivisor<l, $, args> =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
type isDivisible<data> = [data] extends [number] ? true : false
|
|
6
|
+
export type validateDivisor<l, $, args> =
|
|
7
|
+
inferAstIn<l, $, args> extends infer data ?
|
|
8
|
+
[data] extends [number] ?
|
|
9
|
+
validateAst<l, $, args>
|
|
10
|
+
: ErrorMessage<writeIndivisibleMessage<Schema<data>>>
|
|
11
|
+
: never
|