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/__tests__/array.test.ts
CHANGED
|
@@ -1,29 +1,47 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { writeUnresolvableMessage } from "@arktype/schema"
|
|
2
3
|
import { scope, type } from "arktype"
|
|
3
|
-
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
4
4
|
import { incompleteArrayTokenMessage } from "../parser/string/shift/operator/operator.js"
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
multipleVariadicMesage,
|
|
7
|
+
writeNonArraySpreadMessage
|
|
8
8
|
} from "../parser/tuple.js"
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
contextualize(
|
|
11
|
+
"non-tuple",
|
|
12
|
+
() => {
|
|
13
|
+
it("allows and apply", () => {
|
|
13
14
|
const t = type("string[]")
|
|
14
15
|
attest<string[]>(t.infer)
|
|
15
16
|
attest(t.allows([])).equals(true)
|
|
17
|
+
attest(t([])).snap([])
|
|
16
18
|
attest(t.allows(["foo", "bar"])).equals(true)
|
|
19
|
+
attest(t(["foo", "bar"])).snap(["foo", "bar"])
|
|
17
20
|
attest(t.allows(["foo", "bar", 5])).equals(false)
|
|
21
|
+
attest(t(["foo", "bar", 5]).toString()).snap(
|
|
22
|
+
"value at [2] must be a string (was number)"
|
|
23
|
+
)
|
|
18
24
|
attest(t.allows([5, "foo", "bar"])).equals(false)
|
|
25
|
+
attest(t([5, "foo", "bar"]).toString()).snap(
|
|
26
|
+
"value at [0] must be a string (was number)"
|
|
27
|
+
)
|
|
19
28
|
})
|
|
29
|
+
|
|
20
30
|
it("nested", () => {
|
|
21
31
|
const t = type("string[][]")
|
|
22
32
|
attest<string[][]>(t.infer)
|
|
23
33
|
attest(t.allows([])).equals(true)
|
|
34
|
+
attest(t([])).snap([])
|
|
24
35
|
attest(t.allows([["foo"]])).equals(true)
|
|
36
|
+
attest(t([["foo"]])).snap([["foo"]])
|
|
25
37
|
attest(t.allows(["foo"])).equals(false)
|
|
38
|
+
attest(t(["foo"]).toString()).snap(
|
|
39
|
+
"value at [0] must be an array (was string)"
|
|
40
|
+
)
|
|
26
41
|
attest(t.allows([["foo", 5]])).equals(false)
|
|
42
|
+
attest(t([["foo", 5]]).toString()).snap(
|
|
43
|
+
"value at [0][1] must be a string (was number)"
|
|
44
|
+
)
|
|
27
45
|
})
|
|
28
46
|
|
|
29
47
|
it("tuple expression", () => {
|
|
@@ -31,39 +49,7 @@ describe("array", () => {
|
|
|
31
49
|
attest<string[]>(t.infer)
|
|
32
50
|
attest(t.json).equals(type("string[]").json)
|
|
33
51
|
})
|
|
34
|
-
|
|
35
|
-
it("string optional", () => {
|
|
36
|
-
const t = type(["string?"])
|
|
37
|
-
attest<[string?]>(t.infer)
|
|
38
|
-
attest(t.json).equals(type(["string?"]).json)
|
|
39
|
-
})
|
|
40
|
-
it("optional tuple", () => {
|
|
41
|
-
const t = type([["string", "?"]])
|
|
42
|
-
attest<[string?]>(t.infer)
|
|
43
|
-
attest(t.json).equals(type(["string?"]).json)
|
|
44
|
-
})
|
|
45
|
-
it("multi-optional tuple", () => {
|
|
46
|
-
const t = type([["string?", "?"]])
|
|
47
|
-
attest<[string?]>(t.infer)
|
|
48
|
-
attest(t.json).equals(type(["string?"]).json)
|
|
49
|
-
})
|
|
50
|
-
it("nested optional tuple", () => {
|
|
51
|
-
const t = type([["string?"], "string?"])
|
|
52
|
-
attest<[[string?], string?]>(t.infer)
|
|
53
|
-
})
|
|
54
|
-
it("shallow optional string", () => {
|
|
55
|
-
// @ts-expect-error
|
|
56
|
-
attest(() => type("string?")).throwsAndHasTypeError(
|
|
57
|
-
writeUnresolvableMessage("string?")
|
|
58
|
-
)
|
|
59
|
-
})
|
|
60
|
-
it("shallow optional tuple", () => {
|
|
61
|
-
// @ts-expect-error
|
|
62
|
-
attest(() => type(["string", "?"])).throws(
|
|
63
|
-
writeUnresolvableMessage("?")
|
|
64
|
-
)
|
|
65
|
-
})
|
|
66
|
-
})
|
|
52
|
+
|
|
67
53
|
it("root expression", () => {
|
|
68
54
|
const t = type("string", "[]")
|
|
69
55
|
attest<string[]>(t.infer)
|
|
@@ -79,24 +65,37 @@ describe("array", () => {
|
|
|
79
65
|
writeUnresolvableMessage("hmm")
|
|
80
66
|
)
|
|
81
67
|
})
|
|
68
|
+
|
|
82
69
|
it("incomplete token", () => {
|
|
83
70
|
// @ts-expect-error
|
|
84
71
|
attest(() => type("string[")).throwsAndHasTypeError(
|
|
85
72
|
incompleteArrayTokenMessage
|
|
86
73
|
)
|
|
87
74
|
})
|
|
88
|
-
}
|
|
89
|
-
|
|
75
|
+
},
|
|
76
|
+
"non-variadic tuple",
|
|
77
|
+
() => {
|
|
90
78
|
it("shallow", () => {
|
|
91
79
|
const t = type(["string", "number"])
|
|
92
80
|
attest<[string, number]>(t.infer)
|
|
93
81
|
attest(t.allows(["", 0])).equals(true)
|
|
82
|
+
attest(t(["", 0])).snap(["", 0])
|
|
94
83
|
attest(t.allows([true, 0])).equals(false)
|
|
84
|
+
attest(t([true, 0]).toString()).snap(
|
|
85
|
+
"value at [0] must be a string (was true)"
|
|
86
|
+
)
|
|
95
87
|
attest(t.allows([0, false])).equals(false)
|
|
88
|
+
attest(t([0, false]).toString())
|
|
89
|
+
.snap(`value at [0] must be a string (was number)
|
|
90
|
+
value at [1] must be a number (was false)`)
|
|
96
91
|
// too short
|
|
97
92
|
attest(t.allows([""])).equals(false)
|
|
93
|
+
attest(t([""]).toString()).snap('must be exactly length 2 (was [""])')
|
|
98
94
|
// too long
|
|
99
95
|
attest(t.allows(["", 0, 1])).equals(false)
|
|
96
|
+
attest(t(["", 0, 1]).toString()).snap(
|
|
97
|
+
'must be exactly length 2 (was ["",0,1])'
|
|
98
|
+
)
|
|
100
99
|
// non-array
|
|
101
100
|
attest(
|
|
102
101
|
t.allows({
|
|
@@ -105,105 +104,159 @@ describe("array", () => {
|
|
|
105
104
|
1: 0
|
|
106
105
|
})
|
|
107
106
|
).equals(false)
|
|
107
|
+
attest(
|
|
108
|
+
t({
|
|
109
|
+
length: 2,
|
|
110
|
+
0: "",
|
|
111
|
+
1: 0
|
|
112
|
+
}).toString()
|
|
113
|
+
).snap("must be an array (was object)")
|
|
108
114
|
})
|
|
115
|
+
|
|
109
116
|
it("nested", () => {
|
|
110
|
-
const t = type([["string", "number"], [{ a: "
|
|
117
|
+
const t = type([["string", "number"], [{ a: "bigint", b: ["null"] }]])
|
|
111
118
|
attest<
|
|
112
119
|
[
|
|
113
120
|
[string, number],
|
|
114
121
|
[
|
|
115
122
|
{
|
|
116
|
-
a:
|
|
123
|
+
a: bigint
|
|
117
124
|
b: [null]
|
|
118
125
|
}
|
|
119
126
|
]
|
|
120
127
|
]
|
|
121
128
|
>(t.infer)
|
|
129
|
+
const valid: typeof t.infer = [["", 0], [{ a: 0n, b: [null] }]]
|
|
130
|
+
attest(t.allows(valid)).equals(true)
|
|
131
|
+
attest(t(valid)).equals(valid)
|
|
132
|
+
const invalid = [["", 0], [{ a: 0n, b: [undefined] }]]
|
|
133
|
+
attest(t.allows(invalid)).equals(false)
|
|
134
|
+
attest(t(invalid).toString()).snap(
|
|
135
|
+
"value at [1][0].b[0] must be null (was undefined)"
|
|
136
|
+
)
|
|
137
|
+
})
|
|
122
138
|
|
|
123
|
-
|
|
124
|
-
|
|
139
|
+
it("optional tuple", () => {
|
|
140
|
+
const t = type(["string", "?"])
|
|
141
|
+
attest<[string?]>(t.infer)
|
|
142
|
+
attest(t([])).equals([])
|
|
143
|
+
attest(t(["foo"])).equals(["foo"])
|
|
144
|
+
attest(t([5]).toString()).snap(
|
|
145
|
+
"value at [0] must be a string (was number)"
|
|
146
|
+
)
|
|
147
|
+
attest(t(["foo", "bar"]).toString()).snap(
|
|
148
|
+
"must be at most length 1 (was 2)"
|
|
149
|
+
)
|
|
125
150
|
})
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
151
|
+
|
|
152
|
+
it("nested optional tuple", () => {
|
|
153
|
+
const t = type([["string", "?"], "string", "?"])
|
|
154
|
+
attest<[[string?], string?]>(t.infer)
|
|
155
|
+
})
|
|
156
|
+
},
|
|
157
|
+
"variadic tuple",
|
|
158
|
+
() => {
|
|
159
|
+
it("spreads simple arrays", () => {
|
|
160
|
+
const wellRested = type(["string", "...", "number[]"])
|
|
161
|
+
attest<[string, ...number[]]>(wellRested.infer)
|
|
162
|
+
attest(wellRested(["foo"])).equals(["foo"])
|
|
163
|
+
attest(wellRested(["foo", 1, 2])).equals(["foo", 1, 2])
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it("spreads array expressions", () => {
|
|
167
|
+
const greatSpread = type(["0", "...", "(Date|RegExp)[]"])
|
|
168
|
+
attest<[0, ...(RegExp | Date)[]]>(greatSpread.infer)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
it("distributes spread unions", () => {
|
|
172
|
+
const t = type(["1", "...", "(Date[] | RegExp[])"])
|
|
173
|
+
attest<[1, ...(Date[] | RegExp[])]>(t.infer)
|
|
174
|
+
const expected = type(["1", "...", "Date[]"]).or(["1", "...", "RegExp[]"])
|
|
175
|
+
attest(t.json).equals(expected.json)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it("distributes spread union tuples", () => {
|
|
179
|
+
const counting = ["2", "3", "4"] as const
|
|
180
|
+
const fibbing = ["1", "2", "3", "5", "8"] as const
|
|
181
|
+
const countOrFib = type(counting, "|", fibbing)
|
|
182
|
+
attest<[2, 3, 4] | [1, 2, 3, 5, 8]>(countOrFib.infer)
|
|
183
|
+
const t = type(["1", "...", countOrFib])
|
|
184
|
+
attest<[1, 2, 3, 4] | [1, 1, 2, 3, 5, 8]>(t.infer)
|
|
185
|
+
const expected = type(["1", ...counting]).or(["1", ...fibbing])
|
|
186
|
+
attest(t.json).equals(expected.json)
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it("allows array keyword", () => {
|
|
190
|
+
const types = scope({
|
|
191
|
+
myArrayKeyword: "boolean[]",
|
|
192
|
+
myVariadicKeyword: ["string", "...", "myArrayKeyword"]
|
|
193
|
+
}).export()
|
|
194
|
+
attest<[string, ...boolean[]]>(types.myVariadicKeyword.infer)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it("errors on non-array", () => {
|
|
198
|
+
attest(() =>
|
|
199
|
+
// @ts-expect-error
|
|
200
|
+
type(["number", "...", "string"])
|
|
201
|
+
).throwsAndHasTypeError(writeNonArraySpreadMessage("string"))
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it("allows multiple fixed spreads", () => {
|
|
205
|
+
const t = type([
|
|
206
|
+
"string",
|
|
207
|
+
"...",
|
|
208
|
+
"number[]",
|
|
209
|
+
"...",
|
|
210
|
+
["boolean", "bigint"],
|
|
211
|
+
"...",
|
|
212
|
+
["symbol"]
|
|
213
|
+
])
|
|
214
|
+
const expected = type([
|
|
215
|
+
"string",
|
|
216
|
+
"...",
|
|
217
|
+
"number[]",
|
|
218
|
+
"boolean",
|
|
219
|
+
"bigint",
|
|
220
|
+
"symbol"
|
|
221
|
+
])
|
|
222
|
+
attest<[string, ...number[], boolean, bigint, symbol]>(t.infer)
|
|
223
|
+
attest<typeof expected.infer>(t.infer)
|
|
224
|
+
attest(t.json).equals(expected.json)
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
it("errors on multiple variadic", () => {
|
|
228
|
+
attest(() =>
|
|
229
|
+
type([
|
|
230
|
+
"...",
|
|
231
|
+
"string[]",
|
|
176
232
|
// @ts-expect-error
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
233
|
+
"...",
|
|
234
|
+
"number[]"
|
|
235
|
+
])
|
|
236
|
+
).throwsAndHasTypeError(multipleVariadicMesage)
|
|
180
237
|
})
|
|
181
|
-
}
|
|
182
|
-
|
|
238
|
+
},
|
|
239
|
+
"intersection",
|
|
240
|
+
() => {
|
|
183
241
|
it("shallow array intersection", () => {
|
|
184
|
-
const
|
|
185
|
-
const expected = type("'foo'[]")
|
|
186
|
-
attest(
|
|
242
|
+
const t = type("string[]&'foo'[]")
|
|
243
|
+
const expected = type("'foo'[]")
|
|
244
|
+
attest(t.json).equals(expected.json)
|
|
187
245
|
})
|
|
246
|
+
|
|
188
247
|
it("deep array intersection", () => {
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
]).json
|
|
193
|
-
const expected = type([{ a: "string", b: "number" }, "[]"]).json
|
|
194
|
-
attest(actual).is(expected)
|
|
248
|
+
const t = type([{ a: "string" }, "[]"]).and([{ b: "number" }, "[]"])
|
|
249
|
+
const expected = type([{ a: "string", b: "number" }, "[]"])
|
|
250
|
+
attest(t.json).equals(expected.json)
|
|
195
251
|
})
|
|
252
|
+
|
|
196
253
|
it("tuple intersection", () => {
|
|
197
254
|
const t = type([[{ a: "string" }], "&", [{ b: "boolean" }]])
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
a: string
|
|
202
|
-
b: boolean
|
|
203
|
-
}
|
|
204
|
-
]
|
|
205
|
-
>(t.infer)
|
|
255
|
+
const expected = type([{ a: "string", b: "boolean" }])
|
|
256
|
+
attest<typeof expected>(t)
|
|
257
|
+
attest(t.json).equals(expected.json)
|
|
206
258
|
})
|
|
259
|
+
|
|
207
260
|
it("tuple and array", () => {
|
|
208
261
|
const tupleAndArray = type([
|
|
209
262
|
[{ a: "string" }],
|
|
@@ -215,31 +268,19 @@ describe("array", () => {
|
|
|
215
268
|
"&",
|
|
216
269
|
[{ a: "string" }]
|
|
217
270
|
])
|
|
218
|
-
attest<
|
|
219
|
-
[
|
|
220
|
-
{
|
|
221
|
-
a: string
|
|
222
|
-
b: boolean
|
|
223
|
-
}
|
|
224
|
-
]
|
|
225
|
-
>(tupleAndArray.infer)
|
|
226
271
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
b: boolean
|
|
232
|
-
}
|
|
233
|
-
]
|
|
234
|
-
>(arrayAndTuple.infer)
|
|
272
|
+
const expected = type([{ a: "string", b: "boolean" }])
|
|
273
|
+
attest<typeof expected>(tupleAndArray)
|
|
274
|
+
|
|
275
|
+
attest<typeof expected>(arrayAndTuple)
|
|
235
276
|
|
|
236
|
-
|
|
237
|
-
attest(
|
|
238
|
-
attest(arrayAndTuple.json).is(expected)
|
|
277
|
+
attest(tupleAndArray.json).equals(expected.json)
|
|
278
|
+
attest(arrayAndTuple.json).equals(expected.json)
|
|
239
279
|
})
|
|
280
|
+
|
|
240
281
|
it("variadic and tuple", () => {
|
|
241
282
|
const b = type([{ b: "boolean" }, "[]"])
|
|
242
|
-
const t = type([{ a: "string" },
|
|
283
|
+
const t = type([{ a: "string" }, "...", b]).and([
|
|
243
284
|
{ c: "number" },
|
|
244
285
|
{ d: "Date" }
|
|
245
286
|
])
|
|
@@ -249,24 +290,102 @@ describe("array", () => {
|
|
|
249
290
|
])
|
|
250
291
|
attest(t.json).equals(expected.json)
|
|
251
292
|
})
|
|
293
|
+
|
|
252
294
|
it("variadic and array", () => {
|
|
253
295
|
const b = type({ b: "boolean" }, "[]")
|
|
254
|
-
const t = type([{ a: "string" },
|
|
296
|
+
const t = type([{ a: "string" }, "...", b]).and([{ c: "number" }, "[]"])
|
|
255
297
|
const expected = type([
|
|
256
298
|
{ a: "string", c: "number" },
|
|
257
|
-
|
|
299
|
+
"...",
|
|
300
|
+
[{ b: "boolean", c: "number" }, "[]"]
|
|
258
301
|
])
|
|
259
302
|
attest<typeof expected.infer>(t.infer)
|
|
260
303
|
attest(t.json).equals(expected.json)
|
|
261
304
|
})
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
305
|
+
|
|
306
|
+
// based on the equivalent type-level test from @arktype/util
|
|
307
|
+
it("kitchen sink", () => {
|
|
308
|
+
const l = type([
|
|
309
|
+
{ a: "0" },
|
|
310
|
+
{ b: "1" },
|
|
311
|
+
"?",
|
|
312
|
+
{ c: "2" },
|
|
313
|
+
"?",
|
|
314
|
+
"...",
|
|
315
|
+
[{ d: "3" }, "[]"]
|
|
316
|
+
])
|
|
317
|
+
const r = type([
|
|
318
|
+
{ e: "4" },
|
|
319
|
+
"?",
|
|
320
|
+
{ f: "5" },
|
|
321
|
+
"?",
|
|
322
|
+
"...",
|
|
323
|
+
[{ g: "6" }, "[]"]
|
|
324
|
+
])
|
|
325
|
+
const result = l.and(r)
|
|
326
|
+
|
|
327
|
+
const expected = type([
|
|
328
|
+
{ a: "0", e: "4" },
|
|
329
|
+
{ b: "1", f: "5" },
|
|
330
|
+
"?",
|
|
331
|
+
{ c: "2", g: "6" },
|
|
332
|
+
"?",
|
|
333
|
+
"...",
|
|
334
|
+
[{ d: "3", g: "6" }, "[]"]
|
|
335
|
+
])
|
|
336
|
+
|
|
337
|
+
attest(expected.expression).snap(
|
|
338
|
+
"[{ a: 0, e: 4 }, { b: 1, f: 5 }?, { c: 2, g: 6 }?, ...{ d: 3, g: 6 }[]]"
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
attest<typeof expected>(result)
|
|
342
|
+
attest(result.json).equals(expected.json)
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
it("prefix and postfix", () => {
|
|
346
|
+
const l = type(["...", [{ a: "0" }, "[]"], { b: "0" }, { c: "0" }])
|
|
347
|
+
const r = type([{ x: "0" }, { y: "0" }, "...", [{ z: "0" }, "[]"]])
|
|
348
|
+
|
|
349
|
+
// currently getting this "Expected" result at a type-level incurs
|
|
350
|
+
// too high a performance cost for such a niche intersection.
|
|
351
|
+
const expected = type([
|
|
352
|
+
{ a: "0", x: "0" },
|
|
353
|
+
{ a: "0", y: "0" },
|
|
354
|
+
"...",
|
|
355
|
+
[{ a: "0", z: "0" }, "[]"],
|
|
356
|
+
{ b: "0", z: "0" },
|
|
357
|
+
{ c: "0", z: "0" }
|
|
358
|
+
])
|
|
359
|
+
.or([
|
|
360
|
+
{ a: "0", x: "0" },
|
|
361
|
+
{ b: "0", y: "0" },
|
|
362
|
+
{ c: "0", z: "0" }
|
|
363
|
+
])
|
|
364
|
+
.or([
|
|
365
|
+
{ b: "0", x: "0" },
|
|
366
|
+
{ c: "0", y: "0" }
|
|
367
|
+
])
|
|
368
|
+
|
|
369
|
+
const lrResult = l.and(r)
|
|
370
|
+
attest(lrResult.json).snap(expected.json)
|
|
371
|
+
const rlResult = r.and(l)
|
|
372
|
+
attest(rlResult.json).snap(expected.json)
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
it("reduces minLength", () => {
|
|
376
|
+
const t = type(["number", "number", "...", "number[]", "number"])
|
|
377
|
+
const expected = type("number[]>=3")
|
|
378
|
+
attest(t.json).equals(expected.json)
|
|
379
|
+
})
|
|
380
|
+
}
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
// TODO: reenable
|
|
384
|
+
// describe("traversal", () => {
|
|
385
|
+
// it("multiple errors", () => {
|
|
386
|
+
// const stringArray = type("string[]")
|
|
387
|
+
// attest(stringArray([1, 2]).toString()).snap(
|
|
388
|
+
// "Item at index 0 must be a string (was number)\nItem at index 1 must be a string (was number)"
|
|
389
|
+
// )
|
|
390
|
+
// })
|
|
391
|
+
// })
|
|
@@ -1,49 +1,56 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import { type } from "arktype"
|
|
3
3
|
import { writeBadDefinitionTypeMessage } from "../parser/definition.js"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
contextualize(() => {
|
|
6
6
|
it("undefined", () => {
|
|
7
7
|
// @ts-expect-error
|
|
8
8
|
attest(() => type({ bad: undefined })).throwsAndHasTypeError(
|
|
9
9
|
writeBadDefinitionTypeMessage("undefined")
|
|
10
10
|
)
|
|
11
11
|
})
|
|
12
|
+
|
|
12
13
|
it("null", () => {
|
|
13
14
|
// @ts-expect-error
|
|
14
15
|
attest(() => type({ bad: null })).throwsAndHasTypeError(
|
|
15
16
|
writeBadDefinitionTypeMessage("null")
|
|
16
17
|
)
|
|
17
18
|
})
|
|
19
|
+
|
|
18
20
|
it("boolean", () => {
|
|
19
21
|
// @ts-expect-error
|
|
20
22
|
attest(() => type({ bad: true })).throwsAndHasTypeError(
|
|
21
23
|
writeBadDefinitionTypeMessage("boolean")
|
|
22
24
|
)
|
|
23
25
|
})
|
|
26
|
+
|
|
24
27
|
it("number", () => {
|
|
25
28
|
// @ts-expect-error
|
|
26
29
|
attest(() => type({ bad: 5 })).throwsAndHasTypeError(
|
|
27
30
|
writeBadDefinitionTypeMessage("number")
|
|
28
31
|
)
|
|
29
32
|
})
|
|
33
|
+
|
|
30
34
|
it("bigint", () => {
|
|
31
35
|
// @ts-expect-error
|
|
32
36
|
attest(() => type({ bad: 99999n })).throwsAndHasTypeError(
|
|
33
37
|
writeBadDefinitionTypeMessage("bigint")
|
|
34
38
|
)
|
|
35
39
|
})
|
|
40
|
+
|
|
36
41
|
it("symbol", () => {
|
|
37
42
|
// @ts-expect-error
|
|
38
43
|
attest(() => type({ bad: Symbol() })).throwsAndHasTypeError(
|
|
39
44
|
writeBadDefinitionTypeMessage("symbol")
|
|
40
45
|
)
|
|
41
46
|
})
|
|
47
|
+
|
|
42
48
|
it("any", () => {
|
|
43
49
|
// doesn't error, so this test is just to ensure it doesn't infinitely recurse
|
|
44
50
|
const t = type({ bad: {} as any })
|
|
45
|
-
attest<{}>(t.infer)
|
|
51
|
+
attest<{ bad: any }>(t.infer)
|
|
46
52
|
})
|
|
53
|
+
|
|
47
54
|
it("unknown", () => {
|
|
48
55
|
// @ts-expect-error just results in base completions, so we just check there's an error
|
|
49
56
|
attest(() => type({ bad: {} as unknown })).type.errors("")
|
package/__tests__/basis.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import { type } from "arktype"
|
|
3
3
|
import { writeUnsatisfiableExpressionError } from "../parser/semantic/validate.js"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
contextualize("intersections", () => {
|
|
6
6
|
it("class & literal", () => {
|
|
7
7
|
const a = [0]
|
|
8
8
|
const literal = type("===", a)
|
|
@@ -14,6 +14,7 @@ describe("basis intersections", () => {
|
|
|
14
14
|
attest<number[]>(rl.infer)
|
|
15
15
|
attest(rl.json).equals(literal.json)
|
|
16
16
|
})
|
|
17
|
+
|
|
17
18
|
it("unsatisfiable class & literal", () => {
|
|
18
19
|
const a = [0]
|
|
19
20
|
const literal = type("===", a)
|
|
@@ -21,12 +22,14 @@ describe("basis intersections", () => {
|
|
|
21
22
|
attest(() => literal.and(cls)).throws(writeUnsatisfiableExpressionError(""))
|
|
22
23
|
attest(() => cls.and(literal)).throws(writeUnsatisfiableExpressionError(""))
|
|
23
24
|
})
|
|
25
|
+
|
|
24
26
|
it("domain & literal", () => {
|
|
25
27
|
const literal = type("'foo'")
|
|
26
28
|
const domain = type("string")
|
|
27
29
|
attest(literal.and(domain).json).equals(literal.json)
|
|
28
30
|
attest(domain.and(literal).json).equals(literal.json)
|
|
29
31
|
})
|
|
32
|
+
|
|
30
33
|
it("unsatisfiable domain & literal", () => {
|
|
31
34
|
const literal = type("'foo'")
|
|
32
35
|
const domain = type("number")
|
|
@@ -37,6 +40,7 @@ describe("basis intersections", () => {
|
|
|
37
40
|
writeUnsatisfiableExpressionError("")
|
|
38
41
|
)
|
|
39
42
|
})
|
|
43
|
+
|
|
40
44
|
it("domain & class", () => {
|
|
41
45
|
const domain = type("object")
|
|
42
46
|
const cls = type("instanceof", Date)
|