arktype 2.0.0-dev.1 → 2.0.0-dev.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/array.test.ts +270 -151
- package/__tests__/badDefinitionType.test.ts +10 -3
- package/__tests__/basis.test.ts +6 -2
- package/__tests__/bounds.test.ts +282 -352
- package/__tests__/cast.test.ts +7 -2
- package/__tests__/completions.test.ts +8 -3
- package/__tests__/config.test.ts +95 -167
- package/__tests__/dateLiteral.test.ts +8 -4
- package/__tests__/declared.test.ts +7 -6
- package/__tests__/discrimination.test.ts +1 -1
- package/__tests__/divisor.test.ts +46 -22
- package/__tests__/enclosed.test.ts +39 -25
- package/__tests__/examples.test.ts +56 -56
- package/__tests__/expressions.test.ts +184 -112
- package/__tests__/generic.test.ts +307 -288
- package/__tests__/group.test.ts +67 -54
- package/__tests__/imports.test.ts +62 -49
- package/__tests__/instanceof.test.ts +25 -23
- package/__tests__/intersection.test.ts +52 -48
- package/__tests__/keyTraversal.test.ts +77 -73
- package/__tests__/keyof.test.ts +68 -52
- package/__tests__/keywords.test.ts +164 -138
- package/__tests__/literal.test.ts +60 -21
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +222 -22
- package/__tests__/narrow.test.ts +52 -54
- package/__tests__/objectLiteral.test.ts +347 -205
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/pipe.test.ts +501 -0
- package/__tests__/realWorld.test.ts +63 -0
- package/__tests__/regex.test.ts +17 -7
- package/__tests__/scope.bench.ts +1 -1
- package/__tests__/scope.test.ts +257 -23
- package/__tests__/serialization.test.ts +19 -0
- package/__tests__/string.test.ts +11 -7
- package/__tests__/submodule.test.ts +26 -39
- package/__tests__/this.test.ts +83 -57
- package/__tests__/thunk.test.ts +13 -7
- package/__tests__/traverse.test.ts +76 -54
- package/__tests__/tsGenerics.test.ts +12 -12
- package/__tests__/type.test.ts +36 -0
- package/__tests__/typeReference.test.ts +7 -7
- package/__tests__/unenclosed.test.ts +116 -100
- package/__tests__/union.test.ts +171 -104
- package/ark.ts +21 -65
- package/generic.ts +69 -0
- package/main.ts +6 -18
- package/match.ts +195 -93
- package/module.ts +15 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/generic.js.map +1 -0
- package/out/main.d.ts +6 -7
- package/out/main.js +4 -3
- package/out/main.js.map +1 -1
- package/out/match.d.ts +57 -34
- package/out/match.js +51 -17
- package/out/match.js.map +1 -1
- package/out/module.d.ts +10 -0
- package/out/module.js +3 -0
- package/out/module.js.map +1 -0
- package/out/parser/definition.d.ts +11 -13
- package/out/parser/definition.js +10 -13
- package/out/parser/definition.js.map +1 -1
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts +45 -14
- package/out/parser/objectLiteral.js +61 -52
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/bounds.js.map +1 -1
- package/out/parser/semantic/divisor.d.ts +5 -7
- package/out/parser/semantic/divisor.js +0 -1
- package/out/parser/semantic/infer.d.ts +24 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/infer.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +15 -14
- package/out/parser/semantic/validate.js +1 -1
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +13 -13
- package/out/parser/string/reduce/dynamic.js +31 -26
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/reduce/shared.d.ts +6 -20
- package/out/parser/string/reduce/shared.js +0 -1
- package/out/parser/string/reduce/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +27 -24
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
- package/out/parser/string/shift/operand/enclosed.js +12 -13
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
- package/out/parser/string/shift/operand/genericArgs.js +6 -10
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +8 -12
- package/out/parser/string/shift/operand/operand.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts +9 -13
- package/out/parser/string/shift/operator/bounds.js +43 -54
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts +2 -4
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +2 -3
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/operator/operator.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts +2 -2
- package/out/parser/string/shift/scanner.js +14 -16
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts +8 -6
- package/out/parser/string/string.js +1 -3
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts +102 -43
- package/out/parser/tuple.js +119 -87
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +43 -120
- package/out/scope.js +39 -209
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +43 -64
- package/out/type.js +30 -131
- package/out/type.js.map +1 -1
- package/package.json +7 -3
- package/parser/definition.ts +77 -93
- package/parser/generic.ts +38 -40
- package/parser/objectLiteral.ts +172 -136
- package/parser/semantic/bounds.ts +33 -32
- package/parser/semantic/divisor.ts +8 -10
- package/parser/semantic/infer.ts +141 -0
- package/parser/semantic/utils.ts +12 -23
- package/parser/semantic/validate.ts +81 -73
- package/parser/string/reduce/dynamic.ts +67 -55
- package/parser/string/reduce/shared.ts +1 -3
- package/parser/string/reduce/static.ts +67 -69
- package/parser/string/shift/operand/date.ts +15 -14
- package/parser/string/shift/operand/enclosed.ts +30 -25
- package/parser/string/shift/operand/genericArgs.ts +46 -45
- package/parser/string/shift/operand/operand.ts +32 -38
- package/parser/string/shift/operand/unenclosed.ts +107 -158
- package/parser/string/shift/operator/bounds.ts +118 -112
- package/parser/string/shift/operator/divisor.ts +14 -17
- package/parser/string/shift/operator/operator.ts +35 -46
- package/parser/string/shift/scanner.ts +51 -50
- package/parser/string/string.ts +30 -35
- package/parser/tuple.ts +375 -307
- package/scope.ts +170 -453
- package/type.ts +193 -274
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/typeMethods.test.ts +0 -69
- package/out/ark.d.ts.map +0 -1
- package/out/arkConfig.d.ts +0 -2
- package/out/arkConfig.d.ts.map +0 -1
- package/out/arkConfig.js +0 -10
- package/out/arkConfig.js.map +0 -1
- package/out/main.d.ts.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/intersections.d.ts +0 -14
- package/out/parser/semantic/intersections.d.ts.map +0 -1
- package/out/parser/semantic/intersections.js +0 -2
- package/out/parser/semantic/intersections.js.map +0 -1
- package/out/parser/semantic/semantic.d.ts +0 -33
- package/out/parser/semantic/semantic.d.ts.map +0 -1
- package/out/parser/semantic/semantic.js +0 -2
- package/out/parser/semantic/semantic.js.map +0 -1
- package/out/parser/semantic/utils.d.ts.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/shared.d.ts +0 -54
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -47
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/test.d.ts +0 -2
- package/out/test.d.ts.map +0 -1
- package/out/test.js +0 -7
- package/out/test.js.map +0 -1
- package/out/tsconfig.build.tsbuildinfo +0 -1
- package/out/type.d.ts.map +0 -1
- package/out/util.d.ts +0 -19
- package/out/util.d.ts.map +0 -1
- package/out/util.js +0 -7
- package/out/util.js.map +0 -1
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -144
- package/parser/shared.ts +0 -152
- package/util.ts +0 -36
|
@@ -1,64 +1,133 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import { registry } from "@arktype/schema"
|
|
3
|
-
import { printable } from "@arktype/util"
|
|
4
|
-
import { scope, type } from "arktype"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
5
2
|
import {
|
|
6
3
|
writeInvalidPropertyKeyMessage,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
attest(o.json).snap({
|
|
21
|
-
basis: "object",
|
|
22
|
-
required: [
|
|
23
|
-
{ key: "a", value: "string" },
|
|
24
|
-
{ key: "b", value: "number" }
|
|
25
|
-
]
|
|
4
|
+
writeUnboundableMessage,
|
|
5
|
+
writeUnresolvableMessage
|
|
6
|
+
} from "@arktype/schema"
|
|
7
|
+
import { printable, registeredReference } from "@arktype/util"
|
|
8
|
+
import { scope, type } from "arktype"
|
|
9
|
+
import { writeInvalidSpreadTypeMessage } from "../parser/objectLiteral.js"
|
|
10
|
+
|
|
11
|
+
contextualize(
|
|
12
|
+
"named",
|
|
13
|
+
() => {
|
|
14
|
+
it("empty", () => {
|
|
15
|
+
const o = type({})
|
|
16
|
+
attest(o.json).equals(type("object").json)
|
|
26
17
|
})
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
18
|
+
|
|
19
|
+
it("required", () => {
|
|
20
|
+
const o = type({ a: "string", b: "number" })
|
|
21
|
+
attest<{ a: string; b: number }>(o.infer)
|
|
22
|
+
attest(o.json).snap({
|
|
23
|
+
domain: "object",
|
|
24
|
+
prop: [
|
|
25
|
+
{ key: "a", value: "string" },
|
|
26
|
+
{ key: "b", value: "number" }
|
|
27
|
+
]
|
|
28
|
+
})
|
|
35
29
|
})
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
30
|
+
|
|
31
|
+
it("optional keys", () => {
|
|
32
|
+
const o = type({ "a?": "string", b: "number" })
|
|
33
|
+
attest<{ a?: string; b: number }>(o.infer)
|
|
34
|
+
attest(o.json).snap({
|
|
35
|
+
domain: "object",
|
|
36
|
+
prop: [
|
|
37
|
+
{ key: "a", optional: true, value: "string" },
|
|
38
|
+
{ key: "b", value: "number" }
|
|
39
|
+
]
|
|
40
|
+
})
|
|
42
41
|
})
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
|
|
43
|
+
it("symbol key", () => {
|
|
44
|
+
const s = Symbol()
|
|
45
|
+
const name = registeredReference(s)
|
|
46
|
+
const t = type({
|
|
47
|
+
[s]: "string"
|
|
48
|
+
})
|
|
49
|
+
attest<{ [s]: string }>(t.infer)
|
|
50
|
+
attest(t.json).snap({
|
|
51
|
+
domain: "object",
|
|
52
|
+
prop: [{ key: name, value: "string" }]
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it("error in obj that has tuple that writes error at proper path", () => {
|
|
57
|
+
// @ts-expect-error
|
|
58
|
+
attest(() => type({ "a?": ["string", ["stringx", "?"]] }))
|
|
59
|
+
.throws(writeUnresolvableMessage("stringx"))
|
|
60
|
+
.type.errors.snap(
|
|
61
|
+
"Type '\"stringx\"' is not assignable to type '\"'stringx' is unresolvable \"'."
|
|
62
|
+
)
|
|
47
63
|
})
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const t = type({
|
|
53
|
-
[s]: "number?"
|
|
64
|
+
|
|
65
|
+
it("nested", () => {
|
|
66
|
+
const t = type({ "a?": { b: "boolean" } })
|
|
67
|
+
attest<{ a?: { b: boolean } }>(t.infer)
|
|
54
68
|
})
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
69
|
+
|
|
70
|
+
it("intersections", () => {
|
|
71
|
+
const a = { "a?": "string" } as const
|
|
72
|
+
const b = { b: "string" } as const
|
|
73
|
+
const c = { "c?": "string" } as const
|
|
74
|
+
const abc = type(a).and(b).and(c)
|
|
75
|
+
attest<{ a?: string; b: string; c?: string }>(abc.infer)
|
|
76
|
+
attest(abc.json).equals(type({ ...a, ...b, ...c }).json)
|
|
77
|
+
attest(abc.json).equals(type([[a, "&", b], "&", c]).json)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it("intersection", () => {
|
|
81
|
+
const t = type({ a: "number" }).and({ b: "boolean" })
|
|
82
|
+
// Should be simplified from {a: number} & {b: boolean} to {a: number, b: boolean}
|
|
83
|
+
attest(t.infer).type.toString.snap("{ a: number; b: boolean; }")
|
|
84
|
+
attest(t.json).equals(type({ a: "number", b: "boolean" }).json)
|
|
59
85
|
})
|
|
60
|
-
|
|
61
|
-
|
|
86
|
+
|
|
87
|
+
it("escaped optional token", () => {
|
|
88
|
+
const t = type({ "a\\?": "string" })
|
|
89
|
+
attest<{ "a?": string }>(t.infer)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it("traverse optional", () => {
|
|
93
|
+
const o = type({ "a?": "string" })
|
|
94
|
+
attest(o({ a: "a" })).snap({ a: "a" })
|
|
95
|
+
attest(o({})).snap({})
|
|
96
|
+
attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
// it("traverse strict optional", () => {
|
|
100
|
+
// // TODO: strict
|
|
101
|
+
// const o = type({ "a?": "string" })
|
|
102
|
+
// attest(o({ a: "a" })).snap({ a: "a" })
|
|
103
|
+
// attest(o({})).snap({})
|
|
104
|
+
// attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
105
|
+
// })
|
|
106
|
+
|
|
107
|
+
// it("multiple bad strict", () => {
|
|
108
|
+
// const t = type({ a: "string", b: "boolean" }).configure({
|
|
109
|
+
// keys: "strict"
|
|
110
|
+
// })
|
|
111
|
+
// attest(t({ a: 1, b: 2 }).toString()).snap(
|
|
112
|
+
// "a must be a string (was number)\nb must be boolean (was number)"
|
|
113
|
+
// )
|
|
114
|
+
// })
|
|
115
|
+
|
|
116
|
+
// it("optional symbol", () => {
|
|
117
|
+
// const s = Symbol()
|
|
118
|
+
// const name = reference(s)
|
|
119
|
+
// const t = type({
|
|
120
|
+
// [optional(s)]: "number"
|
|
121
|
+
// })
|
|
122
|
+
// attest<{ [s]?: number }>(t.infer)
|
|
123
|
+
// attest(t.json).equals({
|
|
124
|
+
// domain: "object",
|
|
125
|
+
// optional: [{ key: name, value: "number" }]
|
|
126
|
+
// })
|
|
127
|
+
// })
|
|
128
|
+
},
|
|
129
|
+
"spread syntax",
|
|
130
|
+
() => {
|
|
62
131
|
it("within scope", () => {
|
|
63
132
|
const s = scope({
|
|
64
133
|
user: { isAdmin: "false", name: "string" },
|
|
@@ -67,8 +136,8 @@ describe("object literal", () => {
|
|
|
67
136
|
|
|
68
137
|
attest<{ isAdmin: true; name: string }>(s.admin.infer)
|
|
69
138
|
attest(s.admin.json).equals({
|
|
70
|
-
|
|
71
|
-
|
|
139
|
+
domain: "object",
|
|
140
|
+
prop: [
|
|
72
141
|
{ key: "isAdmin", value: { unit: true } },
|
|
73
142
|
{ key: "name", value: "string" }
|
|
74
143
|
]
|
|
@@ -80,9 +149,9 @@ describe("object literal", () => {
|
|
|
80
149
|
const admin = type({ "...": user, isAdmin: "true" })
|
|
81
150
|
|
|
82
151
|
attest<{ isAdmin: true; name: string }>(admin.infer)
|
|
83
|
-
attest(admin.json).
|
|
84
|
-
|
|
85
|
-
|
|
152
|
+
attest(admin.json).snap({
|
|
153
|
+
domain: "object",
|
|
154
|
+
prop: [
|
|
86
155
|
{ key: "isAdmin", value: { unit: true } },
|
|
87
156
|
{ key: "name", value: "string" }
|
|
88
157
|
]
|
|
@@ -105,9 +174,12 @@ describe("object literal", () => {
|
|
|
105
174
|
}>(t.infer)
|
|
106
175
|
|
|
107
176
|
attest(t.json).snap({
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
{
|
|
177
|
+
domain: "object",
|
|
178
|
+
prop: [
|
|
179
|
+
{
|
|
180
|
+
key: "inherited",
|
|
181
|
+
value: [{ unit: false }, { unit: true }]
|
|
182
|
+
},
|
|
111
183
|
{ key: "overridden", value: "number" }
|
|
112
184
|
]
|
|
113
185
|
})
|
|
@@ -120,9 +192,9 @@ describe("object literal", () => {
|
|
|
120
192
|
|
|
121
193
|
attest<{ "...": string }>(t.infer)
|
|
122
194
|
|
|
123
|
-
attest(t.json).
|
|
124
|
-
|
|
125
|
-
|
|
195
|
+
attest(t.json).snap({
|
|
196
|
+
domain: "object",
|
|
197
|
+
prop: [{ key: "...", value: "string" }]
|
|
126
198
|
})
|
|
127
199
|
})
|
|
128
200
|
|
|
@@ -142,159 +214,229 @@ describe("object literal", () => {
|
|
|
142
214
|
|
|
143
215
|
attest<{ isAdmin: true; name: string }>(adminUser.infer)
|
|
144
216
|
attest(adminUser.json).snap({
|
|
145
|
-
|
|
146
|
-
|
|
217
|
+
domain: "object",
|
|
218
|
+
prop: [
|
|
147
219
|
{ key: "isAdmin", value: { unit: true } },
|
|
148
220
|
{ key: "name", value: "string" }
|
|
149
221
|
]
|
|
150
222
|
})
|
|
151
223
|
})
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
attest(
|
|
224
|
+
},
|
|
225
|
+
"index",
|
|
226
|
+
() => {
|
|
227
|
+
it("string index", () => {
|
|
228
|
+
const o = type({ "[string]": "string" })
|
|
229
|
+
attest<{ [x: string]: string }>(o.infer)
|
|
230
|
+
attest(o.json).snap({
|
|
231
|
+
domain: "object",
|
|
232
|
+
index: [{ key: "string", value: "string" }]
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
attest(o({})).equals({})
|
|
236
|
+
attest(o({ a: "a", b: "b" })).equals({ a: "a", b: "b" })
|
|
237
|
+
|
|
238
|
+
const validWithSymbol = { a: "a", [Symbol()]: null }
|
|
239
|
+
attest(validWithSymbol).equals(validWithSymbol)
|
|
240
|
+
|
|
241
|
+
attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
242
|
+
attest(o({ a: true, b: false }).toString())
|
|
243
|
+
.snap(`a must be a string (was true)
|
|
244
|
+
b must be a string (was false)`)
|
|
165
245
|
})
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
.
|
|
171
|
-
|
|
172
|
-
|
|
246
|
+
|
|
247
|
+
it("symbol index", () => {
|
|
248
|
+
const o = type({ "[symbol]": "1" })
|
|
249
|
+
attest<{ [x: symbol]: 1 }>(o.infer)
|
|
250
|
+
attest(o.json).snap({
|
|
251
|
+
domain: "object",
|
|
252
|
+
index: [{ key: "symbol", value: { unit: 1 } }]
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
attest(o({})).equals({})
|
|
256
|
+
|
|
257
|
+
attest(o({ a: 999 })).unknown.snap({ a: 999 })
|
|
258
|
+
|
|
259
|
+
const zildjian = Symbol()
|
|
260
|
+
const zildjianName = printable(zildjian)
|
|
261
|
+
|
|
262
|
+
// I've been dope, suspenseful with a pencil
|
|
263
|
+
// Ever since...
|
|
264
|
+
const prince = Symbol()
|
|
265
|
+
const princeName = printable(prince)
|
|
266
|
+
|
|
267
|
+
attest(o({ [zildjian]: 1, [prince]: 1 })).equals({
|
|
268
|
+
[zildjian]: 1,
|
|
269
|
+
[prince]: 1
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
attest({ a: 0, [zildjian]: 1 }).equals({ a: 0, [zildjian]: 1 })
|
|
273
|
+
|
|
274
|
+
attest(o({ [zildjian]: 0 }).toString()).equals(
|
|
275
|
+
`value at [${zildjianName}] must be 1 (was 0)`
|
|
173
276
|
)
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
attest<{ [x: string]: string }>(o.infer)
|
|
178
|
-
})
|
|
179
|
-
it("enumerable indexed union", () => {
|
|
180
|
-
const o = type({ "['foo' | 'bar']": "string" })
|
|
181
|
-
attest<{ foo: string; bar: string }>(o.infer)
|
|
182
|
-
})
|
|
183
|
-
it("non-enumerable indexed union", () => {
|
|
184
|
-
const o = type({ "[string | symbol]": "string" })
|
|
185
|
-
attest<{ [x: string]: string; [x: symbol]: string }>(o.infer)
|
|
186
|
-
})
|
|
187
|
-
it("multiple indexed", () => {
|
|
188
|
-
const o = type({
|
|
189
|
-
"[string]": "string",
|
|
190
|
-
"[symbol]": "number"
|
|
277
|
+
attest(o({ [prince]: null, [zildjian]: undefined }).toString())
|
|
278
|
+
.snap(`value at [${princeName}] must be 1 (was null)
|
|
279
|
+
value at [${zildjianName}] must be 1 (was undefined)`)
|
|
191
280
|
})
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
"optional?": "'bar'"
|
|
281
|
+
|
|
282
|
+
it("enumerable indexed union", () => {
|
|
283
|
+
const o = type({ "['foo' | 'bar']": "string" })
|
|
284
|
+
const expected = type({ foo: "string", bar: "string" })
|
|
285
|
+
attest<typeof expected>(o)
|
|
286
|
+
attest(o.json).equals(expected.json)
|
|
199
287
|
})
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
}).export()
|
|
209
|
-
type Key = symbol | "foo" | "bar" | "baz"
|
|
210
|
-
attest<Key>(types.key.infer)
|
|
211
|
-
attest<Record<Key, string>>(types.obj.infer)
|
|
212
|
-
})
|
|
213
|
-
it("syntax error in index definition", () => {
|
|
214
|
-
attest(() =>
|
|
215
|
-
type({
|
|
216
|
-
// @ts-expect-error
|
|
217
|
-
"[unresolvable]": "string"
|
|
288
|
+
|
|
289
|
+
it("non-enumerable indexed union", () => {
|
|
290
|
+
const o = type({ "[string | symbol]": "string" })
|
|
291
|
+
attest<{ [x: string]: string; [x: symbol]: string }>(o.infer)
|
|
292
|
+
attest(o.json).snap({
|
|
293
|
+
domain: "object",
|
|
294
|
+
index: [{ key: ["string", "symbol"], value: "string" }]
|
|
218
295
|
})
|
|
219
|
-
)
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
// @ts-expect-error
|
|
226
|
-
"[unresolvable]": "'unresolvable' is unresolvable"
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
it("multiple indexed", () => {
|
|
299
|
+
const o = type({
|
|
300
|
+
"[string]": "string",
|
|
301
|
+
"[symbol]": "number"
|
|
227
302
|
})
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
"[symbol<5]": "string"
|
|
303
|
+
attest<{ [x: string]: string; [x: symbol]: number }>(o.infer)
|
|
304
|
+
attest(o.json).snap({
|
|
305
|
+
domain: "object",
|
|
306
|
+
index: [
|
|
307
|
+
{ key: "string", value: "string" },
|
|
308
|
+
{ key: "symbol", value: "number" }
|
|
309
|
+
]
|
|
236
310
|
})
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
311
|
+
|
|
312
|
+
attest(o({})).equals({})
|
|
313
|
+
attest(o({ foo: "f" })).equals({ foo: "f" })
|
|
314
|
+
|
|
315
|
+
const sym = Symbol()
|
|
316
|
+
|
|
317
|
+
const symName = printable(sym)
|
|
318
|
+
|
|
319
|
+
const validWithStringsAndSymbols = {
|
|
320
|
+
str: "string",
|
|
321
|
+
[sym]: 8675309
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
attest(o(validWithStringsAndSymbols)).equals(validWithStringsAndSymbols)
|
|
325
|
+
|
|
326
|
+
attest(
|
|
327
|
+
o({
|
|
328
|
+
str: 100,
|
|
329
|
+
[sym]: "💯"
|
|
330
|
+
}).toString()
|
|
331
|
+
).snap(`str must be a string (was number)
|
|
332
|
+
value at [${symName}] must be a number (was string)`)
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
it("all key kinds", () => {
|
|
336
|
+
const o = type({
|
|
337
|
+
"[string]": "string",
|
|
338
|
+
required: "'foo'",
|
|
339
|
+
"optional?": "'bar'"
|
|
340
|
+
})
|
|
341
|
+
attest<{ [x: string]: string; required: "foo"; optional?: "bar" }>(
|
|
342
|
+
o.infer
|
|
343
|
+
)
|
|
344
|
+
attest(o.json).snap({
|
|
345
|
+
domain: "object",
|
|
346
|
+
prop: [
|
|
347
|
+
{ key: "optional", optional: true, value: { unit: "bar" } },
|
|
348
|
+
{ key: "required", value: { unit: "foo" } }
|
|
349
|
+
],
|
|
350
|
+
index: [{ key: "string", value: "string" }]
|
|
245
351
|
})
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
352
|
+
|
|
353
|
+
const valid: typeof o.infer = { required: "foo", other: "bar" }
|
|
354
|
+
attest(o(valid)).equals(valid)
|
|
355
|
+
attest(
|
|
356
|
+
o({
|
|
357
|
+
optional: "wrongString",
|
|
358
|
+
other: 0n
|
|
359
|
+
}).toString()
|
|
360
|
+
).snap(`optional must be "bar" (was "wrongString")
|
|
361
|
+
required must be "foo" (was missing)
|
|
362
|
+
other must be a string (was bigint)`)
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
it("index key from scope", () => {
|
|
366
|
+
const types = scope({
|
|
367
|
+
key: "symbol|'foo'|'bar'|'baz'",
|
|
368
|
+
obj: {
|
|
369
|
+
"[key]": "string"
|
|
370
|
+
}
|
|
371
|
+
}).export()
|
|
372
|
+
type Key = symbol | "foo" | "bar" | "baz"
|
|
373
|
+
attest<Key>(types.key.infer)
|
|
374
|
+
attest<Record<Key, string>>(types.obj.infer)
|
|
375
|
+
|
|
376
|
+
const expected = type({ "[symbol]": "string" }).and({
|
|
377
|
+
foo: "string",
|
|
378
|
+
bar: "string",
|
|
379
|
+
baz: "string"
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
attest(types.obj.json).snap(expected.json)
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
it("syntax error in index definition", () => {
|
|
386
|
+
attest(() =>
|
|
387
|
+
type({
|
|
388
|
+
// @ts-expect-error
|
|
389
|
+
"[unresolvable]": "string"
|
|
390
|
+
})
|
|
391
|
+
).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
it("does not allow syntax error message as value", () => {
|
|
395
|
+
attest(() =>
|
|
396
|
+
type({
|
|
397
|
+
// @ts-expect-error
|
|
398
|
+
"[unresolvable]": "'unresolvable' is unresolvable"
|
|
399
|
+
})
|
|
400
|
+
).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
|
|
401
|
+
})
|
|
402
|
+
|
|
403
|
+
it("semantic error in index definition", () => {
|
|
404
|
+
attest(() =>
|
|
405
|
+
type({
|
|
406
|
+
// @ts-expect-error
|
|
407
|
+
"[symbol<5]": "string"
|
|
408
|
+
})
|
|
409
|
+
).throwsAndHasTypeError(writeUnboundableMessage("symbol"))
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
it("invalid key type for index definition", () => {
|
|
413
|
+
attest(() =>
|
|
414
|
+
type({
|
|
415
|
+
// @ts-expect-error
|
|
416
|
+
"[object]": "string"
|
|
417
|
+
})
|
|
418
|
+
).throwsAndHasTypeError(writeInvalidPropertyKeyMessage("object"))
|
|
419
|
+
})
|
|
420
|
+
|
|
421
|
+
it("does not allow invalid key type error as value", () => {
|
|
422
|
+
attest(() =>
|
|
423
|
+
type({
|
|
424
|
+
// @ts-expect-error
|
|
425
|
+
"[object]":
|
|
426
|
+
"Indexed key definition 'object' must be a string, number or symbol"
|
|
427
|
+
})
|
|
428
|
+
)
|
|
429
|
+
.throws(writeUnresolvableMessage("Indexed"))
|
|
430
|
+
.type.errors(writeInvalidPropertyKeyMessage("object"))
|
|
431
|
+
})
|
|
432
|
+
|
|
433
|
+
it("escaped index", () => {
|
|
434
|
+
const o = type({ "\\[string]": "string" })
|
|
435
|
+
attest<{ "[string]": string }>(o.infer)
|
|
436
|
+
attest(o.json).snap({
|
|
437
|
+
domain: "object",
|
|
438
|
+
prop: [{ key: "[string]", value: "string" }]
|
|
255
439
|
})
|
|
256
|
-
).throwsAndHasTypeError(writeInvalidPropertyKeyMessage("object"))
|
|
257
|
-
})
|
|
258
|
-
|
|
259
|
-
it("nested", () => {
|
|
260
|
-
const t = type({ "a?": { b: "boolean" } })
|
|
261
|
-
attest<{ a?: { b: boolean } }>(t.infer)
|
|
262
|
-
})
|
|
263
|
-
it("intersections", () => {
|
|
264
|
-
const a = { "a?": "string" } as const
|
|
265
|
-
const b = { b: "string" } as const
|
|
266
|
-
const c = { "c?": "string" } as const
|
|
267
|
-
const abc = type(a).and(b).and(c)
|
|
268
|
-
attest<{ a?: string; b: string; c?: string }>(abc.infer)
|
|
269
|
-
attest(abc.json).equals(type({ ...a, ...b, ...c }).json)
|
|
270
|
-
attest(abc.json).equals(type([[a, "&", b], "&", c]).json)
|
|
271
|
-
})
|
|
272
|
-
it("traverse optional", () => {
|
|
273
|
-
const o = type({ "a?": "string" }).configure({ keys: "strict" })
|
|
274
|
-
attest(o({ a: "a" }).out).snap({ a: "a" })
|
|
275
|
-
attest(o({}).out).snap({})
|
|
276
|
-
attest(o({ a: 1 }).errors?.summary).snap("a must be a string (was number)")
|
|
277
|
-
})
|
|
278
|
-
it("intersection", () => {
|
|
279
|
-
const t = type({ a: "number" }).and({ b: "boolean" })
|
|
280
|
-
// Should be simplified from {a: number} & {b: boolean} to {a: number, b: boolean}
|
|
281
|
-
attest(t.infer).type.toString.snap("{ a: number; b: boolean; }")
|
|
282
|
-
attest(t.json).equals(type({ a: "number", b: "boolean" }).json)
|
|
283
|
-
})
|
|
284
|
-
it("escaped optional token", () => {
|
|
285
|
-
const t = type({ "a\\?": "string" })
|
|
286
|
-
attest<{ "a?": string }>(t.infer)
|
|
287
|
-
})
|
|
288
|
-
it("escaped index", () => {
|
|
289
|
-
const o = type({ "\\[string]": "string" })
|
|
290
|
-
attest<{ "[string]": string }>(o.infer)
|
|
291
|
-
})
|
|
292
|
-
it("multiple bad strict", () => {
|
|
293
|
-
const t = type({ a: "string", b: "boolean" }).configure({
|
|
294
|
-
keys: "strict"
|
|
295
440
|
})
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
)
|
|
299
|
-
})
|
|
300
|
-
})
|
|
441
|
+
}
|
|
442
|
+
)
|
|
@@ -106,25 +106,25 @@ bench("divisor", () => {
|
|
|
106
106
|
.types([1023, "instantiations"])
|
|
107
107
|
|
|
108
108
|
bench("filter-tuple", () => {
|
|
109
|
-
const _ = type(["boolean", ":",
|
|
109
|
+
const _ = type(["boolean", ":", b => b])
|
|
110
110
|
})
|
|
111
111
|
.median([8.04, "us"])
|
|
112
112
|
.types([583, "instantiations"])
|
|
113
113
|
|
|
114
114
|
bench("filter-chain", () => {
|
|
115
|
-
const _ = type("boolean").narrow(
|
|
115
|
+
const _ = type("boolean").narrow(b => b)
|
|
116
116
|
})
|
|
117
117
|
.median([12.19, "us"])
|
|
118
118
|
.types([42, "instantiations"])
|
|
119
119
|
|
|
120
120
|
bench("morph-tuple", () => {
|
|
121
|
-
const _ = type(["boolean", "=>",
|
|
121
|
+
const _ = type(["boolean", "=>", b => b])
|
|
122
122
|
})
|
|
123
123
|
.median([8.96, "us"])
|
|
124
124
|
.types([562, "instantiations"])
|
|
125
125
|
|
|
126
126
|
bench("morph-chain", () => {
|
|
127
|
-
const _ = type("boolean").
|
|
127
|
+
const _ = type("boolean").pipe(b => b)
|
|
128
128
|
})
|
|
129
129
|
.median([2.98, "us"])
|
|
130
130
|
.types([26, "instantiations"])
|