arktype 2.0.0-dev.2 → 2.0.0-dev.4
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 +225 -82
- package/__tests__/divisor.test.ts +6 -2
- package/__tests__/instanceof.test.ts +1 -1
- package/match.ts +3 -3
- package/out/match.d.ts +3 -3
- package/out/parser/objectLiteral.d.ts.map +1 -1
- package/out/parser/objectLiteral.js +31 -35
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/semantic.d.ts +4 -5
- package/out/parser/semantic/semantic.d.ts.map +1 -1
- package/out/parser/shared.d.ts +6 -0
- package/out/parser/shared.d.ts.map +1 -1
- package/out/parser/shared.js +5 -4
- package/out/parser/shared.js.map +1 -1
- package/out/parser/tuple.d.ts +14 -13
- package/out/parser/tuple.d.ts.map +1 -1
- package/out/parser/tuple.js +92 -50
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts.map +1 -1
- package/out/scope.js +7 -4
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +1 -2
- package/out/type.d.ts.map +1 -1
- package/out/type.js +3 -3
- package/out/type.js.map +1 -1
- package/package.json +3 -3
- package/parser/objectLiteral.ts +51 -55
- package/parser/semantic/semantic.ts +5 -5
- package/parser/semantic/validate.ts +1 -1
- package/parser/shared.ts +8 -5
- package/parser/tuple.ts +169 -81
- package/scope.ts +7 -4
- package/type.ts +4 -4
- package/parser/semantic/intersections.ts +0 -46
package/parser/objectLiteral.ts
CHANGED
|
@@ -35,70 +35,66 @@ export const parseObjectLiteral = (def: Dict, ctx: ParseContext): TypeNode => {
|
|
|
35
35
|
// anywhere except for the beginning.
|
|
36
36
|
// Discussion in ArkType Discord:
|
|
37
37
|
// https://discord.com/channels/957797212103016458/1103023445035462678/1182814502471860334
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
38
|
+
|
|
39
|
+
const parsedEntries = stringAndSymbolicEntriesOf(def).map(parseEntry)
|
|
40
|
+
if (parsedEntries[0].kind === "spread") {
|
|
41
|
+
// remove the spread entry so we can iterate over the remaining entries
|
|
42
|
+
// expecting non-spread entries
|
|
43
|
+
const spreadEntry = parsedEntries.shift()!
|
|
44
|
+
const spreadNode = ctx.scope.parse(spreadEntry.innerValue, ctx)
|
|
45
|
+
|
|
46
|
+
if (
|
|
47
|
+
spreadNode.kind !== "intersection" ||
|
|
48
|
+
!spreadNode.extends(keywords.object)
|
|
49
|
+
) {
|
|
50
|
+
return throwParseError(
|
|
51
|
+
writeInvalidSpreadTypeMessage(printable(spreadEntry.innerValue))
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// TODO: move to props group merge in schema
|
|
56
|
+
// For each key on spreadNode, add it to our object.
|
|
57
|
+
// We filter out keys from the spreadNode that will be defined later on this same object
|
|
58
|
+
// because the currently parsed definition will overwrite them.
|
|
59
|
+
spreadNode.required?.forEach(
|
|
60
|
+
(spreadRequired) =>
|
|
61
|
+
!parsedEntries.some(
|
|
62
|
+
({ innerKey }) => innerKey === spreadRequired.key
|
|
63
|
+
) && required.push(spreadRequired)
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
spreadNode.optional?.forEach(
|
|
67
|
+
(spreadOptional) =>
|
|
68
|
+
!parsedEntries.some(
|
|
69
|
+
({ innerKey }) => innerKey === spreadOptional.key
|
|
70
|
+
) && optional.push(spreadOptional)
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
for (const entry of parsedEntries) {
|
|
74
|
+
if (entry.kind === "spread") {
|
|
75
|
+
return throwParseError(
|
|
76
|
+
"Spread operator may only be used as the first key in an object"
|
|
77
|
+
)
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
ctx.path.push(
|
|
80
81
|
`${
|
|
81
|
-
typeof
|
|
82
|
-
? `[${printable(
|
|
83
|
-
:
|
|
82
|
+
typeof entry.innerKey === "symbol"
|
|
83
|
+
? `[${printable(entry.innerKey)}]`
|
|
84
|
+
: entry.innerKey
|
|
84
85
|
}`
|
|
85
86
|
)
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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)
|
|
93
94
|
} else {
|
|
94
|
-
required.push(
|
|
95
|
-
key: result.innerKey,
|
|
96
|
-
value: valueNode
|
|
97
|
-
})
|
|
95
|
+
required.push(inner)
|
|
98
96
|
}
|
|
99
97
|
ctx.path.pop()
|
|
100
|
-
|
|
101
|
-
hasSeenFirstKey ||= true
|
|
102
98
|
}
|
|
103
99
|
|
|
104
100
|
return schema({
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
Constraints,
|
|
2
3
|
DateLiteral,
|
|
3
|
-
Refinements,
|
|
4
4
|
RegexLiteral,
|
|
5
5
|
distill,
|
|
6
|
+
inferIntersection,
|
|
6
7
|
is
|
|
7
8
|
} from "@arktype/schema"
|
|
8
9
|
import type {
|
|
@@ -24,7 +25,6 @@ import type {
|
|
|
24
25
|
LimitLiteral
|
|
25
26
|
} from "../string/reduce/shared.js"
|
|
26
27
|
import type { StringLiteral } from "../string/shift/operand/enclosed.js"
|
|
27
|
-
import type { inferIntersection } from "./intersections.js"
|
|
28
28
|
|
|
29
29
|
export type inferAstRoot<ast, $, args> = inferAst<ast, $, args, {}>
|
|
30
30
|
|
|
@@ -34,7 +34,7 @@ export type inferAst<
|
|
|
34
34
|
ast,
|
|
35
35
|
$,
|
|
36
36
|
args,
|
|
37
|
-
refinements extends
|
|
37
|
+
refinements extends Constraints
|
|
38
38
|
> = ast extends List
|
|
39
39
|
? inferExpression<ast, $, args, refinements>
|
|
40
40
|
: inferTerminal<ast, $, args, refinements>
|
|
@@ -48,7 +48,7 @@ export type inferExpression<
|
|
|
48
48
|
ast extends List,
|
|
49
49
|
$,
|
|
50
50
|
args,
|
|
51
|
-
refinements extends
|
|
51
|
+
refinements extends Constraints
|
|
52
52
|
> = ast extends GenericInstantiationAst
|
|
53
53
|
? inferDefinition<
|
|
54
54
|
ast[0]["definition"],
|
|
@@ -122,7 +122,7 @@ export type inferTerminal<
|
|
|
122
122
|
token,
|
|
123
123
|
$,
|
|
124
124
|
args,
|
|
125
|
-
refinements extends
|
|
125
|
+
refinements extends Constraints
|
|
126
126
|
> = token extends keyof args | keyof $
|
|
127
127
|
? {} extends refinements
|
|
128
128
|
? resolve<token, $, args>
|
package/parser/shared.ts
CHANGED
|
@@ -12,7 +12,9 @@ type KeyParseResult<kind extends ParsedKeyKind = ParsedKeyKind> = {
|
|
|
12
12
|
innerKey: string | symbol
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
type
|
|
15
|
+
export type EntryValueParseResult<
|
|
16
|
+
kind extends ParsedValueKind = ParsedValueKind
|
|
17
|
+
> = {
|
|
16
18
|
kind: kind
|
|
17
19
|
innerValue: unknown
|
|
18
20
|
}
|
|
@@ -51,9 +53,9 @@ type validateObjectValueString<def, $, args> =
|
|
|
51
53
|
|
|
52
54
|
type DefinitionEntry = readonly [string | symbol, unknown]
|
|
53
55
|
|
|
54
|
-
const
|
|
56
|
+
export const parseEntryValue = (value: unknown): EntryValueParseResult => {
|
|
55
57
|
if (typeof value === "string") {
|
|
56
|
-
if (value
|
|
58
|
+
if (value.at(-1) === "?" && value.length > 1) {
|
|
57
59
|
return {
|
|
58
60
|
kind: "optional",
|
|
59
61
|
innerValue: value.slice(0, -1)
|
|
@@ -63,7 +65,8 @@ const getInnerValue = (value: unknown): ValueParseResult => {
|
|
|
63
65
|
if (value.length === 2 && value[1] === "?") {
|
|
64
66
|
return {
|
|
65
67
|
kind: "optional",
|
|
66
|
-
|
|
68
|
+
// allow redundant optionality like ["string?", "?"]
|
|
69
|
+
innerValue: parseEntryValue(value[0]).innerValue
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
}
|
|
@@ -87,7 +90,7 @@ export const parseEntry = ([key, value]: DefinitionEntry): EntryParseResult => {
|
|
|
87
90
|
: key === "\\..."
|
|
88
91
|
? { innerKey: "...", kind: "required" }
|
|
89
92
|
: { innerKey: key, kind: "required" }
|
|
90
|
-
const valueParseResult =
|
|
93
|
+
const valueParseResult = parseEntryValue(value)
|
|
91
94
|
return {
|
|
92
95
|
innerKey: keyParseResult.innerKey,
|
|
93
96
|
innerValue: valueParseResult.innerValue,
|
package/parser/tuple.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import {
|
|
2
2
|
keywords,
|
|
3
|
+
makeRootAndArrayPropertiesMutable,
|
|
3
4
|
schema,
|
|
4
5
|
type BaseMeta,
|
|
5
6
|
type Morph,
|
|
6
7
|
type MorphChildKind,
|
|
8
|
+
type MutableInner,
|
|
9
|
+
type Node,
|
|
7
10
|
type Out,
|
|
8
11
|
type Predicate,
|
|
9
12
|
type Schema,
|
|
10
13
|
type TypeNode,
|
|
14
|
+
type UnionChildKind,
|
|
11
15
|
type extractIn,
|
|
12
16
|
type extractOut,
|
|
17
|
+
type inferIntersection,
|
|
13
18
|
type inferMorphOut,
|
|
14
19
|
type inferNarrow
|
|
15
20
|
} from "@arktype/schema"
|
|
16
21
|
import {
|
|
22
|
+
append,
|
|
17
23
|
isArray,
|
|
18
24
|
objectKindOrDomainOf,
|
|
19
25
|
printable,
|
|
@@ -21,6 +27,7 @@ import {
|
|
|
21
27
|
type BuiltinObjectKind,
|
|
22
28
|
type Constructor,
|
|
23
29
|
type Domain,
|
|
30
|
+
type ErrorMessage,
|
|
24
31
|
type List,
|
|
25
32
|
type conform,
|
|
26
33
|
type evaluate,
|
|
@@ -28,80 +35,144 @@ import {
|
|
|
28
35
|
} from "@arktype/util"
|
|
29
36
|
import type { ParseContext } from "../scope.js"
|
|
30
37
|
import type { inferDefinition, validateDefinition } from "./definition.js"
|
|
31
|
-
import type { inferIntersection } from "./semantic/intersections.js"
|
|
32
38
|
import type { InfixOperator, PostfixExpression } from "./semantic/semantic.js"
|
|
33
39
|
import { writeUnsatisfiableExpressionError } from "./semantic/validate.js"
|
|
34
40
|
import {
|
|
35
41
|
configureShallowDescendants,
|
|
36
|
-
|
|
42
|
+
parseEntryValue,
|
|
37
43
|
type EntryParseResult,
|
|
44
|
+
type EntryValueParseResult,
|
|
45
|
+
type parseEntry,
|
|
38
46
|
type validateObjectValue
|
|
39
47
|
} from "./shared.js"
|
|
40
48
|
import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js"
|
|
41
49
|
import type { BaseCompletions } from "./string/string.js"
|
|
42
50
|
|
|
43
|
-
export const parseTuple = (def:
|
|
51
|
+
export const parseTuple = (def: readonly unknown[], ctx: ParseContext) =>
|
|
44
52
|
maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx)
|
|
45
53
|
|
|
46
|
-
export const parseTupleLiteral = (
|
|
47
|
-
|
|
48
|
-
|
|
54
|
+
export const parseTupleLiteral = (
|
|
55
|
+
def: readonly unknown[],
|
|
56
|
+
ctx: ParseContext
|
|
57
|
+
): TypeNode => {
|
|
58
|
+
let sequences: MutableInner<"sequence">[] = [{}]
|
|
49
59
|
for (let i = 0; i < def.length; i++) {
|
|
50
|
-
let elementDef = def[i]
|
|
51
60
|
ctx.path.push(`${i}`)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
variadicIndex = i
|
|
58
|
-
} else if (
|
|
59
|
-
isArray(elementDef) &&
|
|
60
|
-
elementDef.length === 2 &&
|
|
61
|
-
elementDef[0] === "..."
|
|
62
|
-
) {
|
|
63
|
-
elementDef = elementDef[1]
|
|
64
|
-
if (variadicIndex !== undefined) {
|
|
65
|
-
return throwParseError(multipleVariadicMesage)
|
|
61
|
+
const parsedElementDef = parseSpreadable(def[i])
|
|
62
|
+
const element = ctx.scope.parse(parsedElementDef.innerValue, ctx)
|
|
63
|
+
if (parsedElementDef.spread) {
|
|
64
|
+
if (parsedElementDef.kind === "optional") {
|
|
65
|
+
return throwParseError(spreadOptionalMessage)
|
|
66
66
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const parsedEntry = parseEntry([`${i}`, elementDef])
|
|
70
|
-
const value = ctx.scope.parse(parsedEntry.innerValue, ctx)
|
|
71
|
-
if (variadicIndex === i) {
|
|
72
|
-
if (!value.extends(keywords.Array)) {
|
|
73
|
-
return throwParseError(writeNonArrayRestMessage(elementDef))
|
|
67
|
+
if (!element.extends(keywords.Array)) {
|
|
68
|
+
return throwParseError(writeNonArraySpreadMessage(element))
|
|
74
69
|
}
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
//
|
|
78
|
-
|
|
70
|
+
// a spread must be distributed over branches e.g.:
|
|
71
|
+
// def: [string, ...(number[] | [true, false])]
|
|
72
|
+
// nodes: [string, ...number[]] | [string, true, false]
|
|
73
|
+
sequences = sequences.flatMap((base) =>
|
|
74
|
+
// since appendElement mutates base, we have to shallow-ish clone it for each branch
|
|
75
|
+
element.branches.map((branch) =>
|
|
76
|
+
appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
79
|
} else {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
})
|
|
80
|
+
sequences = sequences.map((base) =>
|
|
81
|
+
appendElement(
|
|
82
|
+
base,
|
|
83
|
+
parsedElementDef.kind === "optional" ? "optional" : "required",
|
|
84
|
+
element
|
|
85
|
+
)
|
|
86
|
+
)
|
|
88
87
|
}
|
|
89
88
|
ctx.path.pop()
|
|
90
89
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
90
|
+
return schema(
|
|
91
|
+
...sequences.map((sequence) => ({
|
|
92
|
+
basis: Array,
|
|
93
|
+
sequence
|
|
94
|
+
}))
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const appendElement = (
|
|
99
|
+
base: MutableInner<"sequence">,
|
|
100
|
+
kind: "optional" | "required" | "variadic",
|
|
101
|
+
element: TypeNode
|
|
102
|
+
): MutableInner<"sequence"> => {
|
|
103
|
+
switch (kind) {
|
|
104
|
+
case "required":
|
|
105
|
+
if (base.optionals)
|
|
106
|
+
// e.g. [string?, number]
|
|
107
|
+
return throwParseError(requiredPostOptionalMessage)
|
|
108
|
+
if (base.variadic) {
|
|
109
|
+
// e.g. [...string[], number]
|
|
110
|
+
base.postfix = append(base.postfix, element)
|
|
111
|
+
} else {
|
|
112
|
+
// e.g. [string, number]
|
|
113
|
+
base.prefix = append(base.prefix, element)
|
|
114
|
+
}
|
|
115
|
+
return base
|
|
116
|
+
case "optional":
|
|
117
|
+
if (base.variadic)
|
|
118
|
+
// e.g. [...string[], number?]
|
|
119
|
+
return throwParseError(optionalPostVariadicMessage)
|
|
120
|
+
// e.g. [string, number?]
|
|
121
|
+
base.optionals = append(base.optionals, element)
|
|
122
|
+
return base
|
|
123
|
+
case "variadic":
|
|
124
|
+
// e.g. [...string[], number, ...string[]]
|
|
125
|
+
if (base.postfix) throwParseError(multipleVariadicMesage)
|
|
126
|
+
if (base.variadic) {
|
|
127
|
+
if (!base.variadic.equals(element)) {
|
|
128
|
+
// e.g. [...string[], ...number[]]
|
|
129
|
+
throwParseError(multipleVariadicMesage)
|
|
130
|
+
}
|
|
131
|
+
// e.g. [...string[], ...string[]]
|
|
132
|
+
// do nothing, second spread doesn't change the type
|
|
133
|
+
} else {
|
|
134
|
+
// e.g. [string, ...number[]]
|
|
135
|
+
base.variadic = element
|
|
136
|
+
}
|
|
137
|
+
return base
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const appendSpreadBranch = (
|
|
142
|
+
base: MutableInner<"sequence">,
|
|
143
|
+
branch: Node<UnionChildKind>
|
|
144
|
+
): MutableInner<"sequence"> => {
|
|
145
|
+
const spread = branch.firstReferenceOfKind("sequence")
|
|
146
|
+
if (!spread) {
|
|
147
|
+
// the only array with no sequence reference is unknown[]
|
|
148
|
+
return appendElement(base, "variadic", keywords.unknown)
|
|
149
|
+
}
|
|
150
|
+
spread.prefix.forEach((node) => appendElement(base, "required", node))
|
|
151
|
+
spread.optionals.forEach((node) => appendElement(base, "optional", node))
|
|
152
|
+
spread.variadic && appendElement(base, "variadic", spread.variadic)
|
|
153
|
+
spread.postfix.forEach((node) => appendElement(base, "required", node))
|
|
154
|
+
return base
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
type TupleElementParseResult = EntryValueParseResult & { spread?: true }
|
|
158
|
+
|
|
159
|
+
const parseSpreadable = (elementDef: unknown): TupleElementParseResult => {
|
|
160
|
+
if (typeof elementDef === "string" && elementDef.startsWith("...")) {
|
|
161
|
+
// variadic string definition like "...string[]"
|
|
162
|
+
return { ...parseEntryValue(elementDef.slice(3)), spread: true }
|
|
163
|
+
}
|
|
164
|
+
if (
|
|
165
|
+
isArray(elementDef) &&
|
|
166
|
+
elementDef.length === 2 &&
|
|
167
|
+
elementDef[0] === "..."
|
|
168
|
+
) {
|
|
169
|
+
// variadic tuple expression like ["...", { a: "1" }]
|
|
170
|
+
return { ...parseEntryValue(elementDef[1]), spread: true }
|
|
100
171
|
}
|
|
101
|
-
return
|
|
172
|
+
return parseEntryValue(elementDef)
|
|
102
173
|
}
|
|
103
174
|
|
|
104
|
-
|
|
175
|
+
const maybeParseTupleExpression = (
|
|
105
176
|
def: List,
|
|
106
177
|
ctx: ParseContext
|
|
107
178
|
): TypeNode | undefined => {
|
|
@@ -157,28 +228,32 @@ export type validateTupleLiteral<
|
|
|
157
228
|
tail,
|
|
158
229
|
$,
|
|
159
230
|
args,
|
|
160
|
-
[...result, validateTupleElement<head,
|
|
231
|
+
[...result, validateTupleElement<head, result, $, args>]
|
|
161
232
|
>
|
|
162
233
|
: result
|
|
163
234
|
|
|
164
|
-
type validateTupleElement<
|
|
165
|
-
head
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
235
|
+
type validateTupleElement<
|
|
236
|
+
head,
|
|
237
|
+
result extends unknown[],
|
|
238
|
+
$,
|
|
239
|
+
args
|
|
240
|
+
> = head extends variadicExpression<infer operand>
|
|
241
|
+
? validateDefinition<operand, $, args> extends infer syntacticResult
|
|
242
|
+
? syntacticResult extends operand
|
|
243
|
+
? semanticallyValidateRestElement<
|
|
244
|
+
operand,
|
|
245
|
+
$,
|
|
246
|
+
args
|
|
247
|
+
> extends infer semanticResult
|
|
248
|
+
? semanticResult extends operand
|
|
249
|
+
? Extract<result[number], variadicExpression> extends never
|
|
250
|
+
? head
|
|
251
|
+
: ErrorMessage<multipleVariadicMessage>
|
|
252
|
+
: semanticResult
|
|
253
|
+
: never
|
|
254
|
+
: syntacticResult
|
|
255
|
+
: never
|
|
256
|
+
: validateObjectValue<head, $, args>
|
|
182
257
|
|
|
183
258
|
type semanticallyValidateRestElement<operand, $, args> = inferDefinition<
|
|
184
259
|
operand,
|
|
@@ -186,26 +261,39 @@ type semanticallyValidateRestElement<operand, $, args> = inferDefinition<
|
|
|
186
261
|
args
|
|
187
262
|
> extends infer result
|
|
188
263
|
? result extends never
|
|
189
|
-
?
|
|
264
|
+
? writeNonArraySpreadMessage<operand>
|
|
190
265
|
: isAny<result> extends true
|
|
191
|
-
?
|
|
266
|
+
? writeNonArraySpreadMessage<operand>
|
|
192
267
|
: result extends readonly unknown[]
|
|
193
268
|
? operand
|
|
194
|
-
:
|
|
269
|
+
: writeNonArraySpreadMessage<operand>
|
|
195
270
|
: never
|
|
196
271
|
|
|
197
|
-
export const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
272
|
+
export const writeNonArraySpreadMessage = <operand extends string | TypeNode>(
|
|
273
|
+
operand: operand
|
|
274
|
+
) =>
|
|
275
|
+
`Spread element must be an array (was ${operand})` as writeNonArraySpreadMessage<operand>
|
|
201
276
|
|
|
202
|
-
type
|
|
203
|
-
|
|
204
|
-
|
|
277
|
+
type writeNonArraySpreadMessage<operand> =
|
|
278
|
+
`Spread element must be an array${operand extends string
|
|
279
|
+
? `(was ${operand})`
|
|
280
|
+
: ""}`
|
|
205
281
|
|
|
206
282
|
export const multipleVariadicMesage = `A tuple may have at most one variadic element`
|
|
207
283
|
|
|
208
|
-
type
|
|
284
|
+
type multipleVariadicMessage = typeof multipleVariadicMesage
|
|
285
|
+
|
|
286
|
+
export const requiredPostOptionalMessage = `A required element may not follow an optional element`
|
|
287
|
+
|
|
288
|
+
type requiredPostOptionalMessage = typeof requiredPostOptionalMessage
|
|
289
|
+
|
|
290
|
+
export const optionalPostVariadicMessage = `An optional element may not follow a variadic element`
|
|
291
|
+
|
|
292
|
+
type optionalPostVariadicMessage = typeof optionalPostVariadicMessage
|
|
293
|
+
|
|
294
|
+
export const spreadOptionalMessage = `A spread element cannot be optional`
|
|
295
|
+
|
|
296
|
+
type spreadOptionalMessage = typeof optionalPostVariadicMessage
|
|
209
297
|
|
|
210
298
|
type inferTupleLiteral<
|
|
211
299
|
def extends List,
|
package/scope.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
domainOf,
|
|
11
11
|
hasDomain,
|
|
12
12
|
isThunk,
|
|
13
|
-
|
|
13
|
+
morph,
|
|
14
14
|
throwParseError,
|
|
15
15
|
type Dict,
|
|
16
16
|
type evaluate,
|
|
@@ -399,7 +399,7 @@ export class Scope<r extends Resolutions = any> {
|
|
|
399
399
|
names extends [] ? keyof r["exports"] & string : names[number]
|
|
400
400
|
> {
|
|
401
401
|
return addArkKind(
|
|
402
|
-
|
|
402
|
+
morph(this.export(...names) as Dict, (alias, value) => [
|
|
403
403
|
`#${alias}`,
|
|
404
404
|
value
|
|
405
405
|
]) as never,
|
|
@@ -444,7 +444,10 @@ export class Scope<r extends Resolutions = any> {
|
|
|
444
444
|
}
|
|
445
445
|
const namesToExport = names.length ? names : this.exportedNames
|
|
446
446
|
return addArkKind(
|
|
447
|
-
|
|
447
|
+
morph(namesToExport, (_, name) => [
|
|
448
|
+
name,
|
|
449
|
+
this.exportCache![name]
|
|
450
|
+
]) as never,
|
|
448
451
|
"module"
|
|
449
452
|
) as never
|
|
450
453
|
}
|
|
@@ -458,7 +461,7 @@ const resolutionsOfModule = (typeSet: ExportCache) => {
|
|
|
458
461
|
const v = typeSet[k]
|
|
459
462
|
if (hasArkKind(v, "module")) {
|
|
460
463
|
const innerResolutions = resolutionsOfModule(v as never)
|
|
461
|
-
const prefixedResolutions =
|
|
464
|
+
const prefixedResolutions = morph(innerResolutions, (innerK, innerV) => [
|
|
462
465
|
`${k}.${innerK}`,
|
|
463
466
|
innerV
|
|
464
467
|
])
|
package/type.ts
CHANGED
|
@@ -11,12 +11,13 @@ import {
|
|
|
11
11
|
type extractIn,
|
|
12
12
|
type extractOut,
|
|
13
13
|
type includesMorphs,
|
|
14
|
+
type inferIntersection,
|
|
14
15
|
type inferMorphOut,
|
|
15
16
|
type inferNarrow
|
|
16
17
|
} from "@arktype/schema"
|
|
17
18
|
import {
|
|
18
19
|
Callable,
|
|
19
|
-
|
|
20
|
+
morph,
|
|
20
21
|
type Constructor,
|
|
21
22
|
type Json,
|
|
22
23
|
type conform
|
|
@@ -30,7 +31,6 @@ import {
|
|
|
30
31
|
parseGenericParams,
|
|
31
32
|
type GenericParamsParseError
|
|
32
33
|
} from "./parser/generic.js"
|
|
33
|
-
import type { inferIntersection } from "./parser/semantic/intersections.js"
|
|
34
34
|
import { configureShallowDescendants } from "./parser/shared.js"
|
|
35
35
|
import type {
|
|
36
36
|
IndexOneOperator,
|
|
@@ -280,7 +280,7 @@ export const validateUninstantiatedGeneric = (g: Generic) => {
|
|
|
280
280
|
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
281
281
|
{
|
|
282
282
|
baseName: "generic",
|
|
283
|
-
args:
|
|
283
|
+
args: morph(g.parameters, (_, name) => [name, keywords.unknown])
|
|
284
284
|
}
|
|
285
285
|
)
|
|
286
286
|
return g
|
|
@@ -293,7 +293,7 @@ export const generic = (
|
|
|
293
293
|
) => {
|
|
294
294
|
return Object.assign(
|
|
295
295
|
(...args: unknown[]) => {
|
|
296
|
-
const argNodes =
|
|
296
|
+
const argNodes = morph(parameters, (i, param) => [
|
|
297
297
|
param,
|
|
298
298
|
parseTypeRoot(args[i], scope)
|
|
299
299
|
])
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { MorphAst, Out } from "@arktype/schema"
|
|
2
|
-
import {
|
|
3
|
-
Hkt,
|
|
4
|
-
type List,
|
|
5
|
-
type conform,
|
|
6
|
-
type evaluate,
|
|
7
|
-
type intersectArrays,
|
|
8
|
-
type isAny
|
|
9
|
-
} from "@arktype/util"
|
|
10
|
-
|
|
11
|
-
export type inferIntersection<l, r> = [l] extends [never]
|
|
12
|
-
? never
|
|
13
|
-
: [r] extends [never]
|
|
14
|
-
? never
|
|
15
|
-
: [l & r] extends [never]
|
|
16
|
-
? never
|
|
17
|
-
: isAny<l | r> extends true
|
|
18
|
-
? any
|
|
19
|
-
: l extends MorphAst<infer lIn, infer lOut>
|
|
20
|
-
? r extends MorphAst
|
|
21
|
-
? never
|
|
22
|
-
: (In: evaluate<lIn & r>) => Out<lOut>
|
|
23
|
-
: r extends MorphAst<infer rIn, infer rOut>
|
|
24
|
-
? (In: evaluate<rIn & l>) => Out<rOut>
|
|
25
|
-
: [l, r] extends [object, object]
|
|
26
|
-
? intersectObjects<l, r> extends infer result
|
|
27
|
-
? result
|
|
28
|
-
: never
|
|
29
|
-
: l & r
|
|
30
|
-
|
|
31
|
-
declare class MorphableIntersection extends Hkt.Kind {
|
|
32
|
-
f: (
|
|
33
|
-
In: conform<this[Hkt.key], [l: unknown, r: unknown]>
|
|
34
|
-
) => inferIntersection<(typeof In)[0], (typeof In)[1]>
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
type intersectObjects<l, r> = [l, r] extends [
|
|
38
|
-
infer lList extends List,
|
|
39
|
-
infer rList extends List
|
|
40
|
-
]
|
|
41
|
-
? intersectArrays<lList, rList, MorphableIntersection>
|
|
42
|
-
: evaluate<
|
|
43
|
-
{
|
|
44
|
-
[k in keyof l]: k extends keyof r ? inferIntersection<l[k], r[k]> : l[k]
|
|
45
|
-
} & Omit<r, keyof l>
|
|
46
|
-
>
|