arktype 2.0.0-dev.0 → 2.0.0-dev.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/array.test.ts +270 -151
- package/__tests__/badDefinitionType.test.ts +10 -3
- package/__tests__/basis.test.ts +6 -2
- package/__tests__/bounds.test.ts +282 -352
- package/__tests__/cast.test.ts +7 -2
- package/__tests__/completions.test.ts +8 -3
- package/__tests__/config.test.ts +95 -167
- package/__tests__/dateLiteral.test.ts +8 -4
- package/__tests__/declared.test.ts +7 -6
- package/__tests__/discrimination.test.ts +1 -1
- package/__tests__/divisor.test.ts +46 -22
- package/__tests__/enclosed.test.ts +39 -25
- package/__tests__/examples.test.ts +56 -56
- package/__tests__/expressions.test.ts +184 -112
- package/__tests__/generic.test.ts +307 -288
- package/__tests__/group.test.ts +67 -54
- package/__tests__/imports.test.ts +62 -49
- package/__tests__/instanceof.test.ts +25 -23
- package/__tests__/intersection.test.ts +52 -48
- package/__tests__/keyTraversal.test.ts +77 -73
- package/__tests__/keyof.test.ts +68 -52
- package/__tests__/keywords.test.ts +164 -138
- package/__tests__/literal.test.ts +60 -21
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +222 -22
- package/__tests__/narrow.test.ts +52 -54
- package/__tests__/objectLiteral.test.ts +347 -205
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/pipe.test.ts +501 -0
- package/__tests__/realWorld.test.ts +63 -0
- package/__tests__/regex.test.ts +17 -7
- package/__tests__/scope.bench.ts +1 -1
- package/__tests__/scope.test.ts +257 -23
- package/__tests__/serialization.test.ts +19 -0
- package/__tests__/string.test.ts +11 -7
- package/__tests__/submodule.test.ts +26 -39
- package/__tests__/this.test.ts +83 -57
- package/__tests__/thunk.test.ts +13 -7
- package/__tests__/traverse.test.ts +76 -54
- package/__tests__/tsGenerics.test.ts +12 -12
- package/__tests__/type.test.ts +36 -0
- package/__tests__/typeReference.test.ts +7 -7
- package/__tests__/unenclosed.test.ts +116 -100
- package/__tests__/union.test.ts +171 -104
- package/ark.ts +21 -65
- package/generic.ts +69 -0
- package/main.ts +6 -18
- package/match.ts +195 -93
- package/module.ts +15 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/generic.js.map +1 -0
- package/out/main.d.ts +6 -7
- package/out/main.js +4 -3
- package/out/main.js.map +1 -1
- package/out/match.d.ts +57 -34
- package/out/match.js +51 -17
- package/out/match.js.map +1 -1
- package/out/module.d.ts +10 -0
- package/out/module.js +3 -0
- package/out/module.js.map +1 -0
- package/out/parser/definition.d.ts +11 -13
- package/out/parser/definition.js +10 -13
- package/out/parser/definition.js.map +1 -1
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts +45 -14
- package/out/parser/objectLiteral.js +61 -52
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/bounds.js.map +1 -1
- package/out/parser/semantic/divisor.d.ts +5 -7
- package/out/parser/semantic/divisor.js +0 -1
- package/out/parser/semantic/infer.d.ts +24 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/infer.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +15 -14
- package/out/parser/semantic/validate.js +1 -1
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +13 -13
- package/out/parser/string/reduce/dynamic.js +31 -26
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/reduce/shared.d.ts +6 -20
- package/out/parser/string/reduce/shared.js +0 -1
- package/out/parser/string/reduce/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +27 -24
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
- package/out/parser/string/shift/operand/enclosed.js +12 -13
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
- package/out/parser/string/shift/operand/genericArgs.js +6 -10
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +8 -12
- package/out/parser/string/shift/operand/operand.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts +9 -13
- package/out/parser/string/shift/operator/bounds.js +43 -54
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts +2 -4
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +2 -3
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/operator/operator.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts +2 -2
- package/out/parser/string/shift/scanner.js +14 -16
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts +8 -6
- package/out/parser/string/string.js +1 -3
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts +102 -43
- package/out/parser/tuple.js +119 -87
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +43 -120
- package/out/scope.js +39 -209
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +43 -64
- package/out/type.js +30 -131
- package/out/type.js.map +1 -1
- package/package.json +7 -3
- package/parser/definition.ts +77 -93
- package/parser/generic.ts +38 -40
- package/parser/objectLiteral.ts +172 -136
- package/parser/semantic/bounds.ts +33 -32
- package/parser/semantic/divisor.ts +8 -10
- package/parser/semantic/infer.ts +141 -0
- package/parser/semantic/utils.ts +12 -23
- package/parser/semantic/validate.ts +81 -73
- package/parser/string/reduce/dynamic.ts +67 -55
- package/parser/string/reduce/shared.ts +1 -3
- package/parser/string/reduce/static.ts +67 -69
- package/parser/string/shift/operand/date.ts +15 -14
- package/parser/string/shift/operand/enclosed.ts +30 -25
- package/parser/string/shift/operand/genericArgs.ts +46 -45
- package/parser/string/shift/operand/operand.ts +32 -38
- package/parser/string/shift/operand/unenclosed.ts +107 -158
- package/parser/string/shift/operator/bounds.ts +118 -112
- package/parser/string/shift/operator/divisor.ts +14 -17
- package/parser/string/shift/operator/operator.ts +35 -46
- package/parser/string/shift/scanner.ts +51 -50
- package/parser/string/string.ts +30 -35
- package/parser/tuple.ts +375 -307
- package/scope.ts +170 -453
- package/type.ts +193 -274
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/typeMethods.test.ts +0 -69
- package/out/ark.d.ts.map +0 -1
- package/out/arkConfig.d.ts +0 -2
- package/out/arkConfig.d.ts.map +0 -1
- package/out/arkConfig.js +0 -10
- package/out/arkConfig.js.map +0 -1
- package/out/main.d.ts.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/intersections.d.ts +0 -14
- package/out/parser/semantic/intersections.d.ts.map +0 -1
- package/out/parser/semantic/intersections.js +0 -2
- package/out/parser/semantic/intersections.js.map +0 -1
- package/out/parser/semantic/semantic.d.ts +0 -33
- package/out/parser/semantic/semantic.d.ts.map +0 -1
- package/out/parser/semantic/semantic.js +0 -2
- package/out/parser/semantic/semantic.js.map +0 -1
- package/out/parser/semantic/utils.d.ts.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/shared.d.ts +0 -54
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -47
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/test.d.ts +0 -2
- package/out/test.d.ts.map +0 -1
- package/out/test.js +0 -7
- package/out/test.js.map +0 -1
- package/out/tsconfig.build.tsbuildinfo +0 -1
- package/out/type.d.ts.map +0 -1
- package/out/util.d.ts +0 -19
- package/out/util.d.ts.map +0 -1
- package/out/util.js +0 -7
- package/out/util.js.map +0 -1
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -144
- package/parser/shared.ts +0 -152
- package/util.ts +0 -36
package/__tests__/group.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import { type } from "arktype"
|
|
3
3
|
import {
|
|
4
4
|
writeUnclosedGroupMessage,
|
|
@@ -6,67 +6,80 @@ import {
|
|
|
6
6
|
} from "../parser/string/reduce/shared.js"
|
|
7
7
|
import { writeExpressionExpectedMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
contextualize(() => {
|
|
10
10
|
it("entire expression", () => {
|
|
11
|
-
|
|
11
|
+
const t = type("(string)")
|
|
12
|
+
const expected = type("string")
|
|
13
|
+
attest<typeof expected>(t)
|
|
14
|
+
attest(t.json).equals(expected.json)
|
|
12
15
|
})
|
|
16
|
+
|
|
13
17
|
it("overrides default precedence", () => {
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
const t = type("(boolean|number)[]")
|
|
19
|
+
const expected = type("boolean|number").array()
|
|
20
|
+
attest<typeof expected>(t)
|
|
21
|
+
attest(t.json).equals(expected.json)
|
|
16
22
|
})
|
|
23
|
+
|
|
17
24
|
it("nested", () => {
|
|
18
25
|
attest<((number | boolean)[] | (string | undefined)[])[]>(
|
|
19
26
|
type("((boolean|number)[]|(string|undefined)[])[]").infer
|
|
20
27
|
)
|
|
21
28
|
})
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
29
|
+
|
|
30
|
+
it("empty", () => {
|
|
31
|
+
attest(() => {
|
|
32
|
+
// @ts-expect-error
|
|
33
|
+
type("()")
|
|
34
|
+
}).throws(writeExpressionExpectedMessage(")"))
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it("unmatched (", () => {
|
|
38
|
+
attest(() => {
|
|
39
|
+
// @ts-expect-error
|
|
40
|
+
type("string|(boolean|number[]")
|
|
41
|
+
}).throwsAndHasTypeError(writeUnclosedGroupMessage(")"))
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it("unmatched )", () => {
|
|
45
|
+
attest(() => {
|
|
46
|
+
// @ts-expect-error
|
|
47
|
+
type("string|number[]|boolean)")
|
|
48
|
+
}).throwsAndHasTypeError(writeUnmatchedGroupCloseMessage(""))
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it("lone )", () => {
|
|
52
|
+
attest(() => {
|
|
53
|
+
// @ts-expect-error
|
|
54
|
+
type(")")
|
|
55
|
+
}).throws(writeExpressionExpectedMessage(")"))
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it("lone (", () => {
|
|
59
|
+
attest(() => {
|
|
60
|
+
// @ts-expect-error
|
|
61
|
+
type("(")
|
|
62
|
+
}).throws(writeExpressionExpectedMessage(""))
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it("deep unmatched (", () => {
|
|
66
|
+
attest(() => {
|
|
67
|
+
// @ts-expect-error
|
|
68
|
+
type("(null|(undefined|(1))|2")
|
|
69
|
+
}).throwsAndHasTypeError(writeUnclosedGroupMessage(")"))
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it("deep unmatched )", () => {
|
|
73
|
+
attest(() => {
|
|
74
|
+
// @ts-expect-error
|
|
75
|
+
type("((string|number)[]|boolean))[]")
|
|
76
|
+
}).throwsAndHasTypeError(writeUnmatchedGroupCloseMessage("[]"))
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it("starting )", () => {
|
|
80
|
+
attest(() => {
|
|
81
|
+
// @ts-expect-error
|
|
82
|
+
type(")number(")
|
|
83
|
+
}).throws(writeExpressionExpectedMessage(")number("))
|
|
71
84
|
})
|
|
72
85
|
})
|
|
@@ -1,24 +1,27 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { writeDuplicateAliasError } from "@arktype/schema"
|
|
2
3
|
import { lazily } from "@arktype/util"
|
|
3
|
-
import {
|
|
4
|
+
import { type Module, scope, type } from "arktype"
|
|
5
|
+
import { writePrefixedPrivateReferenceMessage } from "../parser/semantic/validate.js"
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const yesScope = lazily(() => scope({ yes: "'yes'" }))
|
|
7
|
+
const threeSixtyNoScope = lazily(() =>
|
|
8
|
+
scope({
|
|
9
|
+
three: "3",
|
|
10
|
+
sixty: "60",
|
|
11
|
+
no: "'no'"
|
|
12
|
+
})
|
|
13
|
+
)
|
|
14
|
+
const yesScope = lazily(() => scope({ yes: "'yes'" }))
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
const threeSixtyNoModule = lazily(() => threeSixtyNoScope.export())
|
|
17
|
+
const yesModule = lazily(() => yesScope.export())
|
|
17
18
|
|
|
19
|
+
contextualize(() => {
|
|
18
20
|
it("single", () => {
|
|
19
21
|
const $ = scope({
|
|
20
|
-
...threeSixtyNoModule
|
|
21
|
-
|
|
22
|
+
...threeSixtyNoModule,
|
|
23
|
+
threeSixtyNo: "three|sixty|no"
|
|
24
|
+
})
|
|
22
25
|
attest<{ threeSixtyNo: 3 | 60 | "no" }>($.infer)
|
|
23
26
|
})
|
|
24
27
|
|
|
@@ -29,25 +32,14 @@ describe("scope imports", () => {
|
|
|
29
32
|
extra: "true"
|
|
30
33
|
})
|
|
31
34
|
|
|
32
|
-
const imported =
|
|
35
|
+
const imported = scope({
|
|
36
|
+
...base.import(),
|
|
33
37
|
a: "three|sixty|no|yes|extra"
|
|
34
38
|
})
|
|
35
39
|
|
|
36
40
|
attest<{ a: 3 | 60 | "no" | "yes" | true }>(imported.infer)
|
|
37
41
|
})
|
|
38
42
|
|
|
39
|
-
// TODO: fix, tests for more duplicate scenarios
|
|
40
|
-
// it("duplicate alias", () => {
|
|
41
|
-
// attest(() =>
|
|
42
|
-
// scope({ a: "boolean" })
|
|
43
|
-
// .scope(
|
|
44
|
-
// // @ts-expect-error
|
|
45
|
-
// { a: "string" }
|
|
46
|
-
// )
|
|
47
|
-
// .export()
|
|
48
|
-
// ).throwsAndHasTypeError(writeDuplicateAliasesMessage("a"))
|
|
49
|
-
// })
|
|
50
|
-
|
|
51
43
|
it("import & export", () => {
|
|
52
44
|
const threeSixtyNoScope = scope({
|
|
53
45
|
three: "3",
|
|
@@ -72,22 +64,15 @@ describe("scope imports", () => {
|
|
|
72
64
|
|
|
73
65
|
attest<
|
|
74
66
|
Module<{
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
three: 3
|
|
81
|
-
no: "no"
|
|
82
|
-
private: string
|
|
83
|
-
}
|
|
84
|
-
ambient: Ark
|
|
67
|
+
hasCrept: true
|
|
68
|
+
public: string | true | 3
|
|
69
|
+
"#three": 3
|
|
70
|
+
"#no": "no"
|
|
71
|
+
"#private": string
|
|
85
72
|
}>
|
|
86
73
|
>(types)
|
|
87
74
|
})
|
|
88
|
-
})
|
|
89
75
|
|
|
90
|
-
describe("private aliases", () => {
|
|
91
76
|
it("non-generic", () => {
|
|
92
77
|
const types = scope({
|
|
93
78
|
foo: "bar[]",
|
|
@@ -97,17 +82,45 @@ describe("private aliases", () => {
|
|
|
97
82
|
attest(types.foo.json).equals(type("boolean[]").json)
|
|
98
83
|
attest<
|
|
99
84
|
Module<{
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
ambient: Ark
|
|
85
|
+
foo: boolean[]
|
|
86
|
+
"#bar": boolean
|
|
103
87
|
}>
|
|
104
88
|
>(types)
|
|
105
89
|
})
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
90
|
+
|
|
91
|
+
it("errors on private reference with #", () => {
|
|
92
|
+
attest(() =>
|
|
93
|
+
scope({
|
|
94
|
+
// @ts-expect-error
|
|
95
|
+
xdd: "#kekw",
|
|
96
|
+
"#kekw": "true"
|
|
97
|
+
}).export()
|
|
98
|
+
).throwsAndHasTypeError(writePrefixedPrivateReferenceMessage("#kekw"))
|
|
112
99
|
})
|
|
100
|
+
|
|
101
|
+
it("errors on public and private refrence with same name", () => {
|
|
102
|
+
attest(() =>
|
|
103
|
+
scope({
|
|
104
|
+
kekw: "1",
|
|
105
|
+
// @ts-expect-error
|
|
106
|
+
"#kekw": "1"
|
|
107
|
+
}).export()
|
|
108
|
+
).throwsAndHasTypeError(writeDuplicateAliasError("kekw"))
|
|
109
|
+
attest(() =>
|
|
110
|
+
scope({
|
|
111
|
+
// @ts-expect-error
|
|
112
|
+
"#kekw": "1",
|
|
113
|
+
kekw: "1"
|
|
114
|
+
}).export()
|
|
115
|
+
).throwsAndHasTypeError(writeDuplicateAliasError("kekw"))
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
// TODO: reenable
|
|
119
|
+
// it("generic", () => {
|
|
120
|
+
// const types = scope({
|
|
121
|
+
// foo: "bar<string>[]",
|
|
122
|
+
// "#bar<t>": ["t"]
|
|
123
|
+
// }).export()
|
|
124
|
+
// attest<[string][]>(types.foo.infer)
|
|
125
|
+
// })
|
|
113
126
|
})
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import {
|
|
3
|
-
import { type } from "arktype"
|
|
4
|
-
import type { Ark } from "../ark.js"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { rawSchema } from "@arktype/schema"
|
|
3
|
+
import { type Type, type } from "arktype"
|
|
5
4
|
import { writeInvalidConstructorMessage } from "../parser/tuple.js"
|
|
6
|
-
import type { Type } from "../type.js"
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
contextualize(
|
|
7
|
+
"tuple expression",
|
|
8
|
+
() => {
|
|
10
9
|
it("base", () => {
|
|
11
10
|
const t = type(["instanceof", Error])
|
|
12
11
|
attest<Error>(t.infer)
|
|
13
|
-
|
|
12
|
+
const expected = rawSchema(Error)
|
|
13
|
+
attest(t.json).equals(expected.json)
|
|
14
14
|
const e = new Error()
|
|
15
|
-
attest(t(e)
|
|
16
|
-
attest(t({}).
|
|
15
|
+
attest(t(e)).equals(e)
|
|
16
|
+
attest(t({}).toString()).snap("must be an Error (was object)")
|
|
17
17
|
})
|
|
18
18
|
it("inherited", () => {
|
|
19
19
|
const t = type(["instanceof", TypeError])
|
|
@@ -21,9 +21,9 @@ describe("instanceof", () => {
|
|
|
21
21
|
// for some reason the return of TypeError's constructor is actually
|
|
22
22
|
// inferred as Error? Disabling this check for now, seems like an anomaly.
|
|
23
23
|
// attest<TypeError>(t.infer)
|
|
24
|
-
attest(t(e)
|
|
25
|
-
attest(t(new Error()).
|
|
26
|
-
"
|
|
24
|
+
attest(t(e)).equals(e)
|
|
25
|
+
attest(t(new Error()).toString()).snap(
|
|
26
|
+
"must be an instance of TypeError (was Error)"
|
|
27
27
|
)
|
|
28
28
|
})
|
|
29
29
|
it("abstract", () => {
|
|
@@ -36,7 +36,7 @@ describe("instanceof", () => {
|
|
|
36
36
|
const t = type(["instanceof", Base])
|
|
37
37
|
attest<Base>(t.infer)
|
|
38
38
|
const sub = new Sub()
|
|
39
|
-
attest(t(sub)
|
|
39
|
+
attest(t(sub)).equals(sub)
|
|
40
40
|
})
|
|
41
41
|
it("multiple branches", () => {
|
|
42
42
|
const t = type(["instanceof", Date, Array])
|
|
@@ -53,14 +53,14 @@ describe("instanceof", () => {
|
|
|
53
53
|
private isArk = true
|
|
54
54
|
}
|
|
55
55
|
const ark = type(["instanceof", ArkClass])
|
|
56
|
-
attest<Type<ArkClass,
|
|
56
|
+
attest<Type<ArkClass, {}>>(ark)
|
|
57
57
|
// not expanded since there are no morphs
|
|
58
58
|
attest(ark.infer).type.toString("ArkClass")
|
|
59
59
|
attest(ark.in.infer).type.toString("ArkClass")
|
|
60
60
|
const a = new ArkClass()
|
|
61
|
-
attest(ark(a)
|
|
62
|
-
attest(ark({}).
|
|
63
|
-
"
|
|
61
|
+
attest(ark(a)).equals(a)
|
|
62
|
+
attest(ark({}).toString()).snap(
|
|
63
|
+
"must be an instance of ArkClass (was object)"
|
|
64
64
|
)
|
|
65
65
|
})
|
|
66
66
|
// TODO: Fix- Investigate bidirectional check impact on perf to narrow private props without breaking this case:
|
|
@@ -75,13 +75,15 @@ describe("instanceof", () => {
|
|
|
75
75
|
private isArk = true
|
|
76
76
|
}
|
|
77
77
|
const ark = type(["instanceof", ArkClass])
|
|
78
|
-
|
|
78
|
+
|
|
79
|
+
attest<Type<ArkClass, {}>>(ark)
|
|
79
80
|
// not expanded since there are no morphs
|
|
80
81
|
attest(ark.infer).type.toString("ArkClass")
|
|
81
82
|
attest(ark.in.infer).type.toString("ArkClass")
|
|
82
83
|
})
|
|
83
|
-
}
|
|
84
|
-
|
|
84
|
+
},
|
|
85
|
+
"root expression",
|
|
86
|
+
() => {
|
|
85
87
|
it("class", () => {
|
|
86
88
|
const t = type("instanceof", Error)
|
|
87
89
|
attest<Error>(t.infer)
|
|
@@ -98,5 +100,5 @@ describe("instanceof", () => {
|
|
|
98
100
|
writeInvalidConstructorMessage("Error")
|
|
99
101
|
)
|
|
100
102
|
})
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
+
}
|
|
104
|
+
)
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import { writeIndivisibleMessage } from "@arktype/schema"
|
|
3
|
-
import { type } from "arktype"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
4
2
|
import {
|
|
5
|
-
|
|
3
|
+
keywordNodes,
|
|
4
|
+
writeIndivisibleMessage,
|
|
6
5
|
writeUnresolvableMessage
|
|
7
|
-
} from "
|
|
6
|
+
} from "@arktype/schema"
|
|
7
|
+
import { type } from "arktype"
|
|
8
|
+
import { writeMissingRightOperandMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
contextualize(() => {
|
|
10
11
|
it("two types", () => {
|
|
11
12
|
const t = type("boolean&true")
|
|
12
13
|
attest<true>(t.infer)
|
|
13
14
|
attest(t.json).is(type("true").json)
|
|
14
15
|
})
|
|
16
|
+
|
|
15
17
|
it("intersection parsed before union", () => {
|
|
16
18
|
// Should be parsed as:
|
|
17
19
|
// 1. "0" | ("1"&"string") | "2"
|
|
@@ -20,25 +22,29 @@ describe("intersection", () => {
|
|
|
20
22
|
attest<"0" | "1" | "2">(t.infer)
|
|
21
23
|
attest(t.json).equals(type("===", "0", "1", "2").json)
|
|
22
24
|
})
|
|
25
|
+
|
|
23
26
|
it("tuple expression", () => {
|
|
24
27
|
const t = type([{ a: "string" }, "&", { b: "number" }])
|
|
25
28
|
attest<{ a: string; b: number }>(t.infer)
|
|
26
29
|
attest(t.json).equals(type({ a: "string", b: "number" }).json)
|
|
27
30
|
})
|
|
31
|
+
|
|
28
32
|
it("several types", () => {
|
|
29
33
|
const t = type("unknown&boolean&false")
|
|
30
34
|
attest<false>(t.infer)
|
|
31
35
|
attest(t.json).equals(type("false").json)
|
|
32
36
|
})
|
|
37
|
+
|
|
33
38
|
it("method", () => {
|
|
34
39
|
const t = type({ a: "string" }).and({ b: "boolean" })
|
|
35
40
|
attest<{ a: string; b: boolean }>(t.infer)
|
|
36
41
|
attest(t.json).equals(type({ a: "string", b: "boolean" }).json)
|
|
37
42
|
})
|
|
43
|
+
|
|
38
44
|
it("chained deep intersections", () => {
|
|
39
|
-
const b = type({ b: "boolean" }, "=>",
|
|
45
|
+
const b = type({ b: "boolean" }, "=>", o => [o.b])
|
|
40
46
|
const t = type({
|
|
41
|
-
a: ["string", "=>",
|
|
47
|
+
a: ["string", "=>", s => s.length]
|
|
42
48
|
})
|
|
43
49
|
.and({
|
|
44
50
|
// unable to inline this due to:
|
|
@@ -59,46 +65,44 @@ describe("intersection", () => {
|
|
|
59
65
|
|
|
60
66
|
attest<{ a: number; b: boolean[]; c: "hello" }>(t.infer)
|
|
61
67
|
})
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
68
|
+
it("bad reference", () => {
|
|
69
|
+
// @ts-expect-error
|
|
70
|
+
attest(() => type("boolean&tru"))
|
|
71
|
+
.throws(writeUnresolvableMessage("tru"))
|
|
72
|
+
.type.errors("boolean&true")
|
|
73
|
+
})
|
|
74
|
+
it("double and", () => {
|
|
75
|
+
// @ts-expect-error
|
|
76
|
+
attest(() => type("boolean&&true")).throws(
|
|
77
|
+
writeMissingRightOperandMessage("&", "&true")
|
|
78
|
+
)
|
|
79
|
+
})
|
|
80
|
+
it("implicit never", () => {
|
|
81
|
+
attest(() => type("string&number")).throws.snap(
|
|
82
|
+
"ParseError: Intersection of string and number results in an unsatisfiable type"
|
|
83
|
+
)
|
|
84
|
+
})
|
|
85
|
+
it("left semantic error", () => {
|
|
86
|
+
// @ts-expect-error
|
|
87
|
+
attest(() => type("string%2&'foo'")).throwsAndHasTypeError(
|
|
88
|
+
writeIndivisibleMessage(keywordNodes.string)
|
|
89
|
+
)
|
|
90
|
+
})
|
|
91
|
+
it("right semantic error", () => {
|
|
92
|
+
// @ts-expect-error
|
|
93
|
+
attest(() => type("'foo'&string%2")).throwsAndHasTypeError(
|
|
94
|
+
writeIndivisibleMessage(keywordNodes.string)
|
|
95
|
+
)
|
|
96
|
+
})
|
|
97
|
+
it("chained validation error", () => {
|
|
98
|
+
attest(() =>
|
|
87
99
|
// @ts-expect-error
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
type({ a: "string" }).and({ b: "what" })
|
|
96
|
-
).throwsAndHasTypeError(writeUnresolvableMessage("what"))
|
|
97
|
-
})
|
|
98
|
-
it("at path", () => {
|
|
99
|
-
attest(() => type({ a: "string" }).and({ a: "number" })).throws(
|
|
100
|
-
"Intersection at a of a string and a number results in an unsatisfiable type"
|
|
101
|
-
)
|
|
102
|
-
})
|
|
100
|
+
type({ a: "string" }).and({ b: "what" })
|
|
101
|
+
).throwsAndHasTypeError(writeUnresolvableMessage("what"))
|
|
102
|
+
})
|
|
103
|
+
it("error at path", () => {
|
|
104
|
+
attest(() => type({ a: "string" }).and({ a: "number" })).throws.snap(
|
|
105
|
+
"ParseError: Intersection at a of string and number results in an unsatisfiable type"
|
|
106
|
+
)
|
|
103
107
|
})
|
|
104
108
|
})
|