arktype 2.0.0-dev.2 → 2.0.0-dev.20
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/LICENSE +7 -0
- package/out/api.d.ts +6 -0
- package/out/api.js +5 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/match.d.ts +18 -24
- package/out/match.js +50 -60
- package/out/module.d.ts +11 -0
- package/out/module.js +2 -0
- package/out/parser/definition.d.ts +13 -15
- package/out/parser/definition.js +10 -13
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/objectLiteral.d.ts +57 -18
- package/out/parser/objectLiteral.js +93 -52
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -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/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +16 -13
- package/out/parser/semantic/validate.js +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/shared.d.ts +14 -28
- package/out/parser/string/reduce/shared.js +4 -3
- package/out/parser/string/reduce/static.d.ts +26 -23
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +2 -2
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
- package/out/parser/string/shift/operand/enclosed.js +14 -14
- 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/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +9 -13
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -20
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operator/bounds.d.ts +8 -12
- package/out/parser/string/shift/operator/bounds.js +45 -56
- package/out/parser/string/shift/operator/divisor.d.ts +1 -3
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/operator.d.ts +1 -2
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/scanner.d.ts +8 -7
- package/out/parser/string/shift/scanner.js +17 -16
- package/out/parser/string/string.d.ts +9 -7
- package/out/parser/string/string.js +1 -3
- package/out/parser/tuple.d.ts +101 -42
- package/out/parser/tuple.js +107 -86
- package/out/scope.d.ts +44 -115
- package/out/scope.js +39 -188
- package/out/type.d.ts +63 -61
- package/out/type.js +30 -135
- package/package.json +33 -39
- package/__tests__/array.test.ts +0 -310
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/badDefinitionType.test.ts +0 -51
- package/__tests__/basis.test.ts +0 -46
- package/__tests__/bounds.test.ts +0 -388
- package/__tests__/cast.test.ts +0 -32
- package/__tests__/completions.test.ts +0 -46
- package/__tests__/config.test.ts +0 -77
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/dateLiteral.test.ts +0 -32
- package/__tests__/declared.test.ts +0 -124
- package/__tests__/discrimination.test.ts +0 -74
- package/__tests__/divisor.test.ts +0 -90
- package/__tests__/enclosed.test.ts +0 -93
- package/__tests__/examples.test.ts +0 -56
- package/__tests__/expressions.test.ts +0 -138
- package/__tests__/generateBenchData.ts +0 -89
- package/__tests__/generated/cyclic.ts +0 -3058
- package/__tests__/generic.test.ts +0 -294
- package/__tests__/group.test.ts +0 -72
- package/__tests__/hkt.test.ts +0 -22
- package/__tests__/imports.test.ts +0 -113
- package/__tests__/instanceof.test.ts +0 -102
- package/__tests__/intersection.test.ts +0 -104
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/keyTraversal.test.ts +0 -74
- package/__tests__/keyof.test.ts +0 -134
- package/__tests__/keywords.test.ts +0 -188
- package/__tests__/literal.test.ts +0 -51
- package/__tests__/match.bench.ts +0 -31
- package/__tests__/match.test.ts +0 -224
- package/__tests__/methods.bench.ts +0 -22
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/narrow.test.ts +0 -102
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/object.bench.ts +0 -28
- package/__tests__/objectLiteral.test.ts +0 -301
- package/__tests__/operand.bench.ts +0 -45
- package/__tests__/operator.bench.ts +0 -130
- package/__tests__/regex.test.ts +0 -58
- package/__tests__/scope.bench.ts +0 -39
- package/__tests__/scope.test.ts +0 -140
- package/__tests__/string.test.ts +0 -48
- package/__tests__/submodule.test.ts +0 -99
- package/__tests__/this.test.ts +0 -57
- package/__tests__/thunk.test.ts +0 -80
- package/__tests__/traverse.test.ts +0 -119
- package/__tests__/tsGenerics.test.ts +0 -12
- package/__tests__/typeMethods.test.ts +0 -69
- package/__tests__/typeReference.test.ts +0 -39
- package/__tests__/unenclosed.test.ts +0 -120
- package/__tests__/union.test.ts +0 -147
- package/ark.ts +0 -91
- package/main.ts +0 -18
- package/match.ts +0 -243
- package/out/ark.d.ts.map +0 -1
- package/out/ark.js.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 +0 -7
- package/out/main.d.ts.map +0 -1
- package/out/main.js +0 -4
- package/out/main.js.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/match.js.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/definition.js.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/generic.js.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/objectLiteral.js.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/bounds.js.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/divisor.js.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/utils.js.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/semantic/validate.js.map +0 -1
- package/out/parser/shared.d.ts +0 -56
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -55
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/dynamic.js.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.js.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/reduce/static.js.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.js.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.js.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.js.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.js.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.js.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.js.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.js.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/string/string.js.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/parser/tuple.js.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/scope.js.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/type.js.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/definition.ts +0 -159
- package/parser/generic.ts +0 -68
- package/parser/objectLiteral.ts +0 -197
- package/parser/semantic/bounds.ts +0 -61
- package/parser/semantic/divisor.ts +0 -13
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -143
- package/parser/semantic/utils.ts +0 -26
- package/parser/semantic/validate.ts +0 -109
- package/parser/shared.ts +0 -167
- package/parser/string/reduce/dynamic.ts +0 -196
- package/parser/string/reduce/shared.ts +0 -101
- package/parser/string/reduce/static.ts +0 -298
- package/parser/string/shift/operand/date.ts +0 -54
- package/parser/string/shift/operand/enclosed.ts +0 -118
- package/parser/string/shift/operand/genericArgs.ts +0 -127
- package/parser/string/shift/operand/operand.ts +0 -48
- package/parser/string/shift/operand/unenclosed.ts +0 -268
- package/parser/string/shift/operator/bounds.ts +0 -264
- package/parser/string/shift/operator/divisor.ts +0 -36
- package/parser/string/shift/operator/operator.ts +0 -82
- package/parser/string/shift/scanner.ts +0 -209
- package/parser/string/string.ts +0 -90
- package/parser/tuple.ts +0 -475
- package/scope.ts +0 -548
- package/type.ts +0 -349
- package/util.ts +0 -36
|
@@ -1,32 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,124 +0,0 @@
|
|
|
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
|
-
// TODO: figure out narrowing
|
|
55
|
-
// it("regexp", () => {
|
|
56
|
-
// const t = declare<string>().type(/.*/)
|
|
57
|
-
// attest<string>(t.infer)
|
|
58
|
-
// })
|
|
59
|
-
|
|
60
|
-
it("Inferred<t>", () => {
|
|
61
|
-
const foo = type("'foo'")
|
|
62
|
-
const t = declare<"foo">().type(foo)
|
|
63
|
-
attest<"foo">(t.infer)
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
it("bad tuple expression", () => {
|
|
67
|
-
attest(
|
|
68
|
-
// @ts-expect-error
|
|
69
|
-
declare<"foo" | "bar">().type(["'foo'", "|", "'baz'"])
|
|
70
|
-
).type.errors(`{ declared: "foo" | "bar"; inferred: "foo" | "baz"; }`)
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
it("narrower", () => {
|
|
74
|
-
// @ts-expect-error
|
|
75
|
-
attest(() => declare<string>().type("'foo'")).type.errors(
|
|
76
|
-
`Argument of type 'string' is not assignable to parameter of type '{ declared: string; inferred: "foo"; }'`
|
|
77
|
-
)
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
it("wider", () => {
|
|
81
|
-
attest(() =>
|
|
82
|
-
declare<{ a: string }>().type({
|
|
83
|
-
// @ts-expect-error
|
|
84
|
-
a: "unknown"
|
|
85
|
-
})
|
|
86
|
-
).type.errors(
|
|
87
|
-
`Type 'string' is not assignable to type '{ declared: string; inferred: unknown; }'`
|
|
88
|
-
)
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
it("missing key", () => {
|
|
92
|
-
attest(() =>
|
|
93
|
-
// @ts-expect-error
|
|
94
|
-
declare<{ a: string; b: number }>().type({
|
|
95
|
-
a: "string"
|
|
96
|
-
})
|
|
97
|
-
).type.errors(
|
|
98
|
-
`Property 'b' is missing in type '{ a: "string"; }' but required in type '{ a: "string"; "b?": unknown; }'.`
|
|
99
|
-
)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
it("missing optional key", () => {
|
|
103
|
-
attest(() =>
|
|
104
|
-
// @ts-expect-error
|
|
105
|
-
declare<{ a: string; b?: number }>().type({
|
|
106
|
-
a: "string"
|
|
107
|
-
})
|
|
108
|
-
).type.errors(
|
|
109
|
-
`Property '"b?"' is missing in type '{ a: "string"; }' but required in type '{ a: "string"; "b?": unknown; }'.`
|
|
110
|
-
)
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
it("extraneous key", () => {
|
|
114
|
-
attest(() =>
|
|
115
|
-
declare<{ a: string }>().type({
|
|
116
|
-
a: "string",
|
|
117
|
-
// @ts-expect-error
|
|
118
|
-
b: "boolean"
|
|
119
|
-
})
|
|
120
|
-
).type.errors(
|
|
121
|
-
`Object literal may only specify known properties, and 'b' does not exist in type '{ a: "string"; }'.`
|
|
122
|
-
)
|
|
123
|
-
})
|
|
124
|
-
})
|
|
@@ -1,74 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,90 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,93 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,56 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import { schema } from "@arktype/schema"
|
|
3
|
-
import { type, type Out } from "arktype"
|
|
4
|
-
import { scope, type Ark } from "../ark.js"
|
|
5
|
-
import {
|
|
6
|
-
writeMissingRightOperandMessage,
|
|
7
|
-
writeUnresolvableMessage
|
|
8
|
-
} from "../parser/string/shift/operand/unenclosed.js"
|
|
9
|
-
import type { Type } from "../type.js"
|
|
10
|
-
|
|
11
|
-
describe("tuple expressions", () => {
|
|
12
|
-
it("nested", () => {
|
|
13
|
-
const t = type(["string|bigint", "|", ["number", "|", "boolean"]])
|
|
14
|
-
attest<string | number | bigint | boolean>(t.infer)
|
|
15
|
-
})
|
|
16
|
-
it("autocompletion", () => {
|
|
17
|
-
// @ts-expect-error
|
|
18
|
-
attest(() => type([""])).type.errors(
|
|
19
|
-
`IndexZeroOperator | keyof Ark | "this"`
|
|
20
|
-
)
|
|
21
|
-
// @ts-expect-error
|
|
22
|
-
attest(() => type(["string", ""])).type.errors(
|
|
23
|
-
`"keyof" | keyof Ark | "this" | IndexOneOperator'`
|
|
24
|
-
)
|
|
25
|
-
})
|
|
26
|
-
describe("errors", () => {
|
|
27
|
-
it("missing right operand", () => {
|
|
28
|
-
// @ts-expect-error
|
|
29
|
-
attest(() => type(["string", "|"])).throwsAndHasTypeError(
|
|
30
|
-
writeMissingRightOperandMessage("|", "")
|
|
31
|
-
)
|
|
32
|
-
// @ts-expect-error
|
|
33
|
-
attest(() => type(["string", "&"])).throwsAndHasTypeError(
|
|
34
|
-
writeMissingRightOperandMessage("&", "")
|
|
35
|
-
)
|
|
36
|
-
})
|
|
37
|
-
it("nested parse error", () => {
|
|
38
|
-
attest(() => {
|
|
39
|
-
// @ts-expect-error
|
|
40
|
-
type(["string", "|", "numbr"])
|
|
41
|
-
}).throwsAndHasTypeError(writeUnresolvableMessage("numbr"))
|
|
42
|
-
})
|
|
43
|
-
it("nested object parse error", () => {
|
|
44
|
-
attest(() => {
|
|
45
|
-
// @ts-expect-error
|
|
46
|
-
type([{ s: "strng" }, "|", "number"])
|
|
47
|
-
}).throwsAndHasTypeError(writeUnresolvableMessage("strng"))
|
|
48
|
-
})
|
|
49
|
-
// TODO: reenable
|
|
50
|
-
it("this", () => {
|
|
51
|
-
const t = type([{ a: "string" }, "|", { b: "this" }])
|
|
52
|
-
attest(t.infer).type.toString.snap(
|
|
53
|
-
"{ a: string; } | { b: { a: string; } | any; }"
|
|
54
|
-
)
|
|
55
|
-
const types = scope({
|
|
56
|
-
a: {
|
|
57
|
-
a: "string"
|
|
58
|
-
},
|
|
59
|
-
b: {
|
|
60
|
-
b: "expected"
|
|
61
|
-
},
|
|
62
|
-
expected: "a|b"
|
|
63
|
-
}).export()
|
|
64
|
-
attest(t.json).equals(types.expected.json)
|
|
65
|
-
})
|
|
66
|
-
})
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
describe("root expression", () => {
|
|
70
|
-
it("=== single", () => {
|
|
71
|
-
const t = type("===", 5)
|
|
72
|
-
attest<5>(t.infer)
|
|
73
|
-
attest(t.json).equals(type("5").json)
|
|
74
|
-
})
|
|
75
|
-
it("=== branches", () => {
|
|
76
|
-
const t = type("===", "foo", "bar", "baz")
|
|
77
|
-
attest<"foo" | "bar" | "baz">(t.infer)
|
|
78
|
-
attest(t.json).equals(schema.units("foo", "bar", "baz").json)
|
|
79
|
-
})
|
|
80
|
-
it("instanceof single", () => {
|
|
81
|
-
const t = type("instanceof", RegExp)
|
|
82
|
-
attest<RegExp>(t.infer)
|
|
83
|
-
attest(t.json).equals(schema(RegExp).json)
|
|
84
|
-
})
|
|
85
|
-
it("instanceof branches", () => {
|
|
86
|
-
const t = type("instanceof", Array, Date)
|
|
87
|
-
attest<unknown[] | Date>(t.infer)
|
|
88
|
-
attest(t.json).equals(schema(Array, Date).json)
|
|
89
|
-
})
|
|
90
|
-
it("postfix", () => {
|
|
91
|
-
const t = type({ a: "string" }, "[]")
|
|
92
|
-
attest<{ a: string }[]>(t.infer)
|
|
93
|
-
attest(t.json).equals(type({ a: "string" }).array().json)
|
|
94
|
-
})
|
|
95
|
-
it("infix", () => {
|
|
96
|
-
const t = type({ a: "string" }, "|", { b: "boolean" })
|
|
97
|
-
attest<
|
|
98
|
-
| {
|
|
99
|
-
a: string
|
|
100
|
-
}
|
|
101
|
-
| {
|
|
102
|
-
b: boolean
|
|
103
|
-
}
|
|
104
|
-
>(t.infer)
|
|
105
|
-
|
|
106
|
-
attest(t.json).equals(type({ a: "string" }).or({ b: "boolean" }).json)
|
|
107
|
-
})
|
|
108
|
-
it("morph", () => {
|
|
109
|
-
const t = type({ a: "string" }, "=>", (In) => ({ b: In.a }))
|
|
110
|
-
attest<Type<(In: { a: string }) => Out<{ b: string }>, Ark>>(t)
|
|
111
|
-
})
|
|
112
|
-
it("narrow", () => {
|
|
113
|
-
const t = type(
|
|
114
|
-
{ a: "string" },
|
|
115
|
-
":",
|
|
116
|
-
(In): In is { a: "foo" } => In.a === "foo"
|
|
117
|
-
)
|
|
118
|
-
attest<{ a: "foo" }>(t.infer)
|
|
119
|
-
})
|
|
120
|
-
it("this", () => {
|
|
121
|
-
const t = type({ a: "string" }, "|", { b: "this" })
|
|
122
|
-
attest(t.infer).type.toString.snap(
|
|
123
|
-
"{ a: string; } | { b: { a: string; } | any; }"
|
|
124
|
-
)
|
|
125
|
-
attest(t.json).equals(type([{ a: "string" }, "|", { b: "this" }]).json)
|
|
126
|
-
})
|
|
127
|
-
it("tuple as second arg", () => {
|
|
128
|
-
// this case is not fundamentally unique but TS has a hard time
|
|
129
|
-
// narrowing tuples in contexts like this
|
|
130
|
-
const t = type("keyof", [
|
|
131
|
-
{ a: "string" },
|
|
132
|
-
"&",
|
|
133
|
-
{ b: "boolean" }
|
|
134
|
-
// as const is required for TS <=5.0
|
|
135
|
-
] as const)
|
|
136
|
-
attest<"a" | "b">(t.infer)
|
|
137
|
-
})
|
|
138
|
-
})
|