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
package/__tests__/match.test.ts
CHANGED
|
@@ -1,34 +1,234 @@
|
|
|
1
|
-
// import {
|
|
1
|
+
// import { attest } from "@arktype/attest"
|
|
2
|
+
// import { match, scope } from "arktype"
|
|
2
3
|
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
4
|
+
// it("cases only", () => {
|
|
5
|
+
// const sizeOf = match({
|
|
6
|
+
// "string|Array": (v) => v.length,
|
|
7
|
+
// number: (v) => v,
|
|
8
|
+
// bigint: (v) => v
|
|
9
|
+
// }).orThrow()
|
|
10
|
+
|
|
11
|
+
// attest<number>(sizeOf("abc")).equals(3)
|
|
12
|
+
// attest<number>(sizeOf([1, 2, 3])).equals(3)
|
|
13
|
+
// attest<bigint>(sizeOf(5n)).equals(5n)
|
|
14
|
+
// })
|
|
15
|
+
|
|
16
|
+
// it("properly infers types of inputs/outputs", () => {
|
|
17
|
+
// const matcher = match({ string: (s) => s, number: (n) => n })
|
|
18
|
+
// .when("boolean", (b) => b)
|
|
19
|
+
// .orThrow()
|
|
20
|
+
|
|
21
|
+
// // properly infers the type of the output based on the input
|
|
22
|
+
// attest<string>(matcher("abc")).equals("abc")
|
|
23
|
+
// attest<number>(matcher(4)).equals(4)
|
|
24
|
+
// attest<boolean>(matcher(true)).equals(true)
|
|
25
|
+
|
|
26
|
+
// // and properly handles unions in the input type
|
|
27
|
+
// attest<string | number>(matcher(0 as string | number))
|
|
28
|
+
// })
|
|
29
|
+
|
|
30
|
+
// it("`.when` errors on redundant cases", () => {
|
|
31
|
+
// const matcher = match().when("string", (s) => s)
|
|
32
|
+
|
|
33
|
+
// // @ts-expect-error
|
|
34
|
+
// attest(() => matcher.when("string", (s) => s)).throwsAndHasTypeError(
|
|
35
|
+
// "This branch is redundant and will never be reached" // TODO: rewrite error message
|
|
36
|
+
// )
|
|
37
|
+
// })
|
|
38
|
+
|
|
39
|
+
// it("errors on cases redundant to a previous `cases` block", () => {
|
|
40
|
+
// const matcher = match({ string: (s) => s })
|
|
41
|
+
|
|
42
|
+
// // @ts-expect-error
|
|
43
|
+
// attest(() => matcher.cases({ string: (s) => s })).throwsAndHasTypeError(
|
|
44
|
+
// "This branch is redundant and will never be reached"
|
|
45
|
+
// )
|
|
8
46
|
// })
|
|
9
47
|
|
|
10
|
-
//
|
|
11
|
-
// "
|
|
12
|
-
//
|
|
13
|
-
//
|
|
48
|
+
// // describe("constraint handling", () => {
|
|
49
|
+
// // it("properly considers constrained types as different from their base", () => {
|
|
50
|
+
// // const matcher = match
|
|
51
|
+
// // .only<number>()
|
|
52
|
+
// // .when("number>2", (n) => {
|
|
53
|
+
// // attest<number>(n)
|
|
54
|
+
// // return n
|
|
55
|
+
// // })
|
|
56
|
+
// // .when("number", (n) => n)
|
|
57
|
+
// // .finalize()
|
|
58
|
+
|
|
59
|
+
// // // for assertions
|
|
60
|
+
// // matcher(5)
|
|
61
|
+
// // })
|
|
62
|
+
// // })
|
|
63
|
+
|
|
64
|
+
// describe('"finalizations"', () => {
|
|
65
|
+
// it(".orThrow()", () => {
|
|
66
|
+
// const matcher = match()
|
|
67
|
+
// .when("string", (s) => s)
|
|
68
|
+
// .orThrow()
|
|
69
|
+
|
|
70
|
+
// // properly returns the `never` type and throws given a guaranteed-to-be-invalid input
|
|
71
|
+
// attest<never>(matcher(4))
|
|
72
|
+
|
|
73
|
+
// attest(() => matcher(4)).throws("TODO: what's the error message?") // TODO
|
|
74
|
+
// })
|
|
75
|
+
|
|
76
|
+
// // TODO: reenable
|
|
77
|
+
// // describe(".default", () => {
|
|
78
|
+
// // it("chained, given a callback", () => {
|
|
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("chained, given a value", () => {
|
|
90
|
+
// // const matcher = match()
|
|
91
|
+
// // .when("string", (s) => s)
|
|
92
|
+
// // .default(0)
|
|
93
|
+
|
|
94
|
+
// // attest<string>(matcher("abc")).equals("abc")
|
|
95
|
+
// // attest<number>(matcher(4)).equals(0)
|
|
96
|
+
|
|
97
|
+
// // attest<string | number>(matcher(0 as unknown))
|
|
98
|
+
// // })
|
|
99
|
+
|
|
100
|
+
// // it("in `cases`, given a callback", () => {
|
|
101
|
+
// // const matcher = match({ string: (s) => s, default: (_) => 0 })
|
|
102
|
+
|
|
103
|
+
// // attest<string>(matcher("abc")).equals("abc")
|
|
104
|
+
// // attest<number>(matcher(4)).equals(0)
|
|
105
|
+
|
|
106
|
+
// // attest<string | number>(matcher(0 as unknown))
|
|
107
|
+
// // })
|
|
108
|
+
// // })
|
|
109
|
+
|
|
110
|
+
// it("errors when attempting to `.finalize()` a non-exhaustive matcher", () => {
|
|
111
|
+
// const matcher = match().when("string", (s) => s)
|
|
112
|
+
|
|
113
|
+
// // @ts-expect-error
|
|
114
|
+
// attest(() => matcher.finalize()).throwsAndHasTypeError(
|
|
115
|
+
// "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
|
|
116
|
+
// )
|
|
117
|
+
// })
|
|
118
|
+
|
|
119
|
+
// it("considers `unknown` exhaustive", () => {
|
|
120
|
+
// const matcher = match()
|
|
121
|
+
// .when("unknown", (x) => x)
|
|
122
|
+
// .finalize()
|
|
123
|
+
|
|
124
|
+
// attest(matcher(4)).equals(4)
|
|
125
|
+
// })
|
|
14
126
|
// })
|
|
15
127
|
|
|
16
|
-
//
|
|
128
|
+
// describe(".only<T>", () => {
|
|
129
|
+
// it("does not accept invalid inputs at a type-level", () => {
|
|
130
|
+
// const matcher = match
|
|
131
|
+
// .only<string | number>()
|
|
132
|
+
// .when("string", (s) => s)
|
|
133
|
+
// .when("number", (n) => n)
|
|
134
|
+
// .finalize()
|
|
135
|
+
|
|
136
|
+
// // @ts-expect-error
|
|
137
|
+
// attest(() => matcher(true)).throwsAndHasTypeError(
|
|
138
|
+
// "Argument of type 'true' is not assignable to parameter of type 'string | number'"
|
|
139
|
+
// ) // TODO: what's the runtime error?
|
|
140
|
+
// })
|
|
141
|
+
|
|
142
|
+
// it("errors when attempting to `.finalize()` a non-exhaustive matcher", () => {
|
|
143
|
+
// const matcher = match.only<string | number>().when("string", (s) => s)
|
|
144
|
+
|
|
145
|
+
// // @ts-expect-error
|
|
146
|
+
// attest(() => matcher.finalize()).throwsAndHasTypeError(
|
|
147
|
+
// "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)?
|
|
148
|
+
// )
|
|
149
|
+
// })
|
|
150
|
+
|
|
151
|
+
// it("allows finalizing exhaustive matchers", (_) => {
|
|
152
|
+
// const matcher = match
|
|
153
|
+
// .only<string | number>()
|
|
154
|
+
// .when("string", (s) => s)
|
|
155
|
+
// .when("number", (n) => n)
|
|
156
|
+
// .finalize()
|
|
17
157
|
|
|
18
|
-
//
|
|
158
|
+
// attest<string>(matcher("abc")).equals("abc")
|
|
159
|
+
// attest<number>(matcher(4)).equals(4)
|
|
19
160
|
|
|
20
|
-
//
|
|
161
|
+
// attest<string | number>(matcher(0 as string | number))
|
|
162
|
+
// })
|
|
21
163
|
|
|
22
|
-
//
|
|
164
|
+
// it("infers the parameter to chained .default as the remaining cases", () => {
|
|
165
|
+
// const matcher = match
|
|
166
|
+
// .only<string | number | boolean>()
|
|
167
|
+
// .when("string", (s) => s)
|
|
168
|
+
// .default((n) => {
|
|
169
|
+
// attest<number | boolean>(n)
|
|
170
|
+
// return n
|
|
171
|
+
// })
|
|
172
|
+
|
|
173
|
+
// // for assertions
|
|
174
|
+
// matcher(4)
|
|
175
|
+
// })
|
|
176
|
+
|
|
177
|
+
// it("infers the parameter to in-cases .default", () => {
|
|
178
|
+
// const matcher = match.only<string | number | boolean>().cases({
|
|
179
|
+
// string: (s) => s,
|
|
180
|
+
// default: (n) => {
|
|
181
|
+
// // TS doesn't understand sequentiality in cases, so it's inferred as the in-type
|
|
182
|
+
// attest<string | number | boolean>(n)
|
|
183
|
+
// return n
|
|
184
|
+
// }
|
|
185
|
+
// })
|
|
186
|
+
|
|
187
|
+
// // for assertions
|
|
188
|
+
// matcher(4)
|
|
189
|
+
// })
|
|
190
|
+
|
|
191
|
+
// it("returns `never` on only the specific cases handled by `.orThrow`", () => {
|
|
192
|
+
// const matcher = match
|
|
193
|
+
// .only<string | number>()
|
|
194
|
+
// .when("string", (s) => s)
|
|
195
|
+
// .orThrow()
|
|
196
|
+
|
|
197
|
+
// attest<never>(matcher(4))
|
|
198
|
+
// })
|
|
199
|
+
// })
|
|
200
|
+
|
|
201
|
+
// it("within scope", () => {
|
|
202
|
+
// const threeSixtyNoScope = scope({ three: "3", sixty: "60", no: "'no'" })
|
|
203
|
+
|
|
204
|
+
// const matcher = threeSixtyNoScope
|
|
205
|
+
// .match({
|
|
206
|
+
// three: (three) => {
|
|
207
|
+
// attest<3>(three)
|
|
208
|
+
// return 3
|
|
209
|
+
// }
|
|
210
|
+
// })
|
|
211
|
+
// .when("sixty", (sixty) => {
|
|
212
|
+
// attest<60>(sixty)
|
|
213
|
+
// return 60
|
|
214
|
+
// })
|
|
215
|
+
// .orThrow()
|
|
216
|
+
|
|
217
|
+
// // for assertions
|
|
218
|
+
// matcher(3)
|
|
219
|
+
// matcher(60)
|
|
220
|
+
// })
|
|
23
221
|
|
|
24
|
-
//
|
|
25
|
-
// //
|
|
26
|
-
//
|
|
27
|
-
//
|
|
222
|
+
// it("properly propagates errors from invalid type definitions in `when`", () => {
|
|
223
|
+
// // @ts-expect-error
|
|
224
|
+
// attest(() => match().when("strong", (s) => s)).type.errors(
|
|
225
|
+
// "'strong' is unresolvable"
|
|
226
|
+
// )
|
|
28
227
|
// })
|
|
29
228
|
|
|
30
|
-
//
|
|
31
|
-
// //
|
|
32
|
-
//
|
|
33
|
-
//
|
|
229
|
+
// it("properly propagates errors from invalid type definitions in `cases`", () => {
|
|
230
|
+
// // @ts-expect-error
|
|
231
|
+
// attest(() => match({ strong: (s) => s })).type.errors(
|
|
232
|
+
// "'strong' is unresolvable"
|
|
233
|
+
// )
|
|
34
234
|
// })
|
package/__tests__/narrow.test.ts
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import type { Out } from "@arktype/schema"
|
|
3
|
-
import type
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import type { Narrowed, Out, of, string } from "@arktype/schema"
|
|
3
|
+
import { registeredReference, type equals } from "@arktype/util"
|
|
4
4
|
import { type, type Type } from "arktype"
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
contextualize(() => {
|
|
7
7
|
it("implicit problem", () => {
|
|
8
8
|
const isOdd = (n: number) => n % 2 === 1
|
|
9
|
+
const isOddRef = registeredReference(isOdd)
|
|
9
10
|
const odd = type(["number", ":", isOdd])
|
|
10
11
|
attest<number>(odd.infer)
|
|
11
|
-
|
|
12
|
-
attest(odd(1)
|
|
13
|
-
attest(odd(2).
|
|
14
|
-
"Must be valid according to isOdd (was 2)"
|
|
15
|
-
)
|
|
12
|
+
attest(odd.json).equals({ domain: "number", predicate: [isOddRef] })
|
|
13
|
+
attest(odd(1)).equals(1)
|
|
14
|
+
attest(odd(2).toString()).snap("must be valid according to isOdd (was 2)")
|
|
16
15
|
})
|
|
16
|
+
|
|
17
17
|
it("implicit problem anonymous", () => {
|
|
18
|
-
const even = type(
|
|
19
|
-
attest(even(1).
|
|
18
|
+
const even = type("number", ":", n => n % 2 === 0)
|
|
19
|
+
attest(even(1).toString()).snap(
|
|
20
|
+
"must be valid according to an anonymous predicate (was 1)"
|
|
21
|
+
)
|
|
20
22
|
})
|
|
23
|
+
|
|
21
24
|
it("explicit problem", () => {
|
|
22
25
|
const even = type([
|
|
23
26
|
"number",
|
|
24
27
|
":",
|
|
25
|
-
(n, ctx) => n % 3 === 0 || ctx.invalid(
|
|
28
|
+
(n, ctx) => n % 3 === 0 || ctx.invalid("divisible by 3")
|
|
29
|
+
])
|
|
30
|
+
attest(even(1).toString()).snap("must be divisible by 3 (was 1)")
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it("problem at path", () => {
|
|
34
|
+
const abEqual = type([
|
|
35
|
+
{
|
|
36
|
+
a: "number",
|
|
37
|
+
b: "number"
|
|
38
|
+
},
|
|
39
|
+
":",
|
|
40
|
+
({ a, b }, ctx) => {
|
|
41
|
+
if (a === b) return true
|
|
42
|
+
|
|
43
|
+
ctx.error({ expected: "equal to b", path: ["a"] })
|
|
44
|
+
ctx.error({ expected: "equal to a", path: ["b"] })
|
|
45
|
+
return false
|
|
46
|
+
}
|
|
26
47
|
])
|
|
27
|
-
attest(
|
|
48
|
+
attest(abEqual({ a: 1, b: 1 })).equals({ a: 1, b: 1 })
|
|
49
|
+
attest(abEqual({ a: 1, b: 2 }).toString()).snap(
|
|
50
|
+
'a must be equal to b (was {"a":1,"b":2})\nb must be equal to a (was {"a":1,"b":2})'
|
|
51
|
+
)
|
|
28
52
|
})
|
|
29
|
-
|
|
30
|
-
// type([{ s: "string" }])
|
|
31
|
-
// const abEqual = type([
|
|
32
|
-
// {
|
|
33
|
-
// a: "number",
|
|
34
|
-
// b: "number"
|
|
35
|
-
// },
|
|
36
|
-
// ":",
|
|
37
|
-
// ({ a, b }, ctx) => {
|
|
38
|
-
// if (a === b) {
|
|
39
|
-
// return true
|
|
40
|
-
// }
|
|
41
|
-
// // a, ["a"]
|
|
42
|
-
// ctx.error("equal to b")
|
|
43
|
-
// // b, ["b"]
|
|
44
|
-
// ctx.error("equal to a")
|
|
45
|
-
// return false
|
|
46
|
-
// }
|
|
47
|
-
// ])
|
|
48
|
-
// attest(abEqual({ a: 1, b: 1 }).out).equals({ a: 1, b: 1 })
|
|
49
|
-
// attest(abEqual({ a: 1, b: 2 }).errors?.summary).snap(
|
|
50
|
-
// 'a must be equal to b (was {"a":1,"b":2})\nb must be equal to a (was {"a":1,"b":2})'
|
|
51
|
-
// )
|
|
52
|
-
// })
|
|
53
|
+
|
|
53
54
|
it("functional predicate", () => {
|
|
54
55
|
const one = type(["number", ":", (n): n is 1 => n === 1])
|
|
55
|
-
attest<
|
|
56
|
+
attest<1>(one.infer)
|
|
56
57
|
})
|
|
58
|
+
|
|
57
59
|
it("functional parameter inference", () => {
|
|
58
60
|
type Expected = number | boolean[]
|
|
59
61
|
const validateNumberOrBooleanList = <t>(
|
|
60
62
|
t: equals<t, Expected> extends true ? t : Expected
|
|
61
63
|
) => true
|
|
62
64
|
attest<number | boolean[]>(
|
|
63
|
-
type([
|
|
64
|
-
|
|
65
|
-
":",
|
|
66
|
-
(data) => validateNumberOrBooleanList(data)
|
|
67
|
-
]).infer
|
|
65
|
+
type(["number|boolean[]", ":", data => validateNumberOrBooleanList(data)])
|
|
66
|
+
.infer
|
|
68
67
|
)
|
|
69
68
|
attest(() => {
|
|
70
69
|
// @ts-expect-error
|
|
71
70
|
type(["number|boolean[]", ":", (data: number | string[]) => !!data])
|
|
72
71
|
}).type.errors("Type 'boolean' is not assignable to type 'string'.")
|
|
73
72
|
})
|
|
73
|
+
|
|
74
74
|
it("narrow problem", () => {
|
|
75
75
|
const palindrome = type([
|
|
76
76
|
"string",
|
|
@@ -78,25 +78,23 @@ describe("narrow", () => {
|
|
|
78
78
|
(s, ctx) =>
|
|
79
79
|
s === [...s].reverse().join("") ? true : ctx.invalid("a palindrome")
|
|
80
80
|
])
|
|
81
|
-
attest<Type<string>>(palindrome)
|
|
82
|
-
attest(palindrome("dad")
|
|
83
|
-
attest(palindrome("david").
|
|
84
|
-
|
|
81
|
+
attest<Type<string.narrowed>>(palindrome)
|
|
82
|
+
attest(palindrome("dad")).snap("dad")
|
|
83
|
+
attest(palindrome("david").toString()).snap(
|
|
84
|
+
'must be a palindrome (was "david")'
|
|
85
85
|
)
|
|
86
86
|
})
|
|
87
|
+
|
|
87
88
|
it("narrows the output type of a morph", () => {
|
|
88
89
|
const t = type("string")
|
|
89
|
-
.
|
|
90
|
+
.pipe(s => s.length)
|
|
90
91
|
.narrow((n): n is 5 => n === 5)
|
|
91
|
-
|
|
92
|
-
attest(
|
|
92
|
+
|
|
93
|
+
attest<Type<(In: string) => Out<of<5, Narrowed>>, {}>>(t)
|
|
93
94
|
})
|
|
95
|
+
|
|
94
96
|
it("expression", () => {
|
|
95
97
|
const t = type("string", ":", (s): s is `f${string}` => s[0] === "f")
|
|
96
98
|
attest<`f${string}`>(t.infer)
|
|
97
|
-
attest(t.json).snap({
|
|
98
|
-
basis: "string",
|
|
99
|
-
predicate: ["$ark.anonymousFunction8"]
|
|
100
|
-
})
|
|
101
99
|
})
|
|
102
100
|
})
|