arktype 1.1.0-dev-2 → 2.0.0-dev.1
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
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { scope, type } from "arktype"
|
|
3
|
+
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
4
|
+
import { incompleteArrayTokenMessage } from "../parser/string/shift/operator/operator.js"
|
|
5
|
+
import {
|
|
6
|
+
prematureRestMessage,
|
|
7
|
+
writeNonArrayRestMessage
|
|
8
|
+
} from "../parser/tuple.js"
|
|
9
|
+
|
|
10
|
+
describe("array", () => {
|
|
11
|
+
describe("base", () => {
|
|
12
|
+
it("base", () => {
|
|
13
|
+
const t = type("string[]")
|
|
14
|
+
attest<string[]>(t.infer)
|
|
15
|
+
attest(t.allows([])).equals(true)
|
|
16
|
+
attest(t.allows(["foo", "bar"])).equals(true)
|
|
17
|
+
attest(t.allows(["foo", "bar", 5])).equals(false)
|
|
18
|
+
attest(t.allows([5, "foo", "bar"])).equals(false)
|
|
19
|
+
})
|
|
20
|
+
it("nested", () => {
|
|
21
|
+
const t = type("string[][]")
|
|
22
|
+
attest<string[][]>(t.infer)
|
|
23
|
+
attest(t.allows([])).equals(true)
|
|
24
|
+
attest(t.allows([["foo"]])).equals(true)
|
|
25
|
+
attest(t.allows(["foo"])).equals(false)
|
|
26
|
+
attest(t.allows([["foo", 5]])).equals(false)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it("tuple expression", () => {
|
|
30
|
+
const t = type(["string", "[]"])
|
|
31
|
+
attest<string[]>(t.infer)
|
|
32
|
+
attest(t.json).equals(type("string[]").json)
|
|
33
|
+
})
|
|
34
|
+
describe("optional tuple literals", () => {
|
|
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
|
+
})
|
|
67
|
+
it("root expression", () => {
|
|
68
|
+
const t = type("string", "[]")
|
|
69
|
+
attest<string[]>(t.infer)
|
|
70
|
+
attest(t.json).equals(type("string[]").json)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it("chained", () => {
|
|
74
|
+
const t = type({ a: "string" }).array()
|
|
75
|
+
attest<{ a: string }[]>(t.infer)
|
|
76
|
+
|
|
77
|
+
// @ts-expect-error
|
|
78
|
+
attest(() => type({ a: "hmm" }).array()).throwsAndHasTypeError(
|
|
79
|
+
writeUnresolvableMessage("hmm")
|
|
80
|
+
)
|
|
81
|
+
})
|
|
82
|
+
it("incomplete token", () => {
|
|
83
|
+
// @ts-expect-error
|
|
84
|
+
attest(() => type("string[")).throwsAndHasTypeError(
|
|
85
|
+
incompleteArrayTokenMessage
|
|
86
|
+
)
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
describe("non-variadic tuple", () => {
|
|
90
|
+
it("shallow", () => {
|
|
91
|
+
const t = type(["string", "number"])
|
|
92
|
+
attest<[string, number]>(t.infer)
|
|
93
|
+
attest(t.allows(["", 0])).equals(true)
|
|
94
|
+
attest(t.allows([true, 0])).equals(false)
|
|
95
|
+
attest(t.allows([0, false])).equals(false)
|
|
96
|
+
// too short
|
|
97
|
+
attest(t.allows([""])).equals(false)
|
|
98
|
+
// too long
|
|
99
|
+
attest(t.allows(["", 0, 1])).equals(false)
|
|
100
|
+
// non-array
|
|
101
|
+
attest(
|
|
102
|
+
t.allows({
|
|
103
|
+
length: 2,
|
|
104
|
+
0: "",
|
|
105
|
+
1: 0
|
|
106
|
+
})
|
|
107
|
+
).equals(false)
|
|
108
|
+
})
|
|
109
|
+
it("nested", () => {
|
|
110
|
+
const t = type([["string", "number"], [{ a: "boolean", b: ["null"] }]])
|
|
111
|
+
attest<
|
|
112
|
+
[
|
|
113
|
+
[string, number],
|
|
114
|
+
[
|
|
115
|
+
{
|
|
116
|
+
a: boolean
|
|
117
|
+
b: [null]
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
]
|
|
121
|
+
>(t.infer)
|
|
122
|
+
|
|
123
|
+
attest(t.allows([["", 0], [{ a: true, b: [null] }]])).equals(true)
|
|
124
|
+
attest(t.allows([["", 0], [{ a: true, b: [undefined] }]])).equals(false)
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
describe("variadic tuple", () => {
|
|
128
|
+
describe("variadic", () => {
|
|
129
|
+
it("spreads simple arrays", () => {
|
|
130
|
+
const wellRested = type(["string", "...number[]"])
|
|
131
|
+
attest<[string, ...number[]]>(wellRested.infer)
|
|
132
|
+
attest(wellRested(["foo"]).out).equals(["foo"])
|
|
133
|
+
attest(wellRested(["foo", 1, 2]).out).equals(["foo", 1, 2])
|
|
134
|
+
})
|
|
135
|
+
it("tuple expression", () => {
|
|
136
|
+
const wellRestedTuple = type([
|
|
137
|
+
"number",
|
|
138
|
+
["...", [{ a: "string" }, "[]"]]
|
|
139
|
+
])
|
|
140
|
+
attest<[number, ...{ a: string }[]]>(wellRestedTuple.infer)
|
|
141
|
+
})
|
|
142
|
+
it("spreads array expressions", () => {
|
|
143
|
+
const greatSpread = type([{ a: "boolean" }, "...(Date|RegExp)[]"])
|
|
144
|
+
attest<
|
|
145
|
+
[
|
|
146
|
+
{
|
|
147
|
+
a: boolean
|
|
148
|
+
},
|
|
149
|
+
...(RegExp | Date)[]
|
|
150
|
+
]
|
|
151
|
+
>(greatSpread.infer)
|
|
152
|
+
})
|
|
153
|
+
it("allows array keyword", () => {
|
|
154
|
+
const types = scope({
|
|
155
|
+
myArrayKeyword: "boolean[]",
|
|
156
|
+
myVariadicKeyword: ["string", "...myArrayKeyword"]
|
|
157
|
+
}).export()
|
|
158
|
+
attest<[string, ...boolean[]]>(types.myVariadicKeyword.infer)
|
|
159
|
+
})
|
|
160
|
+
it("errors on non-array", () => {
|
|
161
|
+
attest(() =>
|
|
162
|
+
// @ts-expect-error
|
|
163
|
+
type(["email", "...symbol"])
|
|
164
|
+
).throwsAndHasTypeError(writeNonArrayRestMessage("symbol"))
|
|
165
|
+
attest(() =>
|
|
166
|
+
// @ts-expect-error
|
|
167
|
+
type(["number", ["...", "string"]])
|
|
168
|
+
).throwsAndHasTypeError(writeNonArrayRestMessage("string"))
|
|
169
|
+
})
|
|
170
|
+
it("errors on non-last element", () => {
|
|
171
|
+
attest(() =>
|
|
172
|
+
// @ts-expect-error
|
|
173
|
+
type(["...number[]", "string"])
|
|
174
|
+
).throwsAndHasTypeError(prematureRestMessage)
|
|
175
|
+
attest(() =>
|
|
176
|
+
// @ts-expect-error
|
|
177
|
+
type([["...", "string[]"], "number"])
|
|
178
|
+
).throwsAndHasTypeError(prematureRestMessage)
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
})
|
|
182
|
+
describe("intersection", () => {
|
|
183
|
+
it("shallow array intersection", () => {
|
|
184
|
+
const actual = type("string[]&'foo'[]").json
|
|
185
|
+
const expected = type("'foo'[]").json
|
|
186
|
+
attest(actual).is(expected)
|
|
187
|
+
})
|
|
188
|
+
it("deep array intersection", () => {
|
|
189
|
+
const actual = type([{ a: "string" }, "[]"]).and([
|
|
190
|
+
{ b: "number" },
|
|
191
|
+
"[]"
|
|
192
|
+
]).json
|
|
193
|
+
const expected = type([{ a: "string", b: "number" }, "[]"]).json
|
|
194
|
+
attest(actual).is(expected)
|
|
195
|
+
})
|
|
196
|
+
it("tuple intersection", () => {
|
|
197
|
+
const t = type([[{ a: "string" }], "&", [{ b: "boolean" }]])
|
|
198
|
+
attest<
|
|
199
|
+
[
|
|
200
|
+
{
|
|
201
|
+
a: string
|
|
202
|
+
b: boolean
|
|
203
|
+
}
|
|
204
|
+
]
|
|
205
|
+
>(t.infer)
|
|
206
|
+
})
|
|
207
|
+
it("tuple and array", () => {
|
|
208
|
+
const tupleAndArray = type([
|
|
209
|
+
[{ a: "string" }],
|
|
210
|
+
"&",
|
|
211
|
+
[{ b: "boolean" }, "[]"]
|
|
212
|
+
])
|
|
213
|
+
const arrayAndTuple = type([
|
|
214
|
+
[{ b: "boolean" }, "[]"],
|
|
215
|
+
"&",
|
|
216
|
+
[{ a: "string" }]
|
|
217
|
+
])
|
|
218
|
+
attest<
|
|
219
|
+
[
|
|
220
|
+
{
|
|
221
|
+
a: string
|
|
222
|
+
b: boolean
|
|
223
|
+
}
|
|
224
|
+
]
|
|
225
|
+
>(tupleAndArray.infer)
|
|
226
|
+
|
|
227
|
+
attest<
|
|
228
|
+
[
|
|
229
|
+
{
|
|
230
|
+
a: string
|
|
231
|
+
b: boolean
|
|
232
|
+
}
|
|
233
|
+
]
|
|
234
|
+
>(arrayAndTuple.infer)
|
|
235
|
+
|
|
236
|
+
const expected = type([{ a: "string", b: "boolean" }]).json
|
|
237
|
+
attest(tupleAndArray.json).is(expected)
|
|
238
|
+
attest(arrayAndTuple.json).is(expected)
|
|
239
|
+
})
|
|
240
|
+
it("variadic and tuple", () => {
|
|
241
|
+
const b = type([{ b: "boolean" }, "[]"])
|
|
242
|
+
const t = type([{ a: "string" }, ["...", b]]).and([
|
|
243
|
+
{ c: "number" },
|
|
244
|
+
{ d: "Date" }
|
|
245
|
+
])
|
|
246
|
+
const expected = type([
|
|
247
|
+
{ a: "string", c: "number" },
|
|
248
|
+
{ b: "boolean", d: "Date" }
|
|
249
|
+
])
|
|
250
|
+
attest(t.json).equals(expected.json)
|
|
251
|
+
})
|
|
252
|
+
it("variadic and array", () => {
|
|
253
|
+
const b = type({ b: "boolean" }, "[]")
|
|
254
|
+
const t = type([{ a: "string" }, ["...", b]]).and([{ c: "number" }, "[]"])
|
|
255
|
+
const expected = type([
|
|
256
|
+
{ a: "string", c: "number" },
|
|
257
|
+
["...", [{ b: "boolean", c: "number" }, "[]"]]
|
|
258
|
+
])
|
|
259
|
+
attest<typeof expected.infer>(t.infer)
|
|
260
|
+
attest(t.json).equals(expected.json)
|
|
261
|
+
})
|
|
262
|
+
})
|
|
263
|
+
// TODO: reenable
|
|
264
|
+
// describe("traversal", () => {
|
|
265
|
+
// it("multiple errors", () => {
|
|
266
|
+
// const stringArray = type("string[]")
|
|
267
|
+
// attest(stringArray([1, 2]).errors?.summary).snap(
|
|
268
|
+
// "Item at index 0 must be a string (was number)\nItem at index 1 must be a string (was number)"
|
|
269
|
+
// )
|
|
270
|
+
// })
|
|
271
|
+
// })
|
|
272
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import type { astToString } from "../parser/semantic/utils.js"
|
|
3
|
+
|
|
4
|
+
describe("astToString", () => {
|
|
5
|
+
it("no parentheses if nested ast is an array", () => {
|
|
6
|
+
const t = {} as unknown as astToString<[["number", "[]"], "|", "number"]>
|
|
7
|
+
attest<"'number[]|number'">(t)
|
|
8
|
+
})
|
|
9
|
+
it("parentheses if nested ast is an infix expression", () => {
|
|
10
|
+
const t = {} as unknown as astToString<[["0", "|", "1"], "|", "string"]>
|
|
11
|
+
attest<"'(0|1)|string'">(t)
|
|
12
|
+
})
|
|
13
|
+
it("defaults to '...' if input is bad", () => {
|
|
14
|
+
const t = {} as unknown as astToString<["0", "///", "1"]>
|
|
15
|
+
attest<"'...'">(t)
|
|
16
|
+
})
|
|
17
|
+
})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { type } from "arktype"
|
|
3
|
+
import { writeBadDefinitionTypeMessage } from "../parser/definition.js"
|
|
4
|
+
|
|
5
|
+
describe("definitions", () => {
|
|
6
|
+
it("undefined", () => {
|
|
7
|
+
// @ts-expect-error
|
|
8
|
+
attest(() => type({ bad: undefined })).throwsAndHasTypeError(
|
|
9
|
+
writeBadDefinitionTypeMessage("undefined")
|
|
10
|
+
)
|
|
11
|
+
})
|
|
12
|
+
it("null", () => {
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
attest(() => type({ bad: null })).throwsAndHasTypeError(
|
|
15
|
+
writeBadDefinitionTypeMessage("null")
|
|
16
|
+
)
|
|
17
|
+
})
|
|
18
|
+
it("boolean", () => {
|
|
19
|
+
// @ts-expect-error
|
|
20
|
+
attest(() => type({ bad: true })).throwsAndHasTypeError(
|
|
21
|
+
writeBadDefinitionTypeMessage("boolean")
|
|
22
|
+
)
|
|
23
|
+
})
|
|
24
|
+
it("number", () => {
|
|
25
|
+
// @ts-expect-error
|
|
26
|
+
attest(() => type({ bad: 5 })).throwsAndHasTypeError(
|
|
27
|
+
writeBadDefinitionTypeMessage("number")
|
|
28
|
+
)
|
|
29
|
+
})
|
|
30
|
+
it("bigint", () => {
|
|
31
|
+
// @ts-expect-error
|
|
32
|
+
attest(() => type({ bad: 99999n })).throwsAndHasTypeError(
|
|
33
|
+
writeBadDefinitionTypeMessage("bigint")
|
|
34
|
+
)
|
|
35
|
+
})
|
|
36
|
+
it("symbol", () => {
|
|
37
|
+
// @ts-expect-error
|
|
38
|
+
attest(() => type({ bad: Symbol() })).throwsAndHasTypeError(
|
|
39
|
+
writeBadDefinitionTypeMessage("symbol")
|
|
40
|
+
)
|
|
41
|
+
})
|
|
42
|
+
it("any", () => {
|
|
43
|
+
// doesn't error, so this test is just to ensure it doesn't infinitely recurse
|
|
44
|
+
const t = type({ bad: {} as any })
|
|
45
|
+
attest<{}>(t.infer)
|
|
46
|
+
})
|
|
47
|
+
it("unknown", () => {
|
|
48
|
+
// @ts-expect-error just results in base completions, so we just check there's an error
|
|
49
|
+
attest(() => type({ bad: {} as unknown })).type.errors("")
|
|
50
|
+
})
|
|
51
|
+
})
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { type } from "arktype"
|
|
3
|
+
import { writeUnsatisfiableExpressionError } from "../parser/semantic/validate.js"
|
|
4
|
+
|
|
5
|
+
describe("basis intersections", () => {
|
|
6
|
+
it("class & literal", () => {
|
|
7
|
+
const a = [0]
|
|
8
|
+
const literal = type("===", a)
|
|
9
|
+
const cls = type("instanceof", Array)
|
|
10
|
+
const lr = literal.and(cls)
|
|
11
|
+
attest<number[]>(lr.infer)
|
|
12
|
+
attest(lr.json).equals(literal.json)
|
|
13
|
+
const rl = cls.and(literal)
|
|
14
|
+
attest<number[]>(rl.infer)
|
|
15
|
+
attest(rl.json).equals(literal.json)
|
|
16
|
+
})
|
|
17
|
+
it("unsatisfiable class & literal", () => {
|
|
18
|
+
const a = [0]
|
|
19
|
+
const literal = type("===", a)
|
|
20
|
+
const cls = type("instanceof", Date)
|
|
21
|
+
attest(() => literal.and(cls)).throws(writeUnsatisfiableExpressionError(""))
|
|
22
|
+
attest(() => cls.and(literal)).throws(writeUnsatisfiableExpressionError(""))
|
|
23
|
+
})
|
|
24
|
+
it("domain & literal", () => {
|
|
25
|
+
const literal = type("'foo'")
|
|
26
|
+
const domain = type("string")
|
|
27
|
+
attest(literal.and(domain).json).equals(literal.json)
|
|
28
|
+
attest(domain.and(literal).json).equals(literal.json)
|
|
29
|
+
})
|
|
30
|
+
it("unsatisfiable domain & literal", () => {
|
|
31
|
+
const literal = type("'foo'")
|
|
32
|
+
const domain = type("number")
|
|
33
|
+
attest(() => literal.and(domain)).throws(
|
|
34
|
+
writeUnsatisfiableExpressionError("")
|
|
35
|
+
)
|
|
36
|
+
attest(() => domain.and(literal)).throws(
|
|
37
|
+
writeUnsatisfiableExpressionError("")
|
|
38
|
+
)
|
|
39
|
+
})
|
|
40
|
+
it("domain & class", () => {
|
|
41
|
+
const domain = type("object")
|
|
42
|
+
const cls = type("instanceof", Date)
|
|
43
|
+
attest(domain.and(cls).json).equals(cls.json)
|
|
44
|
+
attest(cls.and(domain).json).equals(cls.json)
|
|
45
|
+
})
|
|
46
|
+
})
|