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
package/type.ts
CHANGED
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
keywords,
|
|
4
4
|
type ArkResult,
|
|
5
5
|
type BaseMeta,
|
|
6
|
-
type KeyCheckKind,
|
|
7
6
|
type Morph,
|
|
8
7
|
type Out,
|
|
9
8
|
type Predicate,
|
|
@@ -12,13 +11,13 @@ import {
|
|
|
12
11
|
type extractIn,
|
|
13
12
|
type extractOut,
|
|
14
13
|
type includesMorphs,
|
|
14
|
+
type inferIntersection,
|
|
15
15
|
type inferMorphOut,
|
|
16
16
|
type inferNarrow
|
|
17
17
|
} from "@arktype/schema"
|
|
18
18
|
import {
|
|
19
19
|
Callable,
|
|
20
|
-
|
|
21
|
-
map,
|
|
20
|
+
morph,
|
|
22
21
|
type Constructor,
|
|
23
22
|
type Json,
|
|
24
23
|
type conform
|
|
@@ -32,7 +31,7 @@ import {
|
|
|
32
31
|
parseGenericParams,
|
|
33
32
|
type GenericParamsParseError
|
|
34
33
|
} from "./parser/generic.js"
|
|
35
|
-
import
|
|
34
|
+
import { configureShallowDescendants } from "./parser/shared.js"
|
|
36
35
|
import type {
|
|
37
36
|
IndexOneOperator,
|
|
38
37
|
IndexZeroOperator,
|
|
@@ -121,11 +120,6 @@ export type DefinitionParser<$> = <def>(
|
|
|
121
120
|
def: validateDefinition<def, $, bindThis<def>>
|
|
122
121
|
) => def
|
|
123
122
|
|
|
124
|
-
export type TypeConfig = {
|
|
125
|
-
keys?: KeyCheckKind
|
|
126
|
-
mustBe?: string
|
|
127
|
-
}
|
|
128
|
-
|
|
129
123
|
export class Type<t = unknown, $ = any> extends Callable<
|
|
130
124
|
(data: unknown) => ArkResult<distill<extractOut<t>>>
|
|
131
125
|
> {
|
|
@@ -133,10 +127,9 @@ export class Type<t = unknown, $ = any> extends Callable<
|
|
|
133
127
|
// TODO: in/out?
|
|
134
128
|
declare infer: distill<extractOut<t>>
|
|
135
129
|
|
|
136
|
-
config: TypeConfig
|
|
137
130
|
root: TypeNode<t>
|
|
138
131
|
allows: this["root"]["allows"]
|
|
139
|
-
|
|
132
|
+
description: string
|
|
140
133
|
json: Json
|
|
141
134
|
|
|
142
135
|
constructor(
|
|
@@ -147,14 +140,19 @@ export class Type<t = unknown, $ = any> extends Callable<
|
|
|
147
140
|
super(root.apply, root)
|
|
148
141
|
this.root = root
|
|
149
142
|
this.allows = root.allows
|
|
150
|
-
this.config = scope.config
|
|
151
143
|
this.json = root.json
|
|
152
|
-
this.
|
|
144
|
+
this.description = this.root.description
|
|
153
145
|
}
|
|
154
146
|
|
|
155
|
-
configure(
|
|
156
|
-
|
|
157
|
-
|
|
147
|
+
configure(configOrDescription: BaseMeta | string): this {
|
|
148
|
+
return new Type(
|
|
149
|
+
configureShallowDescendants(this.root, configOrDescription),
|
|
150
|
+
this.scope
|
|
151
|
+
) as never
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
describe(description: string): this {
|
|
155
|
+
return this.configure(description)
|
|
158
156
|
}
|
|
159
157
|
|
|
160
158
|
// TODO: should return out
|
|
@@ -252,6 +250,10 @@ export class Type<t = unknown, $ = any> extends Callable<
|
|
|
252
250
|
this.outCache ??= new Type(this.root.out, this.scope) as never
|
|
253
251
|
return this.outCache
|
|
254
252
|
}
|
|
253
|
+
|
|
254
|
+
toString() {
|
|
255
|
+
return this.description
|
|
256
|
+
}
|
|
255
257
|
}
|
|
256
258
|
|
|
257
259
|
const parseTypeRoot = (def: unknown, scope: Scope, args?: BoundArgs) =>
|
|
@@ -278,7 +280,7 @@ export const validateUninstantiatedGeneric = (g: Generic) => {
|
|
|
278
280
|
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
279
281
|
{
|
|
280
282
|
baseName: "generic",
|
|
281
|
-
args:
|
|
283
|
+
args: morph(g.parameters, (_, name) => [name, keywords.unknown])
|
|
282
284
|
}
|
|
283
285
|
)
|
|
284
286
|
return g
|
|
@@ -291,7 +293,7 @@ export const generic = (
|
|
|
291
293
|
) => {
|
|
292
294
|
return Object.assign(
|
|
293
295
|
(...args: unknown[]) => {
|
|
294
|
-
const argNodes =
|
|
296
|
+
const argNodes = morph(parameters, (i, param) => [
|
|
295
297
|
param,
|
|
296
298
|
parseTypeRoot(args[i], scope)
|
|
297
299
|
])
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { MorphAst, Out } from "@arktype/schema"
|
|
2
|
-
import {
|
|
3
|
-
Hkt,
|
|
4
|
-
type List,
|
|
5
|
-
type conform,
|
|
6
|
-
type evaluate,
|
|
7
|
-
type intersectArrays,
|
|
8
|
-
type isAny
|
|
9
|
-
} from "@arktype/util"
|
|
10
|
-
|
|
11
|
-
export type inferIntersection<l, r> = [l] extends [never]
|
|
12
|
-
? never
|
|
13
|
-
: [r] extends [never]
|
|
14
|
-
? never
|
|
15
|
-
: [l & r] extends [never]
|
|
16
|
-
? never
|
|
17
|
-
: isAny<l | r> extends true
|
|
18
|
-
? any
|
|
19
|
-
: l extends MorphAst<infer lIn, infer lOut>
|
|
20
|
-
? r extends MorphAst
|
|
21
|
-
? never
|
|
22
|
-
: (In: evaluate<lIn & r>) => Out<lOut>
|
|
23
|
-
: r extends MorphAst<infer rIn, infer rOut>
|
|
24
|
-
? (In: evaluate<rIn & l>) => Out<rOut>
|
|
25
|
-
: [l, r] extends [object, object]
|
|
26
|
-
? intersectObjects<l, r> extends infer result
|
|
27
|
-
? result
|
|
28
|
-
: never
|
|
29
|
-
: l & r
|
|
30
|
-
|
|
31
|
-
declare class MorphableIntersection extends Hkt.Kind {
|
|
32
|
-
f: (
|
|
33
|
-
In: conform<this[Hkt.key], [l: unknown, r: unknown]>
|
|
34
|
-
) => inferIntersection<(typeof In)[0], (typeof In)[1]>
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
type intersectObjects<l, r> = [l, r] extends [
|
|
38
|
-
infer lList extends List,
|
|
39
|
-
infer rList extends List
|
|
40
|
-
]
|
|
41
|
-
? intersectArrays<lList, rList, MorphableIntersection>
|
|
42
|
-
: evaluate<
|
|
43
|
-
{
|
|
44
|
-
[k in keyof l]: k extends keyof r ? inferIntersection<l[k], r[k]> : l[k]
|
|
45
|
-
} & Omit<r, keyof l>
|
|
46
|
-
>
|