arktype 2.0.0-dev.1 → 2.0.0-dev.3
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 +252 -71
- package/__tests__/config.test.ts +75 -166
- package/__tests__/declared.test.ts +5 -4
- package/__tests__/divisor.test.ts +6 -2
- package/__tests__/instanceof.test.ts +1 -1
- package/__tests__/keyTraversal.test.ts +71 -70
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +216 -26
- package/__tests__/narrow.test.ts +3 -3
- package/__tests__/objectLiteral.test.ts +19 -18
- package/match.ts +206 -79
- package/out/match.d.ts +60 -31
- package/out/match.d.ts.map +1 -1
- package/out/match.js +52 -10
- package/out/match.js.map +1 -1
- package/out/parser/definition.d.ts +6 -4
- package/out/parser/definition.d.ts.map +1 -1
- package/out/parser/definition.js.map +1 -1
- package/out/parser/objectLiteral.d.ts.map +1 -1
- package/out/parser/objectLiteral.js +31 -35
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/semantic.d.ts +6 -7
- package/out/parser/semantic/semantic.d.ts.map +1 -1
- package/out/parser/shared.d.ts +10 -2
- package/out/parser/shared.d.ts.map +1 -1
- package/out/parser/shared.js +13 -4
- package/out/parser/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +1 -1
- package/out/parser/string/reduce/static.d.ts.map +1 -1
- package/out/parser/tuple.d.ts +15 -14
- package/out/parser/tuple.d.ts.map +1 -1
- package/out/parser/tuple.js +94 -53
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +6 -11
- package/out/scope.d.ts.map +1 -1
- package/out/scope.js +7 -25
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +5 -9
- package/out/type.d.ts.map +1 -1
- package/out/type.js +14 -10
- package/out/type.js.map +1 -1
- package/package.json +3 -3
- package/parser/definition.ts +7 -7
- package/parser/objectLiteral.ts +47 -51
- package/parser/semantic/semantic.ts +8 -9
- package/parser/semantic/validate.ts +1 -1
- package/parser/shared.ts +27 -9
- package/parser/string/reduce/static.ts +2 -2
- package/parser/tuple.ts +175 -86
- package/scope.ts +12 -35
- package/type.ts +20 -18
- package/parser/semantic/intersections.ts +0 -46
|
@@ -51,10 +51,11 @@ describe("declared", () => {
|
|
|
51
51
|
attest<0 | 1>(t.infer)
|
|
52
52
|
})
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
// TODO: figure out narrowing
|
|
55
|
+
// it("regexp", () => {
|
|
56
|
+
// const t = declare<string>().type(/.*/)
|
|
57
|
+
// attest<string>(t.infer)
|
|
58
|
+
// })
|
|
58
59
|
|
|
59
60
|
it("Inferred<t>", () => {
|
|
60
61
|
const foo = type("'foo'")
|
|
@@ -9,6 +9,10 @@ describe("divisibility", () => {
|
|
|
9
9
|
const divisibleByTwo = type("number%2")
|
|
10
10
|
attest<number>(divisibleByTwo.infer)
|
|
11
11
|
attest(divisibleByTwo.json).snap({ basis: "number", divisor: 2 })
|
|
12
|
+
attest(divisibleByTwo(4).out).snap(4)
|
|
13
|
+
attest(divisibleByTwo(5).errors?.summary).snap(
|
|
14
|
+
"Must be a multiple of 2 (was 5)"
|
|
15
|
+
)
|
|
12
16
|
})
|
|
13
17
|
it("whitespace after %", () => {
|
|
14
18
|
const t = type("number % 5")
|
|
@@ -82,8 +86,8 @@ describe("divisibility", () => {
|
|
|
82
86
|
attest(t.json).equals(type("0").json)
|
|
83
87
|
})
|
|
84
88
|
it("invalid literal", () => {
|
|
85
|
-
attest(() => type("number%3&8")).throws(
|
|
86
|
-
"Intersection of
|
|
89
|
+
attest(() => type("number%3&8")).throws.snap(
|
|
90
|
+
"Error: Intersection of 8 and a number and a multiple of 3 results in an unsatisfiable type"
|
|
87
91
|
)
|
|
88
92
|
})
|
|
89
93
|
})
|
|
@@ -13,7 +13,7 @@ describe("instanceof", () => {
|
|
|
13
13
|
attest(t.json).equals(schema(Error).json)
|
|
14
14
|
const e = new Error()
|
|
15
15
|
attest(t(e).out).equals(e)
|
|
16
|
-
attest(t({}).errors?.summary).snap("Must be an Error (was
|
|
16
|
+
attest(t({}).errors?.summary).snap("Must be an Error (was object)")
|
|
17
17
|
})
|
|
18
18
|
it("inherited", () => {
|
|
19
19
|
const t = type(["instanceof", TypeError])
|
|
@@ -1,73 +1,74 @@
|
|
|
1
1
|
import { attest } from "@arktype/attest"
|
|
2
2
|
import { type } from "arktype"
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
})
|
|
4
|
+
// TODO: reenable
|
|
5
|
+
// describe("key traversal", () => {
|
|
6
|
+
// const getExtraneousB = () => ({ a: "ok", b: "why?" })
|
|
7
|
+
// it("loose by default", () => {
|
|
8
|
+
// const t = type({
|
|
9
|
+
// a: "string"
|
|
10
|
+
// })
|
|
11
|
+
// const dataWithExtraneousB = getExtraneousB()
|
|
12
|
+
// attest(t(dataWithExtraneousB).out).equals(dataWithExtraneousB)
|
|
13
|
+
// })
|
|
14
|
+
// it("invalid union", () => {
|
|
15
|
+
// const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
|
|
16
|
+
// keys: "strict"
|
|
17
|
+
// })
|
|
18
|
+
// attest(o({ a: 2 }).errors?.summary).snap(
|
|
19
|
+
// 'a must be a string or removed (was {"a":2})'
|
|
20
|
+
// )
|
|
21
|
+
// })
|
|
22
|
+
// it("distilled type", () => {
|
|
23
|
+
// const t = type({
|
|
24
|
+
// a: "string"
|
|
25
|
+
// }).configure({ keys: "distilled" })
|
|
26
|
+
// attest(t({ a: "ok" }).out).equals({ a: "ok" })
|
|
27
|
+
// attest(t(getExtraneousB()).out).snap({ a: "ok" })
|
|
28
|
+
// })
|
|
29
|
+
// it("distilled array", () => {
|
|
30
|
+
// const o = type({ a: "email[]" }).configure({
|
|
31
|
+
// keys: "distilled"
|
|
32
|
+
// })
|
|
33
|
+
// attest(o({ a: ["shawn@arktype.io"] }).out).snap({
|
|
34
|
+
// a: ["shawn@arktype.io"]
|
|
35
|
+
// })
|
|
36
|
+
// attest(o({ a: ["notAnEmail"] }).errors?.summary).snap(
|
|
37
|
+
// "a/0 must be a valid email (was 'notAnEmail')"
|
|
38
|
+
// )
|
|
39
|
+
// // can handle missing keys
|
|
40
|
+
// attest(o({ b: ["shawn"] }).errors?.summary).snap("a must be defined")
|
|
41
|
+
// })
|
|
42
|
+
// it("distilled union", () => {
|
|
43
|
+
// const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
|
|
44
|
+
// keys: "distilled"
|
|
45
|
+
// })
|
|
46
|
+
// // can distill to first branch
|
|
47
|
+
// attest(o({ a: "to", z: "bra" }).out).snap({ a: "to" })
|
|
48
|
+
// // can distill to second branch
|
|
49
|
+
// attest(o({ b: true, c: false }).out).snap({ b: true })
|
|
50
|
+
// // can handle missing keys
|
|
51
|
+
// attest(o({ a: 2 }).errors?.summary).snap(
|
|
52
|
+
// 'a must be a string or b must be defined (was {"a":2})'
|
|
53
|
+
// )
|
|
54
|
+
// })
|
|
55
|
+
// it("strict type", () => {
|
|
56
|
+
// const t = type({
|
|
57
|
+
// a: "string"
|
|
58
|
+
// }).configure({ keys: "strict" })
|
|
59
|
+
// attest(t({ a: "ok" }).out).equals({ a: "ok" })
|
|
60
|
+
// attest(t(getExtraneousB()).errors?.summary).snap("b must be removed")
|
|
61
|
+
// })
|
|
62
|
+
// it("strict array", () => {
|
|
63
|
+
// const o = type({ a: "string[]" }).configure({
|
|
64
|
+
// keys: "strict"
|
|
65
|
+
// })
|
|
66
|
+
// attest(o({ a: ["shawn"] }).out).snap({ a: ["shawn"] })
|
|
67
|
+
// attest(o({ a: [2] }).errors?.summary).snap(
|
|
68
|
+
// "a/0 must be a string (was number)"
|
|
69
|
+
// )
|
|
70
|
+
// attest(o({ b: ["shawn"] }).errors?.summary).snap(
|
|
71
|
+
// "b must be removed\na must be defined"
|
|
72
|
+
// )
|
|
73
|
+
// })
|
|
74
|
+
// })
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { bench } from "@arktype/attest"
|
|
2
|
+
import { match } from "arktype"
|
|
3
|
+
|
|
4
|
+
bench("general matchers", () => {
|
|
5
|
+
const matcher = match()
|
|
6
|
+
.when("string", (s) => s)
|
|
7
|
+
.when("number", (n) => n)
|
|
8
|
+
.when("boolean", (b) => b)
|
|
9
|
+
.orThrow()
|
|
10
|
+
|
|
11
|
+
const a = matcher("abc")
|
|
12
|
+
const b = matcher(4)
|
|
13
|
+
const c = matcher(true)
|
|
14
|
+
})
|
|
15
|
+
.median()
|
|
16
|
+
.types()
|
|
17
|
+
|
|
18
|
+
bench("match.only<T>", () => {
|
|
19
|
+
const matcher = match
|
|
20
|
+
.only<string | number | boolean>()
|
|
21
|
+
.when("string", (s) => s)
|
|
22
|
+
.when("number", (n) => n)
|
|
23
|
+
.when("boolean", (b) => b)
|
|
24
|
+
.finalize()
|
|
25
|
+
|
|
26
|
+
const a = matcher("abc")
|
|
27
|
+
const b = matcher(4)
|
|
28
|
+
const c = matcher(true)
|
|
29
|
+
})
|
|
30
|
+
.median()
|
|
31
|
+
.types()
|
package/__tests__/match.test.ts
CHANGED
|
@@ -1,34 +1,224 @@
|
|
|
1
|
-
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { match } from "arktype"
|
|
3
|
+
import { scope } from "../ark.js"
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// })
|
|
5
|
+
describe("match", () => {
|
|
6
|
+
it("properly infers types of inputs/outputs", () => {
|
|
7
|
+
const matcher = match({ string: (s) => s, number: (n) => n })
|
|
8
|
+
.when("boolean", (b) => b)
|
|
9
|
+
.orThrow()
|
|
9
10
|
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
// })
|
|
11
|
+
// properly infers the type of the output based on the input
|
|
12
|
+
attest<string>(matcher("abc")).equals("abc")
|
|
13
|
+
attest<number>(matcher(4)).equals(4)
|
|
14
|
+
attest<boolean>(matcher(true)).equals(true)
|
|
15
15
|
|
|
16
|
-
//
|
|
16
|
+
// and properly handles unions in the input type
|
|
17
|
+
attest<string | number>(matcher(0 as string | number))
|
|
18
|
+
})
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
it("`.when` errors on redundant cases", () => {
|
|
21
|
+
const matcher = match().when("string", (s) => s)
|
|
19
22
|
|
|
20
|
-
//
|
|
23
|
+
// @ts-expect-error
|
|
24
|
+
attest(() => matcher.when("string", (s) => s)).throwsAndHasTypeError(
|
|
25
|
+
"This branch is redundant and will never be reached" // TODO: rewrite error message
|
|
26
|
+
)
|
|
27
|
+
})
|
|
21
28
|
|
|
22
|
-
|
|
29
|
+
it("errors on cases redundant to a previous `cases` block", () => {
|
|
30
|
+
const matcher = match({ string: (s) => s })
|
|
23
31
|
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
// @ts-expect-error
|
|
33
|
+
attest(() => matcher.cases({ string: (s) => s })).throwsAndHasTypeError(
|
|
34
|
+
"This branch is redundant and will never be reached"
|
|
35
|
+
)
|
|
36
|
+
})
|
|
29
37
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
describe("constraint handling", () => {
|
|
39
|
+
it("properly considers constrained types as different from their base", () => {
|
|
40
|
+
const matcher = match
|
|
41
|
+
.only<number>()
|
|
42
|
+
.when("number>2", (n) => {
|
|
43
|
+
attest<number>(n)
|
|
44
|
+
return n
|
|
45
|
+
})
|
|
46
|
+
.when("number", (n) => n)
|
|
47
|
+
.finalize()
|
|
48
|
+
|
|
49
|
+
// for assertions
|
|
50
|
+
matcher(5)
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
describe('"finalizations"', () => {
|
|
55
|
+
it(".orThrow()", () => {
|
|
56
|
+
const matcher = match()
|
|
57
|
+
.when("string", (s) => s)
|
|
58
|
+
.orThrow()
|
|
59
|
+
|
|
60
|
+
// properly returns the `never` type and throws given a guaranteed-to-be-invalid input
|
|
61
|
+
attest<never>(matcher(4))
|
|
62
|
+
|
|
63
|
+
attest(() => matcher(4)).throws("TODO: what's the error message?") // TODO
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
describe(".default", () => {
|
|
67
|
+
it("chained, given a callback", () => {
|
|
68
|
+
const matcher = match()
|
|
69
|
+
.when("string", (s) => s)
|
|
70
|
+
.default((_) => 0)
|
|
71
|
+
|
|
72
|
+
attest<string>(matcher("abc")).equals("abc")
|
|
73
|
+
attest<number>(matcher(4)).equals(0)
|
|
74
|
+
|
|
75
|
+
attest<string | number>(matcher(0 as unknown))
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it("chained, given a value", () => {
|
|
79
|
+
const matcher = match()
|
|
80
|
+
.when("string", (s) => s)
|
|
81
|
+
.default(0)
|
|
82
|
+
|
|
83
|
+
attest<string>(matcher("abc")).equals("abc")
|
|
84
|
+
attest<number>(matcher(4)).equals(0)
|
|
85
|
+
|
|
86
|
+
attest<string | number>(matcher(0 as unknown))
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it("in `cases`, given a callback", () => {
|
|
90
|
+
const matcher = match({ string: (s) => s, default: (_) => 0 })
|
|
91
|
+
|
|
92
|
+
attest<string>(matcher("abc")).equals("abc")
|
|
93
|
+
attest<number>(matcher(4)).equals(0)
|
|
94
|
+
|
|
95
|
+
attest<string | number>(matcher(0 as unknown))
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it("errors when attempting to `.finalize()` a non-exhaustive matcher", () => {
|
|
100
|
+
const matcher = match().when("string", (s) => s)
|
|
101
|
+
|
|
102
|
+
// @ts-expect-error
|
|
103
|
+
attest(() => matcher.finalize()).throwsAndHasTypeError(
|
|
104
|
+
"Cannot manually finalize a non-exhaustive matcher: consider adding a `.default` case, using one of the `.orX` methods, or using `match.only<T>`" // TODO: rewrite message
|
|
105
|
+
)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it("considers `unknown` exhaustive", () => {
|
|
109
|
+
const matcher = match()
|
|
110
|
+
.when("unknown", (x) => x)
|
|
111
|
+
.finalize()
|
|
112
|
+
|
|
113
|
+
attest(matcher(4)).equals(4)
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
describe(".only<T>", () => {
|
|
118
|
+
it("does not accept invalid inputs at a type-level", () => {
|
|
119
|
+
const matcher = match
|
|
120
|
+
.only<string | number>()
|
|
121
|
+
.when("string", (s) => s)
|
|
122
|
+
.when("number", (n) => n)
|
|
123
|
+
.finalize()
|
|
124
|
+
|
|
125
|
+
// @ts-expect-error
|
|
126
|
+
attest(() => matcher(true)).throwsAndHasTypeError(
|
|
127
|
+
"Argument of type 'true' is not assignable to parameter of type 'string | number'"
|
|
128
|
+
) // TODO: what's the runtime error?
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it("errors when attempting to `.finalize()` a non-exhaustive matcher", () => {
|
|
132
|
+
const matcher = match.only<string | number>().when("string", (s) => s)
|
|
133
|
+
|
|
134
|
+
// @ts-expect-error
|
|
135
|
+
attest(() => matcher.finalize()).throwsAndHasTypeError(
|
|
136
|
+
"Cannot manually finalize a non-exhaustive matcher: consider adding a `.default` case, using one of the `.orX` methods, or handling the cases explicitly" // TODO: rewrite message. at runtime can we even show a counterexample (serialize the cases not handled)?
|
|
137
|
+
)
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it("allows finalizing exhaustive matchers", (_) => {
|
|
141
|
+
const matcher = match
|
|
142
|
+
.only<string | number>()
|
|
143
|
+
.when("string", (s) => s)
|
|
144
|
+
.when("number", (n) => n)
|
|
145
|
+
.finalize()
|
|
146
|
+
|
|
147
|
+
attest<string>(matcher("abc")).equals("abc")
|
|
148
|
+
attest<number>(matcher(4)).equals(4)
|
|
149
|
+
|
|
150
|
+
attest<string | number>(matcher(0 as string | number))
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it("infers the parameter to chained .default as the remaining cases", () => {
|
|
154
|
+
const matcher = match
|
|
155
|
+
.only<string | number | boolean>()
|
|
156
|
+
.when("string", (s) => s)
|
|
157
|
+
.default((n) => {
|
|
158
|
+
attest<number | boolean>(n)
|
|
159
|
+
return n
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
// for assertions
|
|
163
|
+
matcher(4)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it("infers the parameter to in-cases .default", () => {
|
|
167
|
+
const matcher = match.only<string | number | boolean>().cases({
|
|
168
|
+
string: (s) => s,
|
|
169
|
+
default: (n) => {
|
|
170
|
+
// TS doesn't understand sequentiality in cases, so it's inferred as the in-type
|
|
171
|
+
attest<string | number | boolean>(n)
|
|
172
|
+
return n
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
// for assertions
|
|
177
|
+
matcher(4)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it("returns `never` on only the specific cases handled by `.orThrow`", () => {
|
|
181
|
+
const matcher = match
|
|
182
|
+
.only<string | number>()
|
|
183
|
+
.when("string", (s) => s)
|
|
184
|
+
.orThrow()
|
|
185
|
+
|
|
186
|
+
attest<never>(matcher(4))
|
|
187
|
+
})
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
it("within scope", () => {
|
|
191
|
+
const threeSixtyNoScope = scope({ three: "3", sixty: "60", no: "'no'" })
|
|
192
|
+
|
|
193
|
+
const matcher = threeSixtyNoScope
|
|
194
|
+
.match({
|
|
195
|
+
three: (three) => {
|
|
196
|
+
attest<3>(three)
|
|
197
|
+
return 3
|
|
198
|
+
}
|
|
199
|
+
})
|
|
200
|
+
.when("sixty", (sixty) => {
|
|
201
|
+
attest<60>(sixty)
|
|
202
|
+
return 60
|
|
203
|
+
})
|
|
204
|
+
.orThrow()
|
|
205
|
+
|
|
206
|
+
// for assertions
|
|
207
|
+
matcher(3)
|
|
208
|
+
matcher(60)
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
it("properly propagates errors from invalid type definitions in `when`", () => {
|
|
212
|
+
// @ts-expect-error
|
|
213
|
+
attest(() => match().when("strong", (s) => s)).type.errors(
|
|
214
|
+
"'strong' is unresolvable"
|
|
215
|
+
)
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
it("properly propagates errors from invalid type definitions in `cases`", () => {
|
|
219
|
+
// @ts-expect-error
|
|
220
|
+
attest(() => match({ strong: (s) => s })).type.errors(
|
|
221
|
+
"'strong' is unresolvable"
|
|
222
|
+
)
|
|
223
|
+
})
|
|
224
|
+
})
|
package/__tests__/narrow.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { attest } from "@arktype/attest"
|
|
2
|
-
import type { Out } from "@arktype/schema"
|
|
2
|
+
import type { Out, is } from "@arktype/schema"
|
|
3
3
|
import type { equals } from "@arktype/util"
|
|
4
4
|
import { type, type Type } from "arktype"
|
|
5
5
|
|
|
@@ -52,7 +52,7 @@ describe("narrow", () => {
|
|
|
52
52
|
// })
|
|
53
53
|
it("functional predicate", () => {
|
|
54
54
|
const one = type(["number", ":", (n): n is 1 => n === 1])
|
|
55
|
-
attest<
|
|
55
|
+
attest<1>(one.infer)
|
|
56
56
|
})
|
|
57
57
|
it("functional parameter inference", () => {
|
|
58
58
|
type Expected = number | boolean[]
|
|
@@ -88,7 +88,7 @@ describe("narrow", () => {
|
|
|
88
88
|
const t = type("string")
|
|
89
89
|
.morph((s) => s.length)
|
|
90
90
|
.narrow((n): n is 5 => n === 5)
|
|
91
|
-
attest<Type<(In: string) => Out<5
|
|
91
|
+
attest<Type<(In: string) => Out<is<5, { anonymousPredicate: true }>>>>(t)
|
|
92
92
|
attest(t.json).snap({ domain: "string" })
|
|
93
93
|
})
|
|
94
94
|
it("expression", () => {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { attest } from "@arktype/attest"
|
|
2
|
-
import {
|
|
3
|
-
import { printable } from "@arktype/util"
|
|
2
|
+
import { printable, reference } from "@arktype/util"
|
|
4
3
|
import { scope, type } from "arktype"
|
|
5
4
|
import {
|
|
6
5
|
writeInvalidPropertyKeyMessage,
|
|
@@ -36,7 +35,7 @@ describe("object literal", () => {
|
|
|
36
35
|
})
|
|
37
36
|
it("symbol key", () => {
|
|
38
37
|
const s = Symbol()
|
|
39
|
-
const name =
|
|
38
|
+
const name = reference(s)
|
|
40
39
|
const t = type({
|
|
41
40
|
[s]: "string"
|
|
42
41
|
})
|
|
@@ -48,7 +47,7 @@ describe("object literal", () => {
|
|
|
48
47
|
})
|
|
49
48
|
it("optional symbol", () => {
|
|
50
49
|
const s = Symbol()
|
|
51
|
-
const name =
|
|
50
|
+
const name = reference(s)
|
|
52
51
|
const t = type({
|
|
53
52
|
[s]: "number?"
|
|
54
53
|
})
|
|
@@ -269,12 +268,13 @@ describe("object literal", () => {
|
|
|
269
268
|
attest(abc.json).equals(type({ ...a, ...b, ...c }).json)
|
|
270
269
|
attest(abc.json).equals(type([[a, "&", b], "&", c]).json)
|
|
271
270
|
})
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
})
|
|
271
|
+
// TODO: reenable
|
|
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
278
|
it("intersection", () => {
|
|
279
279
|
const t = type({ a: "number" }).and({ b: "boolean" })
|
|
280
280
|
// Should be simplified from {a: number} & {b: boolean} to {a: number, b: boolean}
|
|
@@ -289,12 +289,13 @@ describe("object literal", () => {
|
|
|
289
289
|
const o = type({ "\\[string]": "string" })
|
|
290
290
|
attest<{ "[string]": string }>(o.infer)
|
|
291
291
|
})
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
292
|
+
// TODO: reenable
|
|
293
|
+
// it("multiple bad strict", () => {
|
|
294
|
+
// const t = type({ a: "string", b: "boolean" }).configure({
|
|
295
|
+
// keys: "strict"
|
|
296
|
+
// })
|
|
297
|
+
// attest(t({ a: 1, b: 2 }).errors?.summary).snap(
|
|
298
|
+
// "a must be a string (was number)\nb must be boolean (was number)"
|
|
299
|
+
// )
|
|
300
|
+
// })
|
|
300
301
|
})
|