arktype 1.1.0-dev-1 → 2.0.0-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/array.test.ts +272 -0
- package/__tests__/astUtils.test.ts +17 -0
- package/__tests__/badDefinitionType.test.ts +51 -0
- package/__tests__/basis.test.ts +46 -0
- package/__tests__/bounds.test.ts +388 -0
- package/__tests__/cast.test.ts +32 -0
- package/__tests__/completions.test.ts +46 -0
- package/__tests__/config.test.ts +168 -0
- package/__tests__/cyclic.test.ts +144 -0
- package/__tests__/dateLiteral.test.ts +32 -0
- package/__tests__/declared.test.ts +123 -0
- package/__tests__/discrimination.test.ts +74 -0
- package/__tests__/divisor.test.ts +90 -0
- package/__tests__/enclosed.test.ts +93 -0
- package/__tests__/examples.test.ts +56 -0
- package/__tests__/expressions.test.ts +138 -0
- package/__tests__/generateBenchData.ts +89 -0
- package/__tests__/generated/cyclic.ts +3058 -0
- package/__tests__/generic.test.ts +294 -0
- package/__tests__/group.test.ts +72 -0
- package/__tests__/hkt.test.ts +22 -0
- package/__tests__/imports.test.ts +113 -0
- package/__tests__/instanceof.test.ts +102 -0
- package/__tests__/intersection.test.ts +104 -0
- package/__tests__/jsdoc.test.ts +64 -0
- package/__tests__/keyTraversal.test.ts +73 -0
- package/__tests__/keyof.test.ts +134 -0
- package/__tests__/keywords.test.ts +188 -0
- package/__tests__/literal.test.ts +51 -0
- package/__tests__/match.test.ts +34 -0
- package/__tests__/methods.bench.ts +22 -0
- package/__tests__/morph.test.ts +409 -0
- package/__tests__/narrow.test.ts +102 -0
- package/__tests__/nodeDefinitions.test.ts +190 -0
- package/__tests__/object.bench.ts +28 -0
- package/__tests__/objectLiteral.test.ts +300 -0
- package/__tests__/operand.bench.ts +45 -0
- package/__tests__/operator.bench.ts +130 -0
- package/__tests__/regex.test.ts +58 -0
- package/__tests__/scope.bench.ts +39 -0
- package/__tests__/scope.test.ts +140 -0
- package/__tests__/string.test.ts +48 -0
- package/__tests__/submodule.test.ts +99 -0
- package/__tests__/this.test.ts +57 -0
- package/__tests__/thunk.test.ts +80 -0
- package/__tests__/traverse.test.ts +119 -0
- package/__tests__/tsGenerics.test.ts +12 -0
- package/__tests__/typeMethods.test.ts +69 -0
- package/__tests__/typeReference.test.ts +39 -0
- package/__tests__/unenclosed.test.ts +120 -0
- package/__tests__/union.test.ts +147 -0
- package/ark.ts +91 -0
- package/main.ts +18 -0
- package/match.ts +116 -0
- 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/out/match.js +18 -0
- package/out/match.js.map +1 -0
- package/out/parser/definition.d.ts +32 -0
- package/out/parser/definition.d.ts.map +1 -0
- package/out/parser/definition.js +32 -0
- 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/out/parser/objectLiteral.d.ts +33 -0
- 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/out/parser/semantic/bounds.js +2 -0
- package/out/parser/semantic/bounds.js.map +1 -0
- package/out/parser/semantic/divisor.d.ts +9 -0
- package/out/parser/semantic/divisor.d.ts.map +1 -0
- package/out/parser/semantic/divisor.js +2 -0
- package/out/parser/semantic/divisor.js.map +1 -0
- package/out/parser/semantic/intersections.d.ts +14 -0
- package/out/parser/semantic/intersections.d.ts.map +1 -0
- package/out/parser/semantic/intersections.js +2 -0
- package/out/parser/semantic/intersections.js.map +1 -0
- package/out/parser/semantic/semantic.d.ts +33 -0
- package/out/parser/semantic/semantic.d.ts.map +1 -0
- package/out/parser/semantic/semantic.js +2 -0
- package/out/parser/semantic/semantic.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +8 -0
- package/out/parser/semantic/utils.d.ts.map +1 -0
- package/out/parser/semantic/utils.js +2 -0
- package/out/parser/semantic/utils.js.map +1 -0
- package/out/parser/semantic/validate.d.ts +24 -0
- package/out/parser/semantic/validate.d.ts.map +1 -0
- package/out/parser/semantic/validate.js +2 -0
- package/out/parser/semantic/validate.js.map +1 -0
- package/out/parser/shared.d.ts +54 -0
- package/out/parser/shared.d.ts.map +1 -0
- package/out/parser/shared.js +47 -0
- package/out/parser/shared.js.map +1 -0
- package/out/parser/string/reduce/dynamic.d.ts +42 -0
- package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
- package/out/parser/string/reduce/dynamic.js +133 -0
- package/out/parser/string/reduce/dynamic.js.map +1 -0
- package/out/parser/string/reduce/shared.d.ts +56 -0
- package/out/parser/string/reduce/shared.d.ts.map +1 -0
- package/out/parser/string/reduce/shared.js +26 -0
- package/out/parser/string/reduce/shared.js.map +1 -0
- package/out/parser/string/reduce/static.d.ts +176 -0
- package/out/parser/string/reduce/static.d.ts.map +1 -0
- package/out/parser/string/reduce/static.js +2 -0
- package/out/parser/string/reduce/static.js.map +1 -0
- package/out/parser/string/shift/operand/date.d.ts +11 -0
- package/out/parser/string/shift/operand/date.d.ts.map +1 -0
- package/out/parser/string/shift/operand/date.js +26 -0
- package/out/parser/string/shift/operand/date.js.map +1 -0
- package/out/parser/string/shift/operand/enclosed.d.ts +49 -0
- package/out/parser/string/shift/operand/enclosed.d.ts.map +1 -0
- package/out/parser/string/shift/operand/enclosed.js +50 -0
- 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/out/parser/string/shift/operand/operand.d.ts +9 -0
- package/out/parser/string/shift/operand/operand.d.ts.map +1 -0
- package/out/parser/string/shift/operand/operand.js +17 -0
- package/out/parser/string/shift/operand/operand.js.map +1 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts +34 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
- package/out/parser/string/shift/operand/unenclosed.js +68 -0
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
- package/out/parser/string/shift/operator/bounds.d.ts +33 -0
- 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/out/parser/string/shift/operator/divisor.d.ts +9 -0
- package/out/parser/string/shift/operator/divisor.d.ts.map +1 -0
- package/out/parser/string/shift/operator/divisor.js +13 -0
- package/out/parser/string/shift/operator/divisor.js.map +1 -0
- package/out/parser/string/shift/operator/operator.d.ts +13 -0
- package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
- package/out/parser/string/shift/operator/operator.js +29 -0
- package/out/parser/string/shift/operator/operator.js.map +1 -0
- package/out/parser/string/shift/scanner.d.ts +71 -0
- package/out/parser/string/shift/scanner.d.ts.map +1 -0
- package/out/parser/string/shift/scanner.js +99 -0
- package/out/parser/string/shift/scanner.js.map +1 -0
- package/out/parser/string/string.d.ts +24 -0
- 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/out/parser/tuple.d.ts +71 -0
- package/out/parser/tuple.d.ts.map +1 -0
- package/out/parser/tuple.js +137 -0
- package/out/parser/tuple.js.map +1 -0
- package/out/scope.d.ts +159 -0
- package/out/scope.d.ts.map +1 -0
- package/out/scope.js +253 -0
- 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/out/type.d.ts +77 -0
- package/out/type.d.ts.map +1 -0
- package/out/type.js +134 -0
- 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 -10
- package/parser/definition.ts +159 -0
- package/parser/generic.ts +68 -0
- package/parser/objectLiteral.ts +197 -0
- package/parser/semantic/bounds.ts +61 -0
- package/parser/semantic/divisor.ts +13 -0
- package/parser/semantic/intersections.ts +46 -0
- package/parser/semantic/semantic.ts +144 -0
- package/parser/semantic/utils.ts +26 -0
- package/parser/semantic/validate.ts +109 -0
- package/parser/shared.ts +152 -0
- package/parser/string/reduce/dynamic.ts +196 -0
- package/parser/string/reduce/shared.ts +101 -0
- package/parser/string/reduce/static.ts +298 -0
- package/parser/string/shift/operand/date.ts +54 -0
- package/parser/string/shift/operand/enclosed.ts +118 -0
- package/parser/string/shift/operand/genericArgs.ts +127 -0
- package/parser/string/shift/operand/operand.ts +48 -0
- package/parser/string/shift/operand/unenclosed.ts +268 -0
- package/parser/string/shift/operator/bounds.ts +264 -0
- package/parser/string/shift/operator/divisor.ts +36 -0
- package/parser/string/shift/operator/operator.ts +82 -0
- package/parser/string/shift/scanner.ts +209 -0
- package/parser/string/string.ts +90 -0
- package/parser/tuple.ts +474 -0
- package/scope.ts +574 -0
- package/type.ts +347 -0
- package/util.ts +36 -0
- package/dist/main.d.ts +0 -1018
- package/dist/main.js +0 -1450
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { scope } from "arktype"
|
|
3
|
+
|
|
4
|
+
const getCyclicScope = () =>
|
|
5
|
+
scope({
|
|
6
|
+
package: {
|
|
7
|
+
name: "string",
|
|
8
|
+
"dependencies?": "package[]",
|
|
9
|
+
"contributors?": "contributor[]"
|
|
10
|
+
},
|
|
11
|
+
contributor: {
|
|
12
|
+
email: "email",
|
|
13
|
+
"packages?": "package[]"
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
type Package = ReturnType<typeof getCyclicScope>["infer"]["package"]
|
|
18
|
+
|
|
19
|
+
const getCyclicData = () => {
|
|
20
|
+
const packageData = {
|
|
21
|
+
name: "arktype",
|
|
22
|
+
dependencies: [{ name: "typescript" }],
|
|
23
|
+
contributors: [{ email: "david@arktype.io" }]
|
|
24
|
+
} satisfies Package
|
|
25
|
+
packageData.dependencies.push(packageData)
|
|
26
|
+
return packageData
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("cyclic", () => {
|
|
30
|
+
it("cyclic union", () => {
|
|
31
|
+
const $ = scope({
|
|
32
|
+
a: { b: "b|false" },
|
|
33
|
+
b: { a: "a|true" }
|
|
34
|
+
})
|
|
35
|
+
attest($.infer).type.toString.snap(
|
|
36
|
+
"{ a: { b: false | { a: true | any; }; }; b: { a: true | { b: false | any; }; }; }"
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
it("cyclic intersection", () => {
|
|
40
|
+
const $ = scope({
|
|
41
|
+
a: { b: "b&a" },
|
|
42
|
+
b: { a: "a&b" }
|
|
43
|
+
})
|
|
44
|
+
attest($.infer).type.toString.snap(
|
|
45
|
+
"{ a: { b: { a: { b: any; a: any; }; b: any; }; }; b: { a: { b: { a: any; b: any; }; a: any; }; }; }"
|
|
46
|
+
)
|
|
47
|
+
})
|
|
48
|
+
// TODO: reenable
|
|
49
|
+
it("cyclic", () => {
|
|
50
|
+
const types = scope({ a: { b: "b" }, b: { a: "a" } }).export()
|
|
51
|
+
// attest(types.a.node).snap({
|
|
52
|
+
// object: { props: { b: "b" } }
|
|
53
|
+
// })
|
|
54
|
+
// Type hint displays as "..." on hitting cycle (or any if "noErrorTruncation" is true)
|
|
55
|
+
attest<{
|
|
56
|
+
b: {
|
|
57
|
+
a: {
|
|
58
|
+
b: {
|
|
59
|
+
a: any
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}>(types.a.infer)
|
|
64
|
+
attest<{
|
|
65
|
+
b: {
|
|
66
|
+
a: any
|
|
67
|
+
}
|
|
68
|
+
}>(types.b.infer.a.b.a.b.a.b.a)
|
|
69
|
+
|
|
70
|
+
// @ts-expect-error
|
|
71
|
+
attest(types.a.infer.b.a.b.c).type.errors.snap(
|
|
72
|
+
`Property 'c' does not exist on type '{ a: { b: ...; }; }'.`
|
|
73
|
+
)
|
|
74
|
+
})
|
|
75
|
+
it("allows valid", () => {
|
|
76
|
+
const types = getCyclicScope().export()
|
|
77
|
+
const data = getCyclicData()
|
|
78
|
+
attest(types.package(data).out).snap({
|
|
79
|
+
name: "arktype",
|
|
80
|
+
dependencies: [{ name: "typescript" }, "(cycle)" as any as Package],
|
|
81
|
+
contributors: [{ email: "david@arktype.io" }]
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
it("adds errors on invalid", () => {
|
|
85
|
+
const types = getCyclicScope().export()
|
|
86
|
+
const data = getCyclicData()
|
|
87
|
+
data.contributors[0].email = "ssalbdivad"
|
|
88
|
+
attest(types.package(data).errors?.summary).snap(
|
|
89
|
+
"dependencies/1/contributors/0/email must be a valid email (was 'ssalbdivad')\ncontributors/0/email must be a valid email (was 'ssalbdivad')"
|
|
90
|
+
)
|
|
91
|
+
})
|
|
92
|
+
it("can include cyclic data in message", () => {
|
|
93
|
+
const data = getCyclicData()
|
|
94
|
+
const nonSelfDependent = getCyclicScope().type([
|
|
95
|
+
"package",
|
|
96
|
+
":",
|
|
97
|
+
(p) => !p.dependencies?.some((d) => d.name === p.name)
|
|
98
|
+
])
|
|
99
|
+
attest(nonSelfDependent(data).errors?.summary).snap(
|
|
100
|
+
'Must be valid (was {"name":"arktype","dependencies":[{"name":"typescript"},"(cycle)"],"contributors":[{"email":"david@arktype.io"}]})'
|
|
101
|
+
)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it("equivalent cycles reduce", () => {
|
|
105
|
+
// // TODO: reduce this case or create an issue
|
|
106
|
+
// const $ = scope({
|
|
107
|
+
// user: {
|
|
108
|
+
// friends: "user[]",
|
|
109
|
+
// name: "string"
|
|
110
|
+
// },
|
|
111
|
+
// admin: {
|
|
112
|
+
// friends: "user[]",
|
|
113
|
+
// name: "string"
|
|
114
|
+
// }
|
|
115
|
+
// })
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it("union cyclic reference", () => {
|
|
119
|
+
const types = scope({
|
|
120
|
+
a: {
|
|
121
|
+
b: "b"
|
|
122
|
+
},
|
|
123
|
+
b: {
|
|
124
|
+
a: "a|3"
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
attest(types.infer).type.toString.snap(
|
|
128
|
+
"{ a: { b: { a: 3 | any; }; }; b: { a: 3 | { b: any; }; }; }"
|
|
129
|
+
)
|
|
130
|
+
})
|
|
131
|
+
it("intersect cyclic reference", () => {
|
|
132
|
+
const types = scope({
|
|
133
|
+
a: {
|
|
134
|
+
b: "b"
|
|
135
|
+
},
|
|
136
|
+
b: {
|
|
137
|
+
c: "a&b"
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
attest(types.infer).type.toString.snap(
|
|
141
|
+
"{ a: { b: { c: { b: any; c: any; }; }; }; b: { c: { b: any; c: any; }; }; }"
|
|
142
|
+
)
|
|
143
|
+
})
|
|
144
|
+
})
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { type } from "arktype"
|
|
3
|
+
import { writeInvalidDateMessage } from "../parser/string/shift/operand/date.js"
|
|
4
|
+
|
|
5
|
+
describe("date literal", () => {
|
|
6
|
+
it("base", () => {
|
|
7
|
+
const t = type("d'2000/05/05'")
|
|
8
|
+
attest<Date>(t.infer)
|
|
9
|
+
attest(t.allows(new Date("2000/05/05"))).equals(true)
|
|
10
|
+
attest(t.allows(new Date("2000/06/05"))).equals(false)
|
|
11
|
+
attest(t.allows(new Date("2000/05/05T09:00:00.00Z"))).equals(false)
|
|
12
|
+
})
|
|
13
|
+
it("with punctuation", () => {
|
|
14
|
+
const ISO = type("d'2000-05-05T04:00:00.000Z'")
|
|
15
|
+
attest<Date>(ISO.infer)
|
|
16
|
+
attest(ISO.allows(new Date("2000/05/05"))).equals(true)
|
|
17
|
+
attest(ISO.allows(new Date("2000/07/05"))).equals(false)
|
|
18
|
+
})
|
|
19
|
+
it("allows spaces", () => {
|
|
20
|
+
const t = type("d' 2021 / 05 / 01 '")
|
|
21
|
+
attest(t.allows(new Date("2021/05/01"))).equals(true)
|
|
22
|
+
})
|
|
23
|
+
it("epoch", () => {
|
|
24
|
+
const now = new Date()
|
|
25
|
+
const t = type(`d'${now.valueOf()}'`)
|
|
26
|
+
attest(t.allows(now)).equals(true)
|
|
27
|
+
attest(t.allows(new Date(now.valueOf() + 1)))
|
|
28
|
+
})
|
|
29
|
+
it("invalid date", () => {
|
|
30
|
+
attest(() => type("d'tuesday'")).throws(writeInvalidDateMessage("tuesday"))
|
|
31
|
+
})
|
|
32
|
+
})
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { declare, type } from "arktype"
|
|
3
|
+
|
|
4
|
+
describe("declared", () => {
|
|
5
|
+
it("shallow", () => {
|
|
6
|
+
const shallow = declare<number>().type("number")
|
|
7
|
+
attest<number>(shallow.infer)
|
|
8
|
+
attest(shallow.json).equals(type("number").json)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it("obj", () => {
|
|
12
|
+
type Expected = { a: string; b?: number }
|
|
13
|
+
const t = declare<Expected>().type({
|
|
14
|
+
a: "string",
|
|
15
|
+
"b?": "number"
|
|
16
|
+
})
|
|
17
|
+
attest<Expected>(t.infer)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it("tuple", () => {
|
|
21
|
+
type Expected = [string, number]
|
|
22
|
+
const t = declare<Expected>().type(["string", "number"])
|
|
23
|
+
attest<Expected>(t.infer)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it("bad element", () => {
|
|
27
|
+
attest(
|
|
28
|
+
// @ts-expect-error
|
|
29
|
+
declare<[string, number]>().type(["string", "boolean"])
|
|
30
|
+
).type.errors(
|
|
31
|
+
`Type 'string' is not assignable to type '{ declared: number; inferred: boolean; }'`
|
|
32
|
+
)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it("too short", () => {
|
|
36
|
+
// @ts-expect-error
|
|
37
|
+
attest(declare<[string, number]>().type(["string"])).type.errors(
|
|
38
|
+
`Source has 1 element(s) but target requires 2`
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it("too long", () => {
|
|
43
|
+
attest(
|
|
44
|
+
// @ts-expect-error
|
|
45
|
+
declare<[string, number]>().type(["string", "number", "number"])
|
|
46
|
+
).type.errors(`Source has 3 element(s) but target requires 2`)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it("tuple expression", () => {
|
|
50
|
+
const t = declare<0 | 1>().type(["0", "|", "1"])
|
|
51
|
+
attest<0 | 1>(t.infer)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it("regexp", () => {
|
|
55
|
+
const t = declare<string>().type(/.*/)
|
|
56
|
+
attest<string>(t.infer)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it("Inferred<t>", () => {
|
|
60
|
+
const foo = type("'foo'")
|
|
61
|
+
const t = declare<"foo">().type(foo)
|
|
62
|
+
attest<"foo">(t.infer)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it("bad tuple expression", () => {
|
|
66
|
+
attest(
|
|
67
|
+
// @ts-expect-error
|
|
68
|
+
declare<"foo" | "bar">().type(["'foo'", "|", "'baz'"])
|
|
69
|
+
).type.errors(`{ declared: "foo" | "bar"; inferred: "foo" | "baz"; }`)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it("narrower", () => {
|
|
73
|
+
// @ts-expect-error
|
|
74
|
+
attest(() => declare<string>().type("'foo'")).type.errors(
|
|
75
|
+
`Argument of type 'string' is not assignable to parameter of type '{ declared: string; inferred: "foo"; }'`
|
|
76
|
+
)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it("wider", () => {
|
|
80
|
+
attest(() =>
|
|
81
|
+
declare<{ a: string }>().type({
|
|
82
|
+
// @ts-expect-error
|
|
83
|
+
a: "unknown"
|
|
84
|
+
})
|
|
85
|
+
).type.errors(
|
|
86
|
+
`Type 'string' is not assignable to type '{ declared: string; inferred: unknown; }'`
|
|
87
|
+
)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it("missing key", () => {
|
|
91
|
+
attest(() =>
|
|
92
|
+
// @ts-expect-error
|
|
93
|
+
declare<{ a: string; b: number }>().type({
|
|
94
|
+
a: "string"
|
|
95
|
+
})
|
|
96
|
+
).type.errors(
|
|
97
|
+
`Property 'b' is missing in type '{ a: "string"; }' but required in type '{ a: "string"; "b?": unknown; }'.`
|
|
98
|
+
)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it("missing optional key", () => {
|
|
102
|
+
attest(() =>
|
|
103
|
+
// @ts-expect-error
|
|
104
|
+
declare<{ a: string; b?: number }>().type({
|
|
105
|
+
a: "string"
|
|
106
|
+
})
|
|
107
|
+
).type.errors(
|
|
108
|
+
`Property '"b?"' is missing in type '{ a: "string"; }' but required in type '{ a: "string"; "b?": unknown; }'.`
|
|
109
|
+
)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it("extraneous key", () => {
|
|
113
|
+
attest(() =>
|
|
114
|
+
declare<{ a: string }>().type({
|
|
115
|
+
a: "string",
|
|
116
|
+
// @ts-expect-error
|
|
117
|
+
b: "boolean"
|
|
118
|
+
})
|
|
119
|
+
).type.errors(
|
|
120
|
+
`Object literal may only specify known properties, and 'b' does not exist in type '{ a: "string"; }'.`
|
|
121
|
+
)
|
|
122
|
+
})
|
|
123
|
+
})
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// import { attest } from "@arktype/attest"
|
|
2
|
+
// import { scope, type } from "arktype"
|
|
3
|
+
|
|
4
|
+
describe("discrimination", () => {
|
|
5
|
+
// it("2 literal branches", () => {
|
|
6
|
+
// // should not use a switch with <=2 branches to avoid visual clutter
|
|
7
|
+
// const t = type("'a'|'b'")
|
|
8
|
+
// attest(t.json).snap({ unit: "a" })
|
|
9
|
+
// attest(t.allows("a")).equals(true)
|
|
10
|
+
// attest(t.allows("b")).equals(true)
|
|
11
|
+
// attest(t.allows("c")).equals(false)
|
|
12
|
+
// })
|
|
13
|
+
// it(">2 literal branches", () => {
|
|
14
|
+
// const t = type("'a'|'b'|'c'")
|
|
15
|
+
// attest(t.json).snap({ unit: "a" })
|
|
16
|
+
// attest(t.allows("a")).equals(true)
|
|
17
|
+
// attest(t.allows("b")).equals(true)
|
|
18
|
+
// attest(t.allows("c")).equals(true)
|
|
19
|
+
// attest(t.allows("d")).equals(false)
|
|
20
|
+
// })
|
|
21
|
+
// const getPlaces = () =>
|
|
22
|
+
// scope({
|
|
23
|
+
// rainForest: {
|
|
24
|
+
// climate: "'wet'",
|
|
25
|
+
// color: "'green'",
|
|
26
|
+
// isRainForest: "true"
|
|
27
|
+
// },
|
|
28
|
+
// desert: { climate: "'dry'", color: "'brown'", isDesert: "true" },
|
|
29
|
+
// sky: { climate: "'dry'", color: "'blue'", isSky: "true" },
|
|
30
|
+
// ocean: { climate: "'wet'", color: "'blue'", isOcean: "true" }
|
|
31
|
+
// })
|
|
32
|
+
// it("nested", () => {
|
|
33
|
+
// const t = getPlaces().type("ocean|sky|rainForest|desert")
|
|
34
|
+
// attest(t.json).snap()
|
|
35
|
+
// })
|
|
36
|
+
// it("undiscriminable", () => {
|
|
37
|
+
// const t = getPlaces().type([
|
|
38
|
+
// "ocean",
|
|
39
|
+
// "|",
|
|
40
|
+
// {
|
|
41
|
+
// climate: "'wet'",
|
|
42
|
+
// color: "'blue'",
|
|
43
|
+
// indistinguishableFrom: "ocean"
|
|
44
|
+
// }
|
|
45
|
+
// ])
|
|
46
|
+
// })
|
|
47
|
+
// it("doesn't discriminate optional key", () => {
|
|
48
|
+
// const t = type({
|
|
49
|
+
// direction: "'forward' | 'backward'",
|
|
50
|
+
// "operator?": "'by'"
|
|
51
|
+
// }).or({
|
|
52
|
+
// duration: "'s' | 'min' | 'h'",
|
|
53
|
+
// operator: "'to'"
|
|
54
|
+
// })
|
|
55
|
+
// attest(t.root.hasKind("union") && t.root.discriminant).equals(null)
|
|
56
|
+
// })
|
|
57
|
+
// it("default case", () => {
|
|
58
|
+
// const t = getPlaces().type([
|
|
59
|
+
// "ocean|rainForest",
|
|
60
|
+
// "|",
|
|
61
|
+
// { temperature: "'hot'" }
|
|
62
|
+
// ])
|
|
63
|
+
// })
|
|
64
|
+
// it("discriminable default", () => {
|
|
65
|
+
// const t = getPlaces().type([
|
|
66
|
+
// { temperature: "'cold'" },
|
|
67
|
+
// "|",
|
|
68
|
+
// ["ocean|rainForest", "|", { temperature: "'hot'" }]
|
|
69
|
+
// ])
|
|
70
|
+
// })
|
|
71
|
+
// it("won't discriminate between possibly empty arrays", () => {
|
|
72
|
+
// const t = type("string[]|boolean[]")
|
|
73
|
+
// })
|
|
74
|
+
})
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { writeIndivisibleMessage } from "@arktype/schema"
|
|
3
|
+
import { type } from "arktype"
|
|
4
|
+
import { writeInvalidDivisorMessage } from "../parser/string/shift/operator/divisor.js"
|
|
5
|
+
|
|
6
|
+
describe("divisibility", () => {
|
|
7
|
+
describe("parse", () => {
|
|
8
|
+
it("integer literal", () => {
|
|
9
|
+
const divisibleByTwo = type("number%2")
|
|
10
|
+
attest<number>(divisibleByTwo.infer)
|
|
11
|
+
attest(divisibleByTwo.json).snap({ basis: "number", divisor: 2 })
|
|
12
|
+
})
|
|
13
|
+
it("whitespace after %", () => {
|
|
14
|
+
const t = type("number % 5")
|
|
15
|
+
attest<number>(t.infer)
|
|
16
|
+
attest(t.json).snap({ basis: "number", divisor: 5 })
|
|
17
|
+
})
|
|
18
|
+
it("with bound", () => {
|
|
19
|
+
const t = type("number%8<3")
|
|
20
|
+
attest(t.json).equals(type("number%8").and("number<3").json)
|
|
21
|
+
attest(t.root.description).snap(
|
|
22
|
+
"a number and a multiple of 8 and less than 3"
|
|
23
|
+
)
|
|
24
|
+
})
|
|
25
|
+
it("allows non-narrowed divisor", () => {
|
|
26
|
+
const d = 5 as number
|
|
27
|
+
attest<number>(type(`number%${d}`).infer)
|
|
28
|
+
})
|
|
29
|
+
it("fails at runtime on non-integer divisor", () => {
|
|
30
|
+
attest(() => type("number%2.3")).throws(writeInvalidDivisorMessage("2.3"))
|
|
31
|
+
})
|
|
32
|
+
it("non-numeric divisor", () => {
|
|
33
|
+
// @ts-expect-error
|
|
34
|
+
attest(() => type("number%foobar")).throwsAndHasTypeError(
|
|
35
|
+
writeInvalidDivisorMessage("foobar")
|
|
36
|
+
)
|
|
37
|
+
})
|
|
38
|
+
it("zero divisor", () => {
|
|
39
|
+
// @ts-expect-error
|
|
40
|
+
attest(() => type("number%0")).throwsAndHasTypeError(
|
|
41
|
+
writeInvalidDivisorMessage(0)
|
|
42
|
+
)
|
|
43
|
+
})
|
|
44
|
+
it("unknown", () => {
|
|
45
|
+
// @ts-expect-error
|
|
46
|
+
attest(() => type("unknown%2")).throwsAndHasTypeError(
|
|
47
|
+
writeIndivisibleMessage("unknown")
|
|
48
|
+
)
|
|
49
|
+
})
|
|
50
|
+
it("indivisible", () => {
|
|
51
|
+
// @ts-expect-error
|
|
52
|
+
attest(() => type("string%1")).throwsAndHasTypeError(
|
|
53
|
+
writeIndivisibleMessage("string")
|
|
54
|
+
)
|
|
55
|
+
})
|
|
56
|
+
it("overlapping", () => {
|
|
57
|
+
// @ts-expect-error
|
|
58
|
+
attest(() => type("(number|string)%10"))
|
|
59
|
+
.throws("Divisibility operand string must be a number")
|
|
60
|
+
.type.errors("Divisibility operand number | string must be a number")
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
describe("intersection", () => {
|
|
64
|
+
it("identical", () => {
|
|
65
|
+
const t = type("number%2&number%2")
|
|
66
|
+
attest(t.json).equals(type("number%2").json)
|
|
67
|
+
})
|
|
68
|
+
it("purely divisible", () => {
|
|
69
|
+
const t = type("number%4&number%2")
|
|
70
|
+
attest(t.json).equals(type("number%4").json)
|
|
71
|
+
})
|
|
72
|
+
it("common divisor", () => {
|
|
73
|
+
const t = type("number%6&number%4")
|
|
74
|
+
attest(t.json).equals(type("number%12").json)
|
|
75
|
+
})
|
|
76
|
+
it("relatively prime", () => {
|
|
77
|
+
const t = type("number%2&number%3")
|
|
78
|
+
attest(t.json).equals(type("number%6").json)
|
|
79
|
+
})
|
|
80
|
+
it("valid literal", () => {
|
|
81
|
+
const t = type("number%5&0")
|
|
82
|
+
attest(t.json).equals(type("0").json)
|
|
83
|
+
})
|
|
84
|
+
it("invalid literal", () => {
|
|
85
|
+
attest(() => type("number%3&8")).throws(
|
|
86
|
+
"Intersection of (a multiple of 3) and 8 results in an unsatisfiable type"
|
|
87
|
+
)
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
})
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { type } from "arktype"
|
|
3
|
+
import { writeUnterminatedEnclosedMessage } from "../parser/string/shift/operand/enclosed.js"
|
|
4
|
+
|
|
5
|
+
describe("parse enclosed", () => {
|
|
6
|
+
it("with spaces", () => {
|
|
7
|
+
const t = type("'this has spaces'")
|
|
8
|
+
attest<"this has spaces">(t.infer)
|
|
9
|
+
attest(t.json).snap({ unit: "this has spaces" })
|
|
10
|
+
})
|
|
11
|
+
it("with neighbors", () => {
|
|
12
|
+
const t = type("'foo'|/.*/[]")
|
|
13
|
+
attest<"foo" | string[]>(t.infer)
|
|
14
|
+
attest(t.json).snap([{ unit: "foo" }, { basis: "Array" }])
|
|
15
|
+
})
|
|
16
|
+
describe("errors", () => {
|
|
17
|
+
describe("unterminated", () => {
|
|
18
|
+
it("regex", () => {
|
|
19
|
+
// @ts-expect-error
|
|
20
|
+
attest(() => type("/.*")).throwsAndHasTypeError(
|
|
21
|
+
writeUnterminatedEnclosedMessage(".*", "/")
|
|
22
|
+
)
|
|
23
|
+
})
|
|
24
|
+
it("single-quote", () => {
|
|
25
|
+
// @ts-expect-error
|
|
26
|
+
attest(() => type("'.*")).throwsAndHasTypeError(
|
|
27
|
+
writeUnterminatedEnclosedMessage(".*", "'")
|
|
28
|
+
)
|
|
29
|
+
})
|
|
30
|
+
it("double-quote", () => {
|
|
31
|
+
// @ts-expect-error
|
|
32
|
+
attest(() => type('".*')).throwsAndHasTypeError(
|
|
33
|
+
writeUnterminatedEnclosedMessage(".*", '"')
|
|
34
|
+
)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
it("single-quoted", () => {
|
|
39
|
+
attest<"hello">(type("'hello'").infer)
|
|
40
|
+
})
|
|
41
|
+
it("double-quoted", () => {
|
|
42
|
+
attest<"goodbye">(type('"goodbye"').infer)
|
|
43
|
+
})
|
|
44
|
+
it("regex literal", () => {
|
|
45
|
+
attest<string>(type("/.*/").infer)
|
|
46
|
+
})
|
|
47
|
+
it("invalid regex", () => {
|
|
48
|
+
attest(() => type("/[/")).throws(
|
|
49
|
+
"Invalid regular expression: /[/: Unterminated character class"
|
|
50
|
+
)
|
|
51
|
+
})
|
|
52
|
+
it("mixed quote types", () => {
|
|
53
|
+
attest<"'single-quoted'">(type(`"'single-quoted'"`).infer)
|
|
54
|
+
attest<'"double-quoted"'>(type(`'"double-quoted"'`).infer)
|
|
55
|
+
})
|
|
56
|
+
it("ignores enclosed operators", () => {
|
|
57
|
+
attest<"yes|no|maybe">(type("'yes|no|maybe'").infer)
|
|
58
|
+
})
|
|
59
|
+
it("mix of enclosed and unenclosed operators", () => {
|
|
60
|
+
attest<"yes|no" | "true|false">(type("'yes|no'|'true|false'").infer)
|
|
61
|
+
})
|
|
62
|
+
it("escaped enclosing", () => {
|
|
63
|
+
const t = type("'don\\'t'")
|
|
64
|
+
attest<"don't">(t.infer)
|
|
65
|
+
})
|
|
66
|
+
it("string literal stress", () => {
|
|
67
|
+
const s = `"3.
|
|
68
|
+
14159265358979323846264338327950288419716939937510
|
|
69
|
+
58209749445923078164062862089986280348253421170679
|
|
70
|
+
82148086513282306647093844609550582231725359408128
|
|
71
|
+
48111745028410270193852110555964462294895493038196
|
|
72
|
+
44288109756659334461284756482337867831652712019091
|
|
73
|
+
45648566923460348610454326648213393607260249141273
|
|
74
|
+
72458700660631558817488152092096282925409171536436
|
|
75
|
+
78925903600113305305488204665213841469519415116094
|
|
76
|
+
33057270365759591953092186117381932611793105118548
|
|
77
|
+
07446237996274956735188575272489122793818301194912
|
|
78
|
+
98336733624406566430860213949463952247371907021798
|
|
79
|
+
60943702770539217176293176752384674818467669405132
|
|
80
|
+
00056812714526356082778577134275778960917363717872
|
|
81
|
+
14684409012249534301465495853710507922796892589235
|
|
82
|
+
42019956112129021960864034418159813629774771309960
|
|
83
|
+
51870721134999999837297804995105973173281609631859
|
|
84
|
+
50244594553469083026425223082533446850352619311881
|
|
85
|
+
71010003137838752886587533208381420617177669147303
|
|
86
|
+
59825349042875546873115956286388235378759375195778
|
|
87
|
+
185778053217122680661300192"`
|
|
88
|
+
// parses exactly 1001 characters before hitting a recursion limit
|
|
89
|
+
const t = type(s)
|
|
90
|
+
type Expected = typeof s extends `"${infer enclosed}"` ? enclosed : never
|
|
91
|
+
attest<Expected>(t.infer)
|
|
92
|
+
})
|
|
93
|
+
})
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
describe("snippets", () => {
|
|
2
|
+
// it("demo", async () => {
|
|
3
|
+
// const typeSnippet = await import("../../docs/examples/demo.js")
|
|
4
|
+
// attest<{>(typeSnippet.pkg.infer)
|
|
5
|
+
// name: string
|
|
6
|
+
// version: string
|
|
7
|
+
// contributors?: string[]
|
|
8
|
+
// }
|
|
9
|
+
// attest(typeSnippet.errors?.summary).snap(
|
|
10
|
+
// "contributors must be more than 1 items long (was 1)"
|
|
11
|
+
// )
|
|
12
|
+
// })
|
|
13
|
+
// it("type", async () => {
|
|
14
|
+
// const typeSnippet = await import("./benches/type.js")
|
|
15
|
+
// attest<{>(typeSnippet.user.infer)
|
|
16
|
+
// name: string
|
|
17
|
+
// device: {
|
|
18
|
+
// platform: "android" | "ios"
|
|
19
|
+
// version?: number
|
|
20
|
+
// }
|
|
21
|
+
// }
|
|
22
|
+
// attest(typeSnippet.errors?.summary).snap(
|
|
23
|
+
// "device/platform must be 'android' or 'ios' (was 'enigma')"
|
|
24
|
+
// )
|
|
25
|
+
// })
|
|
26
|
+
// // it("scope", async () => {
|
|
27
|
+
// // const scopeSnippet = await import("../examples/scope.js")
|
|
28
|
+
// // attest<{>(scopeSnippet.types.package.infer)
|
|
29
|
+
// // name: string
|
|
30
|
+
// // dependencies?: any[]
|
|
31
|
+
// // devDependencies?: any[]
|
|
32
|
+
// // contributors?: {
|
|
33
|
+
// // email: string
|
|
34
|
+
// // packages?: any[]
|
|
35
|
+
// // }[]
|
|
36
|
+
// // }
|
|
37
|
+
// // attest(scopeSnippet.errors?.summary).snap(
|
|
38
|
+
// // "dependencies/0/dependencies/0/contributors/0/email must be a valid email (was 'david@sharktypeio')\ncontributors/0/email must be a valid email (was 'david@sharktypeio')"
|
|
39
|
+
// // )
|
|
40
|
+
// // })
|
|
41
|
+
// it("optimized", async () => {
|
|
42
|
+
// const example = await import("../../docs/examples/optimized.js")
|
|
43
|
+
// attest(example.deepLeftOrRight.infer).typed as
|
|
44
|
+
// | {
|
|
45
|
+
// auto: {
|
|
46
|
+
// discriminated: "left"
|
|
47
|
+
// }
|
|
48
|
+
// }
|
|
49
|
+
// | {
|
|
50
|
+
// auto: {
|
|
51
|
+
// discriminated: "right"
|
|
52
|
+
// }
|
|
53
|
+
// }
|
|
54
|
+
// attest<number>(example.numericIntersection.infer)
|
|
55
|
+
// })
|
|
56
|
+
})
|