arktype 1.1.0-dev-2 → 2.0.0-beta.0
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/LICENSE +7 -0
- package/out/ark.d.ts +24 -0
- package/out/ark.js +8 -0
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +54 -0
- package/out/generic.js +39 -0
- package/out/index.d.ts +6 -0
- package/out/index.js +5 -0
- package/out/match.d.ts +71 -0
- package/out/match.js +50 -0
- package/out/module.d.ts +11 -0
- package/out/module.js +2 -0
- package/{dist → out}/parser/definition.d.ts +16 -16
- package/{dist → out}/parser/definition.js +12 -12
- package/out/parser/objectLiteral.d.ts +79 -0
- package/out/parser/objectLiteral.js +100 -0
- package/out/parser/semantic/bounds.d.ts +15 -0
- package/{dist → out}/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/default.d.ts +8 -0
- package/out/parser/semantic/default.js +1 -0
- package/out/parser/semantic/divisor.d.ts +7 -0
- package/out/parser/semantic/divisor.js +1 -0
- package/out/parser/semantic/infer.d.ts +25 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/utils.d.ts +4 -0
- package/out/parser/semantic/utils.js +1 -0
- package/out/parser/semantic/validate.d.ts +30 -0
- package/out/parser/semantic/validate.js +2 -0
- package/out/parser/string/reduce/dynamic.d.ts +43 -0
- package/{dist → out}/parser/string/reduce/dynamic.js +50 -33
- package/out/parser/string/reduce/shared.d.ts +42 -0
- package/{dist → out}/parser/string/reduce/shared.js +22 -2
- package/{dist → out}/parser/string/reduce/static.d.ts +29 -27
- package/out/parser/string/reduce/static.js +1 -0
- package/out/parser/string/shift/operand/date.d.ts +9 -0
- package/{dist → out}/parser/string/shift/operand/date.js +5 -8
- package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +5 -16
- package/{dist → out}/parser/string/shift/operand/enclosed.js +15 -15
- package/out/parser/string/shift/operand/genericArgs.d.ts +28 -0
- package/out/parser/string/shift/operand/genericArgs.js +16 -0
- package/out/parser/string/shift/operand/operand.d.ts +9 -0
- package/out/parser/string/shift/operand/operand.js +12 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts +30 -0
- package/out/parser/string/shift/operand/unenclosed.js +53 -0
- package/out/parser/string/shift/operator/bounds.d.ts +27 -0
- package/out/parser/string/shift/operator/bounds.js +94 -0
- package/out/parser/string/shift/operator/default.d.ts +24 -0
- package/out/parser/string/shift/operator/default.js +23 -0
- package/{dist → out}/parser/string/shift/operator/divisor.d.ts +2 -4
- package/{dist → out}/parser/string/shift/operator/divisor.js +2 -4
- package/{dist → out}/parser/string/shift/operator/operator.d.ts +4 -4
- package/out/parser/string/shift/operator/operator.js +22 -0
- package/{dist → out}/parser/string/shift/scanner.d.ts +13 -17
- package/{dist → out}/parser/string/shift/scanner.js +27 -24
- package/out/parser/string/string.d.ts +28 -0
- package/out/parser/string/string.js +24 -0
- package/out/parser/tuple.d.ts +130 -0
- package/out/parser/tuple.js +157 -0
- package/out/scope.d.ts +84 -0
- package/out/scope.js +131 -0
- package/out/type.d.ts +79 -0
- package/out/type.js +37 -0
- package/package.json +37 -41
- package/dist/main.d.ts +0 -11
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -8
- package/dist/main.js.map +0 -1
- package/dist/match.d.ts +0 -46
- package/dist/match.d.ts.map +0 -1
- package/dist/match.js +0 -23
- package/dist/match.js.map +0 -1
- package/dist/parser/definition.d.ts.map +0 -1
- package/dist/parser/definition.js.map +0 -1
- package/dist/parser/generic.d.ts +0 -44
- package/dist/parser/generic.d.ts.map +0 -1
- package/dist/parser/generic.js +0 -46
- package/dist/parser/generic.js.map +0 -1
- package/dist/parser/objectLiteral.d.ts +0 -24
- package/dist/parser/objectLiteral.d.ts.map +0 -1
- package/dist/parser/objectLiteral.js +0 -38
- package/dist/parser/objectLiteral.js.map +0 -1
- package/dist/parser/semantic/bounds.d.ts +0 -13
- package/dist/parser/semantic/bounds.d.ts.map +0 -1
- package/dist/parser/semantic/bounds.js.map +0 -1
- package/dist/parser/semantic/divisor.d.ts +0 -9
- package/dist/parser/semantic/divisor.d.ts.map +0 -1
- package/dist/parser/semantic/divisor.js +0 -2
- package/dist/parser/semantic/divisor.js.map +0 -1
- package/dist/parser/semantic/intersections.d.ts +0 -15
- package/dist/parser/semantic/intersections.d.ts.map +0 -1
- package/dist/parser/semantic/intersections.js +0 -2
- package/dist/parser/semantic/intersections.js.map +0 -1
- package/dist/parser/semantic/semantic.d.ts +0 -22
- package/dist/parser/semantic/semantic.d.ts.map +0 -1
- package/dist/parser/semantic/semantic.js +0 -2
- package/dist/parser/semantic/semantic.js.map +0 -1
- package/dist/parser/semantic/utils.d.ts +0 -8
- package/dist/parser/semantic/utils.d.ts.map +0 -1
- package/dist/parser/semantic/utils.js +0 -2
- package/dist/parser/semantic/utils.js.map +0 -1
- package/dist/parser/semantic/validate.d.ts +0 -24
- package/dist/parser/semantic/validate.d.ts.map +0 -1
- package/dist/parser/semantic/validate.js +0 -2
- package/dist/parser/semantic/validate.js.map +0 -1
- package/dist/parser/shared.d.ts +0 -48
- package/dist/parser/shared.d.ts.map +0 -1
- package/dist/parser/shared.js +0 -41
- package/dist/parser/shared.js.map +0 -1
- package/dist/parser/string/reduce/dynamic.d.ts +0 -40
- package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/dist/parser/string/reduce/dynamic.js.map +0 -1
- package/dist/parser/string/reduce/shared.d.ts +0 -29
- package/dist/parser/string/reduce/shared.d.ts.map +0 -1
- package/dist/parser/string/reduce/shared.js.map +0 -1
- package/dist/parser/string/reduce/static.d.ts.map +0 -1
- package/dist/parser/string/reduce/static.js +0 -2
- package/dist/parser/string/reduce/static.js.map +0 -1
- package/dist/parser/string/shift/operand/date.d.ts +0 -11
- package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/date.js.map +0 -1
- package/dist/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
- package/dist/parser/string/shift/operand/operand.d.ts +0 -9
- package/dist/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/operand.js +0 -17
- package/dist/parser/string/shift/operand/operand.js.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.d.ts +0 -36
- package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.js +0 -68
- package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/dist/parser/string/shift/operator/bounds.d.ts +0 -55
- package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/dist/parser/string/shift/operator/bounds.js +0 -95
- package/dist/parser/string/shift/operator/bounds.js.map +0 -1
- package/dist/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/dist/parser/string/shift/operator/divisor.js.map +0 -1
- package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/dist/parser/string/shift/operator/operator.js +0 -29
- package/dist/parser/string/shift/operator/operator.js.map +0 -1
- package/dist/parser/string/shift/scanner.d.ts.map +0 -1
- package/dist/parser/string/shift/scanner.js.map +0 -1
- package/dist/parser/string/string.d.ts +0 -26
- package/dist/parser/string/string.d.ts.map +0 -1
- package/dist/parser/string/string.js +0 -31
- package/dist/parser/string/string.js.map +0 -1
- package/dist/parser/tuple.d.ts +0 -73
- package/dist/parser/tuple.d.ts.map +0 -1
- package/dist/parser/tuple.js +0 -134
- package/dist/parser/tuple.js.map +0 -1
- package/dist/scope.d.ts +0 -159
- package/dist/scope.d.ts.map +0 -1
- package/dist/scope.js +0 -248
- package/dist/scope.js.map +0 -1
- package/dist/scopes/ark.d.ts +0 -38
- package/dist/scopes/ark.d.ts.map +0 -1
- package/dist/scopes/ark.js +0 -24
- package/dist/scopes/ark.js.map +0 -1
- package/dist/scopes/jsObjects.d.ts +0 -19
- package/dist/scopes/jsObjects.d.ts.map +0 -1
- package/dist/scopes/jsObjects.js +0 -15
- package/dist/scopes/jsObjects.js.map +0 -1
- package/dist/scopes/parsing.d.ts +0 -18
- package/dist/scopes/parsing.d.ts.map +0 -1
- package/dist/scopes/parsing.js +0 -61
- package/dist/scopes/parsing.js.map +0 -1
- package/dist/scopes/tsGenerics.d.ts +0 -17
- package/dist/scopes/tsGenerics.d.ts.map +0 -1
- package/dist/scopes/tsGenerics.js +0 -13
- package/dist/scopes/tsGenerics.js.map +0 -1
- package/dist/scopes/tsKeywords.d.ts +0 -24
- package/dist/scopes/tsKeywords.d.ts.map +0 -1
- package/dist/scopes/tsKeywords.js +0 -20
- package/dist/scopes/tsKeywords.js.map +0 -1
- package/dist/scopes/utils/creditCard.d.ts +0 -3
- package/dist/scopes/utils/creditCard.d.ts.map +0 -1
- package/dist/scopes/utils/creditCard.js +0 -36
- package/dist/scopes/utils/creditCard.js.map +0 -1
- package/dist/scopes/utils/date.d.ts +0 -24
- package/dist/scopes/utils/date.d.ts.map +0 -1
- package/dist/scopes/utils/date.js +0 -53
- package/dist/scopes/utils/date.js.map +0 -1
- package/dist/scopes/validation.d.ts +0 -20
- package/dist/scopes/validation.d.ts.map +0 -1
- package/dist/scopes/validation.js +0 -57
- package/dist/scopes/validation.js.map +0 -1
- package/dist/type.d.ts +0 -101
- package/dist/type.d.ts.map +0 -1
- package/dist/type.js +0 -138
- package/dist/type.js.map +0 -1
- package/main.ts +0 -20
- package/match.ts +0 -137
- package/parser/definition.ts +0 -156
- package/parser/generic.ts +0 -208
- package/parser/objectLiteral.ts +0 -120
- package/parser/semantic/bounds.ts +0 -63
- package/parser/semantic/divisor.ts +0 -13
- package/parser/semantic/intersections.ts +0 -47
- package/parser/semantic/semantic.ts +0 -107
- package/parser/semantic/utils.ts +0 -26
- package/parser/semantic/validate.ts +0 -105
- package/parser/shared.ts +0 -141
- package/parser/string/reduce/dynamic.ts +0 -181
- package/parser/string/reduce/shared.ts +0 -73
- package/parser/string/reduce/static.ts +0 -300
- package/parser/string/shift/operand/date.ts +0 -57
- package/parser/string/shift/operand/enclosed.ts +0 -119
- package/parser/string/shift/operand/operand.ts +0 -48
- package/parser/string/shift/operand/unenclosed.ts +0 -282
- package/parser/string/shift/operator/bounds.ts +0 -229
- package/parser/string/shift/operator/divisor.ts +0 -36
- package/parser/string/shift/operator/operator.ts +0 -82
- package/parser/string/shift/scanner.ts +0 -209
- package/parser/string/string.ts +0 -93
- package/parser/tuple.ts +0 -475
- package/scope.ts +0 -573
- package/scopes/ark.ts +0 -64
- package/scopes/jsObjects.ts +0 -29
- package/scopes/parsing.ts +0 -81
- package/scopes/tsGenerics.ts +0 -28
- package/scopes/tsKeywords.ts +0 -39
- package/scopes/utils/creditCard.ts +0 -37
- package/scopes/utils/date.ts +0 -108
- package/scopes/validation.ts +0 -81
- package/type.ts +0 -403
package/type.ts
DELETED
|
@@ -1,403 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
In,
|
|
3
|
-
TraversalState,
|
|
4
|
-
arkKind,
|
|
5
|
-
builtins,
|
|
6
|
-
inferred,
|
|
7
|
-
registry,
|
|
8
|
-
type BaseAttributes,
|
|
9
|
-
type CheckResult,
|
|
10
|
-
type Morph,
|
|
11
|
-
type Out,
|
|
12
|
-
type Predicate,
|
|
13
|
-
type Root,
|
|
14
|
-
type UnknownNode,
|
|
15
|
-
type inferMorphOut,
|
|
16
|
-
type inferNarrow
|
|
17
|
-
} from "@arktype/schema"
|
|
18
|
-
import {
|
|
19
|
-
CompiledFunction,
|
|
20
|
-
transform,
|
|
21
|
-
type BuiltinObjectKind,
|
|
22
|
-
type BuiltinObjects,
|
|
23
|
-
type Constructor,
|
|
24
|
-
type Json,
|
|
25
|
-
type Primitive,
|
|
26
|
-
type conform
|
|
27
|
-
} from "@arktype/util"
|
|
28
|
-
import type {
|
|
29
|
-
inferDefinition,
|
|
30
|
-
validateDeclared,
|
|
31
|
-
validateDefinition
|
|
32
|
-
} from "./parser/definition.js"
|
|
33
|
-
import {
|
|
34
|
-
parseGenericParams,
|
|
35
|
-
type GenericParamsParseError
|
|
36
|
-
} from "./parser/generic.js"
|
|
37
|
-
import type { inferIntersection } from "./parser/semantic/intersections.js"
|
|
38
|
-
import type {
|
|
39
|
-
IndexOneOperator,
|
|
40
|
-
IndexZeroOperator,
|
|
41
|
-
MorphAst,
|
|
42
|
-
TupleInfixOperator
|
|
43
|
-
} from "./parser/tuple.js"
|
|
44
|
-
import { bindThis, type Module, type Scope } from "./scope.js"
|
|
45
|
-
|
|
46
|
-
export type TypeParser<$> = {
|
|
47
|
-
// Parse and check the definition, returning either the original input for a
|
|
48
|
-
// valid definition or a string representing an error message.
|
|
49
|
-
<const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>
|
|
50
|
-
|
|
51
|
-
// Spread version of a tuple expression
|
|
52
|
-
<const zero, const one, const rest extends readonly unknown[]>(
|
|
53
|
-
_0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>,
|
|
54
|
-
_1: zero extends "keyof"
|
|
55
|
-
? validateTypeRoot<one, $>
|
|
56
|
-
: zero extends "instanceof"
|
|
57
|
-
? conform<one, Constructor>
|
|
58
|
-
: zero extends "==="
|
|
59
|
-
? conform<one, unknown>
|
|
60
|
-
: conform<one, IndexOneOperator>,
|
|
61
|
-
..._2: zero extends "==="
|
|
62
|
-
? rest
|
|
63
|
-
: zero extends "instanceof"
|
|
64
|
-
? conform<rest, readonly Constructor[]>
|
|
65
|
-
: one extends TupleInfixOperator
|
|
66
|
-
? one extends ":"
|
|
67
|
-
? [Predicate<extractIn<inferTypeRoot<zero, $>>>]
|
|
68
|
-
: one extends "=>"
|
|
69
|
-
? // TODO: centralize
|
|
70
|
-
[Morph<extractOut<inferTypeRoot<zero, $>>, unknown>]
|
|
71
|
-
: one extends "@"
|
|
72
|
-
? [string | BaseAttributes]
|
|
73
|
-
: [validateTypeRoot<rest[0], $>]
|
|
74
|
-
: []
|
|
75
|
-
): Type<inferTypeRoot<[zero, one, ...rest], $>, $>
|
|
76
|
-
|
|
77
|
-
<params extends string, const def>(
|
|
78
|
-
params: `<${validateParameterString<params>}>`,
|
|
79
|
-
def: validateDefinition<
|
|
80
|
-
def,
|
|
81
|
-
$,
|
|
82
|
-
{
|
|
83
|
-
[param in parseGenericParams<params>[number]]: unknown
|
|
84
|
-
}
|
|
85
|
-
>
|
|
86
|
-
): Generic<parseGenericParams<params>, def, $>
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export type DeclarationParser<$> = <preinferred>() => {
|
|
90
|
-
// for some reason, making this a const parameter breaks preinferred validation
|
|
91
|
-
type: <def>(
|
|
92
|
-
def: validateDeclared<preinferred, def, $, bindThis<def>>
|
|
93
|
-
) => Type<preinferred, $>
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export const createTypeParser = <$>(scope: Scope): TypeParser<$> => {
|
|
97
|
-
const parser = (...args: unknown[]): Type | Generic => {
|
|
98
|
-
if (args.length === 1) {
|
|
99
|
-
// treat as a simple definition
|
|
100
|
-
return new Type(args[0], scope)
|
|
101
|
-
}
|
|
102
|
-
if (
|
|
103
|
-
args.length === 2 &&
|
|
104
|
-
typeof args[0] === "string" &&
|
|
105
|
-
args[0][0] === "<" &&
|
|
106
|
-
args[0].at(-1) === ">"
|
|
107
|
-
) {
|
|
108
|
-
// if there are exactly two args, the first of which looks like <${string}>,
|
|
109
|
-
// treat as a generic
|
|
110
|
-
const params = parseGenericParams(args[0].slice(1, -1))
|
|
111
|
-
const def = args[1]
|
|
112
|
-
return validateUninstantiatedGeneric(generic(params, def, scope) as never)
|
|
113
|
-
}
|
|
114
|
-
// otherwise, treat as a tuple expression. technically, this also allows
|
|
115
|
-
// non-expression tuple definitions to be parsed, but it's not a supported
|
|
116
|
-
// part of the API as specified by the associated types
|
|
117
|
-
return new Type(args, scope)
|
|
118
|
-
}
|
|
119
|
-
return parser as never
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export type ArkKinds = {
|
|
123
|
-
node: UnknownNode
|
|
124
|
-
generic: Generic
|
|
125
|
-
module: Module
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export const addArkKind = <kind extends ArkKind>(
|
|
129
|
-
value: Omit<ArkKinds[kind], arkKind> & { [arkKind]?: kind },
|
|
130
|
-
kind: kind
|
|
131
|
-
): ArkKinds[kind] =>
|
|
132
|
-
Object.defineProperty(value, arkKind, {
|
|
133
|
-
value: kind,
|
|
134
|
-
enumerable: false
|
|
135
|
-
}) as never
|
|
136
|
-
|
|
137
|
-
export type arkKind = typeof arkKind
|
|
138
|
-
|
|
139
|
-
export type ArkKind = keyof ArkKinds
|
|
140
|
-
|
|
141
|
-
export const hasArkKind = <kind extends ArkKind>(
|
|
142
|
-
value: unknown,
|
|
143
|
-
kind: kind
|
|
144
|
-
): value is ArkKinds[kind] => (value as any)?.[arkKind] === kind
|
|
145
|
-
|
|
146
|
-
export type DefinitionParser<$> = <def>(
|
|
147
|
-
def: validateDefinition<def, $, bindThis<def>>
|
|
148
|
-
) => def
|
|
149
|
-
|
|
150
|
-
export type KeyCheckKind = "distilled" | "strict" | "loose"
|
|
151
|
-
|
|
152
|
-
export type TypeConfig = {
|
|
153
|
-
keys?: KeyCheckKind
|
|
154
|
-
mustBe?: string
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
registry().register(TraversalState, "state")
|
|
158
|
-
|
|
159
|
-
export class Type<t = unknown, $ = any> extends CompiledFunction<
|
|
160
|
-
(data: unknown) => CheckResult<extractOut<t>>
|
|
161
|
-
> {
|
|
162
|
-
declare [inferred]: t
|
|
163
|
-
declare inferMorph: t
|
|
164
|
-
declare infer: extractOut<t>
|
|
165
|
-
declare inferIn: extractIn<t>
|
|
166
|
-
|
|
167
|
-
config: TypeConfig
|
|
168
|
-
root: Root<t>
|
|
169
|
-
condition = ""
|
|
170
|
-
alias: string
|
|
171
|
-
allows: this["root"]["allows"]
|
|
172
|
-
json: Json
|
|
173
|
-
|
|
174
|
-
constructor(
|
|
175
|
-
public definition: unknown,
|
|
176
|
-
public scope: Scope
|
|
177
|
-
) {
|
|
178
|
-
const root = parseTypeRoot(definition, scope) as Root<t>
|
|
179
|
-
super(In, `return true ? { data: ${In} } : { problems: [] } `)
|
|
180
|
-
// const state = new ${registry().reference("state")}();
|
|
181
|
-
// const morphs = [];
|
|
182
|
-
// for(let i = 0; i < morphs.length; i++) {
|
|
183
|
-
// morphs[i]()
|
|
184
|
-
// }
|
|
185
|
-
// return state.finalize(${In});
|
|
186
|
-
this.root = root
|
|
187
|
-
this.allows = root.allows
|
|
188
|
-
this.config = scope.config
|
|
189
|
-
this.json = this.root.json
|
|
190
|
-
this.alias = this.root.alias
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
configure(config: TypeConfig) {
|
|
194
|
-
this.config = { ...this.config, ...config }
|
|
195
|
-
return this
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// TODO: should return out
|
|
199
|
-
from(literal: this["infer"]) {
|
|
200
|
-
return literal
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
fromIn(literal: this["inferIn"]) {
|
|
204
|
-
return literal
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// TODO: Morph intersections, ordering
|
|
208
|
-
and<def>(
|
|
209
|
-
def: validateTypeRoot<def, $>
|
|
210
|
-
): Type<inferIntersection<t, inferTypeRoot<def, $>>, $> {
|
|
211
|
-
return new Type(
|
|
212
|
-
this.root.and(parseTypeRoot(def, this.scope)),
|
|
213
|
-
this.scope
|
|
214
|
-
) as never
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $> {
|
|
218
|
-
return new Type(
|
|
219
|
-
this.root.or(parseTypeRoot(def, this.scope)),
|
|
220
|
-
this.scope
|
|
221
|
-
) as never
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
morph<morph extends Morph<extractOut<t>>>(
|
|
225
|
-
morph: morph
|
|
226
|
-
): Type<(In: this["inferIn"]) => Out<inferMorphOut<ReturnType<morph>>>, $>
|
|
227
|
-
morph<morph extends Morph<extractOut<t>>, def>(
|
|
228
|
-
morph: morph,
|
|
229
|
-
outValidator: validateTypeRoot<def, $>
|
|
230
|
-
): Type<
|
|
231
|
-
(In: this["inferIn"]) => Out<
|
|
232
|
-
// TODO: validate overlapping
|
|
233
|
-
// inferMorphOut<ReturnType<morph>> &
|
|
234
|
-
extractOut<inferTypeRoot<def, $>>
|
|
235
|
-
>,
|
|
236
|
-
$
|
|
237
|
-
>
|
|
238
|
-
morph(morph: Morph, outValidator?: unknown) {
|
|
239
|
-
// TODO: tuple expression for out validator
|
|
240
|
-
outValidator
|
|
241
|
-
return this as never
|
|
242
|
-
// return new Type(
|
|
243
|
-
// this.root.constrain("morph", morph),
|
|
244
|
-
// this.scope
|
|
245
|
-
// ) as never
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// TODO: based on below, should maybe narrow morph output if used after
|
|
249
|
-
narrow<def extends Predicate<extractOut<t>>>(
|
|
250
|
-
def: def
|
|
251
|
-
): Type<
|
|
252
|
-
includesMorphs<t> extends true
|
|
253
|
-
? (In: this["inferIn"]) => Out<inferNarrow<this["infer"], def>>
|
|
254
|
-
: inferNarrow<this["infer"], def>,
|
|
255
|
-
$
|
|
256
|
-
> {
|
|
257
|
-
return new Type(this.root.constrain("predicate", def), this.scope) as never
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
array(): Type<t[], $> {
|
|
261
|
-
return new Type(this.root.array(), this.scope) as never
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
keyof(): Type<keyof this["inferIn"], $> {
|
|
265
|
-
return new Type(this.root.keyof(), this.scope) as never
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
assert(data: unknown): extractOut<t> {
|
|
269
|
-
const result = this.call(null, data)
|
|
270
|
-
return result.problems ? result.problems.throw() : result.data
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// TODO: parse these
|
|
274
|
-
equals<other>(other: Type<other>): this is Type<other, $> {
|
|
275
|
-
return this.root === (other.root as unknown)
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
extends<other>(other: Type<other>): this is Type<other, $> {
|
|
279
|
-
return this.root.extends(other.root)
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const parseTypeRoot = (def: unknown, scope: Scope, args?: BoundArgs) =>
|
|
284
|
-
scope.parseDefinition(def, { args: args ?? bindThis(), baseName: "type" })
|
|
285
|
-
|
|
286
|
-
export type validateTypeRoot<def, $> = validateDefinition<def, $, bindThis<def>>
|
|
287
|
-
|
|
288
|
-
export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>
|
|
289
|
-
|
|
290
|
-
type validateParameterString<params extends string> =
|
|
291
|
-
parseGenericParams<params> extends GenericParamsParseError<infer message>
|
|
292
|
-
? message
|
|
293
|
-
: params
|
|
294
|
-
|
|
295
|
-
export const validateUninstantiatedGeneric = (g: Generic) => {
|
|
296
|
-
// the unconstrained instantiation of the generic is not used for now
|
|
297
|
-
// other than to eagerly validate that the def does not contain any errors
|
|
298
|
-
g.scope.parseDefinition(
|
|
299
|
-
g.definition,
|
|
300
|
-
// once we support constraints on generic parameters, we'd use
|
|
301
|
-
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
302
|
-
{
|
|
303
|
-
baseName: "generic",
|
|
304
|
-
args: transform(g.parameters, ([, name]) => [name, builtins.unknown])
|
|
305
|
-
}
|
|
306
|
-
)
|
|
307
|
-
return g
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
export const generic = (
|
|
311
|
-
parameters: string[],
|
|
312
|
-
definition: unknown,
|
|
313
|
-
scope: Scope
|
|
314
|
-
) => {
|
|
315
|
-
return Object.assign(
|
|
316
|
-
(...args: unknown[]) => {
|
|
317
|
-
const argNodes = transform(parameters, ([i, param]) => [
|
|
318
|
-
param,
|
|
319
|
-
parseTypeRoot(args[i], scope)
|
|
320
|
-
])
|
|
321
|
-
const root = parseTypeRoot(definition, scope, argNodes)
|
|
322
|
-
return new Type(root, scope)
|
|
323
|
-
},
|
|
324
|
-
{
|
|
325
|
-
[arkKind]: "generic",
|
|
326
|
-
parameters,
|
|
327
|
-
definition,
|
|
328
|
-
scope
|
|
329
|
-
// $ is only needed at compile-time
|
|
330
|
-
} satisfies Omit<GenericProps, "$">
|
|
331
|
-
) as unknown as Generic
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// Comparing to Generic directly doesn't work well, so we compare to only its props
|
|
335
|
-
export type GenericProps<
|
|
336
|
-
params extends string[] = string[],
|
|
337
|
-
def = unknown,
|
|
338
|
-
$ = any
|
|
339
|
-
> = {
|
|
340
|
-
[arkKind]: "generic"
|
|
341
|
-
$: $
|
|
342
|
-
parameters: params
|
|
343
|
-
definition: def
|
|
344
|
-
scope: Scope
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
export type BoundArgs = Record<string, Root>
|
|
348
|
-
|
|
349
|
-
// TODO: Fix external reference (i.e. if this is attached to a scope, then args are defined using it)
|
|
350
|
-
export type Generic<
|
|
351
|
-
params extends string[] = string[],
|
|
352
|
-
def = unknown,
|
|
353
|
-
$ = any
|
|
354
|
-
> = {
|
|
355
|
-
<args>(
|
|
356
|
-
...args: conform<
|
|
357
|
-
args,
|
|
358
|
-
{
|
|
359
|
-
[i in keyof params]: validateTypeRoot<args[i & keyof args], $>
|
|
360
|
-
}
|
|
361
|
-
>
|
|
362
|
-
): Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>
|
|
363
|
-
} & GenericProps<params, def, $>
|
|
364
|
-
|
|
365
|
-
type bindGenericInstantiation<params extends string[], $, args> = {
|
|
366
|
-
[i in keyof params & `${number}` as params[i]]: inferTypeRoot<
|
|
367
|
-
args[i & keyof args],
|
|
368
|
-
$
|
|
369
|
-
>
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
export type extractIn<t> = includesMorphs<t> extends true
|
|
373
|
-
? extractMorphs<t, "in">
|
|
374
|
-
: t
|
|
375
|
-
|
|
376
|
-
export type extractOut<t> = includesMorphs<t> extends true
|
|
377
|
-
? extractMorphs<t, "out">
|
|
378
|
-
: t
|
|
379
|
-
|
|
380
|
-
type includesMorphs<t> = [
|
|
381
|
-
t,
|
|
382
|
-
extractMorphs<t, "in">,
|
|
383
|
-
t,
|
|
384
|
-
extractMorphs<t, "out">
|
|
385
|
-
] extends [extractMorphs<t, "in">, t, extractMorphs<t, "out">, t]
|
|
386
|
-
? false
|
|
387
|
-
: true
|
|
388
|
-
|
|
389
|
-
type extractMorphs<t, io extends "in" | "out"> = t extends MorphAst<
|
|
390
|
-
infer i,
|
|
391
|
-
infer o
|
|
392
|
-
>
|
|
393
|
-
? io extends "in"
|
|
394
|
-
? i
|
|
395
|
-
: o
|
|
396
|
-
: t extends TerminallyInferredObjectKind | Primitive
|
|
397
|
-
? t
|
|
398
|
-
: { [k in keyof t]: extractMorphs<t[k], io> }
|
|
399
|
-
|
|
400
|
-
/** Objects we don't want to expand during inference like Date or Promise */
|
|
401
|
-
type TerminallyInferredObjectKind =
|
|
402
|
-
| ReturnType<ArkConfig["preserve"]>
|
|
403
|
-
| BuiltinObjects[Exclude<BuiltinObjectKind, "Object" | "Array">]
|