arktype 2.0.0-dev.0 → 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.
Files changed (219) hide show
  1. package/__tests__/array.test.ts +270 -151
  2. package/__tests__/badDefinitionType.test.ts +10 -3
  3. package/__tests__/basis.test.ts +6 -2
  4. package/__tests__/bounds.test.ts +282 -352
  5. package/__tests__/cast.test.ts +7 -2
  6. package/__tests__/completions.test.ts +8 -3
  7. package/__tests__/config.test.ts +95 -167
  8. package/__tests__/dateLiteral.test.ts +8 -4
  9. package/__tests__/declared.test.ts +7 -6
  10. package/__tests__/discrimination.test.ts +1 -1
  11. package/__tests__/divisor.test.ts +46 -22
  12. package/__tests__/enclosed.test.ts +39 -25
  13. package/__tests__/examples.test.ts +56 -56
  14. package/__tests__/expressions.test.ts +184 -112
  15. package/__tests__/generic.test.ts +307 -288
  16. package/__tests__/group.test.ts +67 -54
  17. package/__tests__/imports.test.ts +62 -49
  18. package/__tests__/instanceof.test.ts +25 -23
  19. package/__tests__/intersection.test.ts +52 -48
  20. package/__tests__/keyTraversal.test.ts +77 -73
  21. package/__tests__/keyof.test.ts +68 -52
  22. package/__tests__/keywords.test.ts +164 -138
  23. package/__tests__/literal.test.ts +60 -21
  24. package/__tests__/match.bench.ts +31 -0
  25. package/__tests__/match.test.ts +222 -22
  26. package/__tests__/narrow.test.ts +52 -54
  27. package/__tests__/objectLiteral.test.ts +347 -205
  28. package/__tests__/operator.bench.ts +4 -4
  29. package/__tests__/pipe.test.ts +501 -0
  30. package/__tests__/realWorld.test.ts +63 -0
  31. package/__tests__/regex.test.ts +17 -7
  32. package/__tests__/scope.bench.ts +1 -1
  33. package/__tests__/scope.test.ts +257 -23
  34. package/__tests__/serialization.test.ts +19 -0
  35. package/__tests__/string.test.ts +11 -7
  36. package/__tests__/submodule.test.ts +26 -39
  37. package/__tests__/this.test.ts +83 -57
  38. package/__tests__/thunk.test.ts +13 -7
  39. package/__tests__/traverse.test.ts +76 -54
  40. package/__tests__/tsGenerics.test.ts +12 -12
  41. package/__tests__/type.test.ts +36 -0
  42. package/__tests__/typeReference.test.ts +7 -7
  43. package/__tests__/unenclosed.test.ts +116 -100
  44. package/__tests__/union.test.ts +171 -104
  45. package/ark.ts +21 -65
  46. package/generic.ts +69 -0
  47. package/main.ts +6 -18
  48. package/match.ts +195 -93
  49. package/module.ts +15 -0
  50. package/out/ark.d.ts +15 -39
  51. package/out/ark.js +9 -18
  52. package/out/ark.js.map +1 -1
  53. package/out/generic.d.ts +21 -0
  54. package/out/generic.js +24 -0
  55. package/out/generic.js.map +1 -0
  56. package/out/main.d.ts +6 -7
  57. package/out/main.js +4 -3
  58. package/out/main.js.map +1 -1
  59. package/out/match.d.ts +57 -34
  60. package/out/match.js +51 -17
  61. package/out/match.js.map +1 -1
  62. package/out/module.d.ts +10 -0
  63. package/out/module.js +3 -0
  64. package/out/module.js.map +1 -0
  65. package/out/parser/definition.d.ts +11 -13
  66. package/out/parser/definition.js +10 -13
  67. package/out/parser/definition.js.map +1 -1
  68. package/out/parser/generic.d.ts +5 -7
  69. package/out/parser/generic.js +7 -11
  70. package/out/parser/generic.js.map +1 -1
  71. package/out/parser/objectLiteral.d.ts +45 -14
  72. package/out/parser/objectLiteral.js +61 -52
  73. package/out/parser/objectLiteral.js.map +1 -1
  74. package/out/parser/semantic/bounds.d.ts +10 -8
  75. package/out/parser/semantic/bounds.js +0 -1
  76. package/out/parser/semantic/bounds.js.map +1 -1
  77. package/out/parser/semantic/divisor.d.ts +5 -7
  78. package/out/parser/semantic/divisor.js +0 -1
  79. package/out/parser/semantic/infer.d.ts +24 -0
  80. package/out/parser/semantic/infer.js +1 -0
  81. package/out/parser/semantic/infer.js.map +1 -0
  82. package/out/parser/semantic/utils.d.ts +3 -7
  83. package/out/parser/semantic/utils.js +0 -1
  84. package/out/parser/semantic/validate.d.ts +15 -14
  85. package/out/parser/semantic/validate.js +1 -1
  86. package/out/parser/semantic/validate.js.map +1 -1
  87. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  88. package/out/parser/string/reduce/dynamic.js +31 -26
  89. package/out/parser/string/reduce/dynamic.js.map +1 -1
  90. package/out/parser/string/reduce/shared.d.ts +6 -20
  91. package/out/parser/string/reduce/shared.js +0 -1
  92. package/out/parser/string/reduce/shared.js.map +1 -1
  93. package/out/parser/string/reduce/static.d.ts +27 -24
  94. package/out/parser/string/reduce/static.js +0 -1
  95. package/out/parser/string/shift/operand/date.d.ts +3 -3
  96. package/out/parser/string/shift/operand/date.js +4 -7
  97. package/out/parser/string/shift/operand/date.js.map +1 -1
  98. package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
  99. package/out/parser/string/shift/operand/enclosed.js +12 -13
  100. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  101. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  102. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  103. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  104. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  105. package/out/parser/string/shift/operand/operand.js +8 -12
  106. package/out/parser/string/shift/operand/operand.js.map +1 -1
  107. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
  108. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  109. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  110. package/out/parser/string/shift/operator/bounds.d.ts +9 -13
  111. package/out/parser/string/shift/operator/bounds.js +43 -54
  112. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  113. package/out/parser/string/shift/operator/divisor.d.ts +2 -4
  114. package/out/parser/string/shift/operator/divisor.js +1 -3
  115. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  116. package/out/parser/string/shift/operator/operator.d.ts +2 -3
  117. package/out/parser/string/shift/operator/operator.js +12 -19
  118. package/out/parser/string/shift/operator/operator.js.map +1 -1
  119. package/out/parser/string/shift/scanner.d.ts +2 -2
  120. package/out/parser/string/shift/scanner.js +14 -16
  121. package/out/parser/string/shift/scanner.js.map +1 -1
  122. package/out/parser/string/string.d.ts +8 -6
  123. package/out/parser/string/string.js +1 -3
  124. package/out/parser/string/string.js.map +1 -1
  125. package/out/parser/tuple.d.ts +102 -43
  126. package/out/parser/tuple.js +119 -87
  127. package/out/parser/tuple.js.map +1 -1
  128. package/out/scope.d.ts +43 -120
  129. package/out/scope.js +39 -209
  130. package/out/scope.js.map +1 -1
  131. package/out/type.d.ts +43 -64
  132. package/out/type.js +30 -131
  133. package/out/type.js.map +1 -1
  134. package/package.json +7 -3
  135. package/parser/definition.ts +77 -93
  136. package/parser/generic.ts +38 -40
  137. package/parser/objectLiteral.ts +172 -136
  138. package/parser/semantic/bounds.ts +33 -32
  139. package/parser/semantic/divisor.ts +8 -10
  140. package/parser/semantic/infer.ts +141 -0
  141. package/parser/semantic/utils.ts +12 -23
  142. package/parser/semantic/validate.ts +81 -73
  143. package/parser/string/reduce/dynamic.ts +67 -55
  144. package/parser/string/reduce/shared.ts +1 -3
  145. package/parser/string/reduce/static.ts +67 -69
  146. package/parser/string/shift/operand/date.ts +15 -14
  147. package/parser/string/shift/operand/enclosed.ts +30 -25
  148. package/parser/string/shift/operand/genericArgs.ts +46 -45
  149. package/parser/string/shift/operand/operand.ts +32 -38
  150. package/parser/string/shift/operand/unenclosed.ts +107 -158
  151. package/parser/string/shift/operator/bounds.ts +118 -112
  152. package/parser/string/shift/operator/divisor.ts +14 -17
  153. package/parser/string/shift/operator/operator.ts +35 -46
  154. package/parser/string/shift/scanner.ts +51 -50
  155. package/parser/string/string.ts +30 -35
  156. package/parser/tuple.ts +375 -307
  157. package/scope.ts +170 -453
  158. package/type.ts +193 -274
  159. package/__tests__/astUtils.test.ts +0 -17
  160. package/__tests__/cyclic.test.ts +0 -144
  161. package/__tests__/jsdoc.test.ts +0 -64
  162. package/__tests__/morph.test.ts +0 -409
  163. package/__tests__/nodeDefinitions.test.ts +0 -190
  164. package/__tests__/typeMethods.test.ts +0 -69
  165. package/out/ark.d.ts.map +0 -1
  166. package/out/arkConfig.d.ts +0 -2
  167. package/out/arkConfig.d.ts.map +0 -1
  168. package/out/arkConfig.js +0 -10
  169. package/out/arkConfig.js.map +0 -1
  170. package/out/main.d.ts.map +0 -1
  171. package/out/match.d.ts.map +0 -1
  172. package/out/parser/definition.d.ts.map +0 -1
  173. package/out/parser/generic.d.ts.map +0 -1
  174. package/out/parser/objectLiteral.d.ts.map +0 -1
  175. package/out/parser/semantic/bounds.d.ts.map +0 -1
  176. package/out/parser/semantic/divisor.d.ts.map +0 -1
  177. package/out/parser/semantic/intersections.d.ts +0 -14
  178. package/out/parser/semantic/intersections.d.ts.map +0 -1
  179. package/out/parser/semantic/intersections.js +0 -2
  180. package/out/parser/semantic/intersections.js.map +0 -1
  181. package/out/parser/semantic/semantic.d.ts +0 -33
  182. package/out/parser/semantic/semantic.d.ts.map +0 -1
  183. package/out/parser/semantic/semantic.js +0 -2
  184. package/out/parser/semantic/semantic.js.map +0 -1
  185. package/out/parser/semantic/utils.d.ts.map +0 -1
  186. package/out/parser/semantic/validate.d.ts.map +0 -1
  187. package/out/parser/shared.d.ts +0 -54
  188. package/out/parser/shared.d.ts.map +0 -1
  189. package/out/parser/shared.js +0 -47
  190. package/out/parser/shared.js.map +0 -1
  191. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  192. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  193. package/out/parser/string/reduce/static.d.ts.map +0 -1
  194. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  195. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  196. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  197. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  198. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  199. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  200. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  201. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  202. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  203. package/out/parser/string/string.d.ts.map +0 -1
  204. package/out/parser/tuple.d.ts.map +0 -1
  205. package/out/scope.d.ts.map +0 -1
  206. package/out/test.d.ts +0 -2
  207. package/out/test.d.ts.map +0 -1
  208. package/out/test.js +0 -7
  209. package/out/test.js.map +0 -1
  210. package/out/tsconfig.build.tsbuildinfo +0 -1
  211. package/out/type.d.ts.map +0 -1
  212. package/out/util.d.ts +0 -19
  213. package/out/util.d.ts.map +0 -1
  214. package/out/util.js +0 -7
  215. package/out/util.js.map +0 -1
  216. package/parser/semantic/intersections.ts +0 -46
  217. package/parser/semantic/semantic.ts +0 -144
  218. package/parser/shared.ts +0 -152
  219. package/util.ts +0 -36
package/generic.ts ADDED
@@ -0,0 +1,69 @@
1
+ import {
2
+ type GenericNodeInstantiation,
3
+ type GenericProps,
4
+ type SchemaScope,
5
+ arkKind
6
+ } from "@arktype/schema"
7
+ import { Callable, type conform } from "@arktype/util"
8
+ import type { inferDefinition } from "./parser/definition.js"
9
+ import type {
10
+ GenericParamsParseError,
11
+ parseGenericParams
12
+ } from "./parser/generic.js"
13
+ import type { Type, inferTypeRoot, validateTypeRoot } from "./type.js"
14
+ export type validateParameterString<params extends string> =
15
+ parseGenericParams<params> extends GenericParamsParseError<infer message> ?
16
+ message
17
+ : params
18
+
19
+ export type GenericTypeInstantiation<
20
+ params extends string[] = string[],
21
+ def = any,
22
+ $ = any
23
+ > = <args>(
24
+ ...args: conform<
25
+ args,
26
+ {
27
+ [i in keyof params]: validateTypeRoot<args[i & keyof args], $>
28
+ }
29
+ >
30
+ ) => Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>
31
+
32
+ export type GenericInstantiation<
33
+ params extends string[] = string[],
34
+ def = any,
35
+ $ = any
36
+ > = GenericTypeInstantiation<params, def, $> &
37
+ GenericNodeInstantiation<params, def, $>
38
+
39
+ // TODO: Fix external reference (i.e. if this is attached to a scope, then args are defined using it)
40
+ type bindGenericInstantiation<params extends string[], $, args> = {
41
+ [i in keyof params & `${number}` as params[i]]: inferTypeRoot<
42
+ args[i & keyof args],
43
+ $
44
+ >
45
+ }
46
+
47
+ export class Generic<params extends string[] = string[], def = unknown, $ = any>
48
+ extends Callable<GenericInstantiation<params, def, $>>
49
+ implements GenericProps<params, def, $>
50
+ {
51
+ readonly [arkKind] = "generic"
52
+
53
+ constructor(
54
+ public params: params,
55
+ public def: def,
56
+ // TODO: should be Scope<$>, but breaks inference
57
+ public $: SchemaScope<$>
58
+ ) {
59
+ super((...args: unknown[]) => {
60
+ // const argNodes = flatMorph(params, (i, param: string) => [
61
+ // param,
62
+ // $.parseRoot(args[i])
63
+ // ])
64
+ // { args: argNodes }
65
+ args
66
+ return $.parseRoot(def) as never
67
+ })
68
+ }
69
+ }
package/main.ts CHANGED
@@ -1,18 +1,6 @@
1
- export {
2
- ArkErrors as ArkErrors,
3
- type ArkTypeError as ArkTypeError
4
- } from "@arktype/schema"
5
- export type { Out, is } from "@arktype/schema"
6
- export {
7
- ark,
8
- arktypes,
9
- declare,
10
- define,
11
- match,
12
- scope,
13
- type,
14
- type Ark
15
- } from "./ark.js"
16
- export type { Module, Scope } from "./scope.js"
17
- export { Type } from "./type.js"
18
- export type { inferTypeRoot, validateTypeRoot } from "./type.js"
1
+ export { ArkError, ArkErrors, ArkTypeError } from "@arktype/schema"
2
+ export type { Ark, ArkConfig, Out } from "@arktype/schema"
3
+ export { ambient, ark, declare, define, match, type } from "./ark.js"
4
+ export { Module } from "./module.js"
5
+ export { scope, type Scope } from "./scope.js"
6
+ export { Type, type inferTypeRoot, type validateTypeRoot } from "./type.js"
package/match.ts CHANGED
@@ -1,116 +1,218 @@
1
- import type { Morph } from "@arktype/schema"
1
+ import type { Narrowed, distillOut, inferIntersection } from "@arktype/schema"
2
2
  import type {
3
- Fn,
3
+ ErrorMessage,
4
4
  isDisjoint,
5
- replaceKey,
6
- returnOf,
7
- unionToTuple,
8
- valueOf
5
+ numericStringKeyOf,
6
+ override,
7
+ propValueOf,
8
+ unionToTuple
9
9
  } from "@arktype/util"
10
10
  import type { Scope } from "./scope.js"
11
- import { Type, type inferTypeRoot, type validateTypeRoot } from "./type.js"
11
+ import type { inferTypeRoot, validateTypeRoot } from "./type.js"
12
12
 
13
- type MatchContext = {
14
- inConstraint: unknown
15
- outConstraint: unknown
16
- thens: readonly Fn[]
13
+ type MatchParserContext = {
14
+ thens: readonly ((In: unknown) => unknown)[]
17
15
  $: unknown
16
+ exhaustiveOver: unknown
18
17
  }
19
18
 
20
- type validateCases<cases, ctx extends MatchContext> = {
21
- [k in keyof cases | keyof ctx["$"] | "default"]?: k extends "default"
22
- ? (In: ctx["inConstraint"]) => ctx["outConstraint"]
23
- : k extends validateTypeRoot<k, ctx["$"]>
24
- ? (
25
- In: ctx["inConstraint"] & inferTypeRoot<k, ctx["$"]>
26
- ) => ctx["outConstraint"]
27
- : validateTypeRoot<k, ctx["$"]>
19
+ export type MatchParser<$> = CaseMatchParser<{
20
+ thens: []
21
+ // "match()" is the same as "match.only<unknown>()"
22
+ exhaustiveOver: unknown
23
+ $: $
24
+ }> & {
25
+ (): ChainableMatchParser<{
26
+ thens: []
27
+ // "match()" is the same as "match.only<unknown>()"
28
+ exhaustiveOver: unknown
29
+ $: $
30
+ }>
31
+ only: <inputs>() => ChainableMatchParser<{
32
+ thens: []
33
+ exhaustiveOver: inputs
34
+ $: $
35
+ }>
36
+ }
37
+
38
+ type matcherInputs<ctx extends MatchParserContext> = Parameters<
39
+ ctx["thens"][number]
40
+ >[0]
41
+
42
+ type getHandledBranches<ctx extends MatchParserContext> = Exclude<
43
+ matcherInputs<ctx>,
44
+ // TODO: add other anon
45
+ Narrowed
46
+ >
47
+
48
+ type getUnhandledBranches<ctx extends MatchParserContext> = distillOut<
49
+ Exclude<ctx["exhaustiveOver"], getHandledBranches<ctx>>
50
+ >
51
+
52
+ type addBranches<
53
+ ctx extends MatchParserContext,
54
+ branches extends unknown[]
55
+ > = override<ctx, { thens: [...ctx["thens"], ...branches] }>
56
+
57
+ type validateWhenDefinition<def, ctx extends MatchParserContext> =
58
+ def extends validateTypeRoot<def, ctx["$"]> ?
59
+ inferMatchBranch<def, ctx> extends getHandledBranches<ctx> ?
60
+ ErrorMessage<"This branch is redundant and will never be reached">
61
+ : def
62
+ : validateTypeRoot<def, ctx["$"]>
63
+
64
+ // infer the types handled by a match branch, which is identical to `inferTypeRoot` while properly
65
+ // excluding cases that are already handled by other branches
66
+ type inferMatchBranch<def, ctx extends MatchParserContext> = distillOut<
67
+ inferIntersection<getUnhandledBranches<ctx>, inferTypeRoot<def, ctx["$"]>>
68
+ >
69
+
70
+ export type ChainableMatchParser<ctx extends MatchParserContext> = {
71
+ // chainable methods
72
+ when: <def, ret>(
73
+ when: validateWhenDefinition<def, ctx>,
74
+ then: (In: inferMatchBranch<def, ctx>) => ret
75
+ ) => ChainableMatchParser<
76
+ addBranches<ctx, [(In: inferMatchBranch<def, ctx>) => ret]>
77
+ >
78
+ cases: CaseMatchParser<ctx>
79
+
80
+ // finalizing methods
81
+ orThrow: () => finalizeMatchParser<
82
+ addBranches<ctx, [(In: getHandledBranches<ctx>) => never]>
83
+ >
84
+ default: MatchParserDefaultInvocation<ctx>
85
+ finalize: (
86
+ this: getUnhandledBranches<ctx> extends never ? ChainableMatchParser<ctx>
87
+ : ErrorMessage<"Cannot manually finalize a non-exhaustive matcher: consider adding a `.default` case, using one of the `.orX` methods, or using `match.only<T>`">
88
+ ) => finalizeMatchParser<ctx>
28
89
  }
29
90
 
30
- type errorCases<cases, ctx extends MatchContext> = {
31
- [k in keyof cases]?: k extends "default"
32
- ? (In: ctx["inConstraint"]) => ctx["outConstraint"]
33
- : k extends validateTypeRoot<k, ctx["$"]>
34
- ? (
35
- In: ctx["inConstraint"] & inferTypeRoot<k, ctx["$"]>
36
- ) => ctx["outConstraint"]
37
- : validateTypeRoot<k, ctx["$"]>
91
+ type MatchParserDefaultInvocation<ctx extends MatchParserContext> = {
92
+ <f extends (In: getUnhandledBranches<ctx>) => unknown>(
93
+ f: f
94
+ ): finalizeWithDefault<ctx, ReturnType<f>>
95
+ <const value>(value: value): finalizeWithDefault<ctx, value>
96
+ }
97
+
98
+ type validateCases<cases, ctx extends MatchParserContext> = {
99
+ [def in keyof cases | keyof ctx["$"] | "default"]?: def extends "default" ?
100
+ (In: getUnhandledBranches<ctx>) => unknown
101
+ : def extends validateWhenDefinition<def, ctx> ?
102
+ (In: inferMatchBranch<def, ctx>) => unknown
103
+ : validateWhenDefinition<def, ctx>
104
+ }
105
+
106
+ type errorCases<cases, ctx extends MatchParserContext> = {
107
+ [def in keyof cases]?: def extends "default" ?
108
+ (In: getUnhandledBranches<ctx>) => unknown
109
+ : def extends validateWhenDefinition<def, ctx> ?
110
+ (In: inferMatchBranch<def, ctx>) => unknown
111
+ : validateWhenDefinition<def, ctx>
38
112
  } & {
39
113
  [k in Exclude<keyof ctx["$"], keyof cases>]?: (
40
- In: ctx["inConstraint"] & ctx["$"][k]
41
- ) => ctx["outConstraint"]
114
+ In: distillOut<inferIntersection<getUnhandledBranches<ctx>, ctx["$"][k]>>
115
+ ) => unknown
42
116
  } & {
43
- default?: (In: ctx["inConstraint"]) => ctx["outConstraint"]
117
+ default?: (In: getUnhandledBranches<ctx>) => unknown
44
118
  }
45
119
 
46
- export type CaseMatchParser<ctx extends MatchContext> = {
47
- <cases>(
48
- def: cases extends validateCases<cases, ctx>
49
- ? cases
50
- : errorCases<cases, ctx>
51
- ): ChainableMatchParser<
52
- replaceKey<ctx, "thens", [...ctx["thens"], ...unionToTuple<valueOf<cases>>]>
120
+ export type CaseMatchParser<ctx extends MatchParserContext> = <cases>(
121
+ def: cases extends validateCases<cases, ctx> ? cases : errorCases<cases, ctx>
122
+ ) => cases extends { default: (...args: never[]) => infer defaultReturn } ?
123
+ finalizeWithDefault<
124
+ addBranches<ctx, unionToTuple<cases[Exclude<keyof cases, "default">]>>,
125
+ defaultReturn
53
126
  >
54
- }
127
+ : ChainableMatchParser<addBranches<ctx, unionToTuple<propValueOf<cases>>>>
55
128
 
56
- export type MatchParser<$> = CaseMatchParser<{
57
- inConstraint: unknown
58
- outConstraint: unknown
59
- thens: []
60
- $: $
61
- }> & {
62
- <In = unknown, Out = unknown>(): ChainableMatchParser<{
63
- inConstraint: In
64
- outConstraint: Out
65
- thens: []
66
- $: $
67
- }>
68
- }
129
+ type finalizeWithDefault<
130
+ ctx extends MatchParserContext,
131
+ defaultReturn
132
+ > = finalizeMatchParser<
133
+ addBranches<ctx, [(_: getUnhandledBranches<ctx>) => defaultReturn]>
134
+ >
69
135
 
70
- export type WhenMatchParser<ctx extends MatchContext> = <
71
- def,
72
- then extends (
73
- In: ctx["inConstraint"] & inferTypeRoot<def, ctx["$"]>
74
- ) => ctx["outConstraint"]
75
- >(
76
- when: validateTypeRoot<def, ctx["$"]>,
77
- then: then
78
- ) => ChainableMatchParser<replaceKey<ctx, "thens", [...ctx["thens"], then]>>
136
+ type finalizeMatchParser<ctx extends MatchParserContext> = MatchInvocation<{
137
+ thens: ctx["thens"]
138
+ initialInputs: ctx["exhaustiveOver"]
139
+ }>
79
140
 
80
- export type MatchInvocation<ctx extends MatchContext> = <
81
- data extends ctx["inConstraint"]
141
+ type MatchInvocationContext = {
142
+ thens: readonly ((...args: never[]) => unknown)[]
143
+ initialInputs: unknown
144
+ }
145
+
146
+ export type MatchInvocation<ctx extends MatchInvocationContext> = <
147
+ data extends ctx["initialInputs"]
82
148
  >(
83
149
  data: data
84
150
  ) => {
85
- [i in Extract<keyof ctx["thens"], `${number}`>]: ctx["thens"][i] extends Fn<
86
- [infer In],
87
- infer Out
88
- >
89
- ? isDisjoint<data, In> extends true
90
- ? never
91
- : Out
92
- : returnOf<ctx["thens"][i]>
93
- }[Extract<keyof ctx["thens"], `${number}`>]
94
-
95
- export type ChainableMatchParser<ctx extends MatchContext> =
96
- MatchInvocation<ctx> & {
97
- cases: CaseMatchParser<ctx>
98
- when: WhenMatchParser<ctx>
99
- }
100
-
101
- export const createMatchParser = <$>(scope: Scope): MatchParser<$> => {
102
- const parser = (cases: Record<string, Morph>) => {
103
- const caseArray = Object.entries(cases).map(([def, morph]) => ({
104
- when: new Type(def, scope).allows,
105
- then: morph
106
- }))
107
- return (data: unknown) => {
108
- for (const c of caseArray) {
109
- if (c.when(data)) {
110
- return c.then(data, {} as never)
111
- }
112
- }
113
- }
114
- }
115
- return parser as never
151
+ [i in numericStringKeyOf<ctx["thens"]>]: isDisjoint<
152
+ data,
153
+ Parameters<ctx["thens"][i]>[0]
154
+ > extends true ?
155
+ never
156
+ : ReturnType<ctx["thens"][i]>
157
+ }[numericStringKeyOf<ctx["thens"]>]
158
+
159
+ export const createMatchParser = <$>($: Scope): MatchParser<$> => {
160
+ return (() => {}).bind($) as never
161
+ // const matchParser = (isRestricted: boolean) => {
162
+ // const handledCases: { when: RawSchema; then: Morph }[] = []
163
+ // let defaultCase: ((x: unknown) => unknown) | null = null
164
+
165
+ // const parser = {
166
+ // when: (when: unknown, then: Morph) => {
167
+ // handledCases.push({ when: $.parseRoot(when, {}), then })
168
+
169
+ // return parser
170
+ // },
171
+
172
+ // finalize: () => {
173
+ // // TODO: exhaustiveness checking
174
+ // const branches = handledCases.flatMap(({ when, then }) => {
175
+ // if (when.kind === "union") {
176
+ // return when.branches.map((branch) => ({
177
+ // from: branch,
178
+ // morph: then
179
+ // }))
180
+ // }
181
+ // if (when.kind === "morph") {
182
+ // return [{ from: when, morph: [when.morph, then] }]
183
+ // }
184
+ // return [{ from: when, morph: then }]
185
+ // })
186
+ // if (defaultCase) {
187
+ // branches.push({ from: keywordNodes.unknown, morph: defaultCase })
188
+ // }
189
+ // const matchers = $.node("union", {
190
+ // branches,
191
+ // ordered: true
192
+ // })
193
+ // return matchers.assert
194
+ // },
195
+
196
+ // orThrow: () => {
197
+ // // implicitly finalize, we don't need to do anything else because we throw either way
198
+ // return parser.finalize()
199
+ // },
200
+
201
+ // default: (x: unknown) => {
202
+ // if (x instanceof Function) {
203
+ // defaultCase = x as never
204
+ // } else {
205
+ // defaultCase = () => x
206
+ // }
207
+
208
+ // return parser.finalize()
209
+ // }
210
+ // }
211
+
212
+ // return parser
213
+ // }
214
+
215
+ // return Object.assign(() => matchParser(false), {
216
+ // only: () => matchParser(true)
217
+ // }) as never
116
218
  }
package/module.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { PreparsedNodeResolution, arkKind } from "@arktype/schema"
2
+ import { DynamicBase, type isAnyOrNever } from "@arktype/util"
3
+ import type { Type } from "./type.js"
4
+
5
+ type exportScope<$> = {
6
+ [k in keyof $]: $[k] extends PreparsedNodeResolution ?
7
+ isAnyOrNever<$[k]> extends true ?
8
+ Type<$[k], $>
9
+ : $[k]
10
+ : Type<$[k], $>
11
+ }
12
+
13
+ export class Module<$ = any> extends DynamicBase<exportScope<$>> {
14
+ declare readonly [arkKind]: "module"
15
+ }
package/out/ark.d.ts CHANGED
@@ -1,29 +1,9 @@
1
- import { JsObjects, Parsing, TsKeywords, Validation, type inferred } from "@arktype/schema";
1
+ import { type Ark, type ArkErrors, type inferred } from "@arktype/schema";
2
+ import type { Generic } from "./generic.js";
2
3
  import type { MatchParser } from "./match.js";
3
- import { Scope, type Module, type ScopeParser } from "./scope.js";
4
- import type { DeclarationParser, DefinitionParser, Generic, TypeParser } from "./type.js";
5
- /** Root scopes can be inferred automatically from node definitions, but
6
- * explicitly typing them can improve responsiveness */
7
- export type RootScope<exports> = Scope<{
8
- exports: exports;
9
- locals: {};
10
- ambient: {};
11
- }>;
12
- export type ArkResolutions = {
13
- exports: Ark;
14
- locals: {};
15
- ambient: Ark;
16
- };
17
- export type ParsingResolutions = {
18
- exports: Parsing.infer;
19
- locals: {};
20
- ambient: {};
21
- };
22
- export type TsGenericsResolutions<$ = Ark> = {
23
- exports: TsGenericsExports<$>;
24
- locals: {};
25
- ambient: {};
26
- };
4
+ import type { Module } from "./module.js";
5
+ import { type Scope } from "./scope.js";
6
+ import type { DeclarationParser, DefinitionParser, TypeParser } from "./type.js";
27
7
  type TsGenericsExports<$ = Ark> = {
28
8
  Record: Generic<[
29
9
  "K",
@@ -32,21 +12,17 @@ type TsGenericsExports<$ = Ark> = {
32
12
  "[K]": "V";
33
13
  }, $>;
34
14
  };
35
- export declare const tsGenerics: Module<TsGenericsResolutions<Ark>>;
36
- export declare const ark: Scope<ArkResolutions>;
37
- export declare const arktypes: Module<ArkResolutions>;
38
- export interface Ark extends TsKeywords.infer, JsObjects.infer, Validation.infer, TsGenericsExports {
39
- parse: Module<ParsingResolutions>;
40
- }
41
- export declare const scope: ScopeParser<{}, Ark>;
42
- export declare const type: TypeParser<Ark>;
43
- export declare const match: MatchParser<Ark>;
15
+ export declare const tsGenerics: Module<TsGenericsExports>;
16
+ export declare const ambient: Scope<Ark>;
17
+ export declare const ark: Module<Ark>;
18
+ export declare const type: TypeParser<{}>;
44
19
  export declare namespace type {
45
- type cast<to> = {
46
- [inferred]?: to;
20
+ type cast<t> = {
21
+ [inferred]?: t;
47
22
  };
23
+ type errors = ArkErrors;
48
24
  }
49
- export declare const define: DefinitionParser<Ark>;
50
- export declare const declare: DeclarationParser<Ark>;
25
+ export declare const match: MatchParser<{}>;
26
+ export declare const define: DefinitionParser<{}>;
27
+ export declare const declare: DeclarationParser<{}>;
51
28
  export {};
52
- //# sourceMappingURL=ark.d.ts.map
package/out/ark.js CHANGED
@@ -1,19 +1,10 @@
1
- import { JsObjects, Parsing, TsKeywords, Validation } from "@arktype/schema";
2
- import { Scope } from "./scope.js";
1
+ import { keywordNodes } from "@arktype/schema";
2
+ import { RawScope, scope } from "./scope.js";
3
3
  export const tsGenerics = {};
4
- export const ark = Scope.root({
5
- ...TsKeywords.resolutions,
6
- ...JsObjects.resolutions,
7
- ...Validation.resolutions,
8
- // TODO: fix
9
- ...tsGenerics,
10
- parse: Parsing.resolutions
11
- }).toAmbient();
12
- export const arktypes = ark.export();
13
- export const scope = ark.scope;
14
- export const type = ark.type;
15
- // TODO: cast needed?
16
- export const match = ark.match;
17
- export const define = ark.define;
18
- export const declare = ark.declare;
19
- //# sourceMappingURL=ark.js.map
4
+ export const ambient = scope(keywordNodes);
5
+ RawScope.ambient = ambient.raw;
6
+ export const ark = ambient.export();
7
+ export const type = ambient.type;
8
+ export const match = ambient.match;
9
+ export const define = ambient.define;
10
+ export const declare = ambient.declare;
package/out/ark.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ark.js","sourceRoot":"","sources":["../ark.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EACT,OAAO,EACP,UAAU,EACV,UAAU,EAEV,MAAM,iBAAiB,CAAA;AAExB,OAAO,EAAE,KAAK,EAAiC,MAAM,YAAY,CAAA;AA4CjE,MAAM,CAAC,MAAM,UAAU,GAAG,EAAmC,CAAA;AAE7D,MAAM,CAAC,MAAM,GAAG,GAA0B,KAAK,CAAC,IAAI,CAAC;IACpD,GAAG,UAAU,CAAC,WAAW;IACzB,GAAG,SAAS,CAAC,WAAW;IACxB,GAAG,UAAU,CAAC,WAAW;IACzB,YAAY;IACZ,GAAG,UAAU;IACb,KAAK,EAAE,OAAO,CAAC,WAA+C;CAC9D,CAAC,CAAC,SAAS,EAAW,CAAA;AAEvB,MAAM,CAAC,MAAM,QAAQ,GAA2B,GAAG,CAAC,MAAM,EAAE,CAAA;AAY5D,MAAM,CAAC,MAAM,KAAK,GAAyB,GAAG,CAAC,KAAc,CAAA;AAE7D,MAAM,CAAC,MAAM,IAAI,GAAoB,GAAG,CAAC,IAAI,CAAA;AAE7C,qBAAqB;AACrB,MAAM,CAAC,MAAM,KAAK,GAAqB,GAAG,CAAC,KAAc,CAAA;AAQzD,MAAM,CAAC,MAAM,MAAM,GAA0B,GAAG,CAAC,MAAM,CAAA;AAEvD,MAAM,CAAC,MAAM,OAAO,GAA2B,GAAG,CAAC,OAAO,CAAA"}
1
+ {"version":3,"file":"ark.js","sourceRoot":"","sources":["../ark.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,YAAY,EACZ,MAAM,iBAAiB,CAAA;AAIxB,OAAO,EAAE,QAAQ,EAAc,KAAK,EAAE,MAAM,YAAY,CAAA;AAexD,MAAM,CAAC,MAAM,UAAU,GAAG,EAA+B,CAAA;AAEzD,MAAM,CAAC,MAAM,OAAO,GAAe,KAAK,CAAC,YAAY,CAAU,CAAA;AAE/D,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC,GAAG,CAAA;AAE9B,MAAM,CAAC,MAAM,GAAG,GAAgB,OAAO,CAAC,MAAM,EAAE,CAAA;AAEhD,MAAM,CAAC,MAAM,IAAI,GAAmB,OAAO,CAAC,IAAa,CAAA;AAUzD,MAAM,CAAC,MAAM,KAAK,GAAoB,OAAO,CAAC,KAAc,CAAA;AAE5D,MAAM,CAAC,MAAM,MAAM,GAAyB,OAAO,CAAC,MAAe,CAAA;AAEnE,MAAM,CAAC,MAAM,OAAO,GAA0B,OAAO,CAAC,OAAgB,CAAA"}
@@ -0,0 +1,21 @@
1
+ import { type GenericNodeInstantiation, type GenericProps, type SchemaScope, arkKind } from "@arktype/schema";
2
+ import { Callable, type conform } from "@arktype/util";
3
+ import type { inferDefinition } from "./parser/definition.js";
4
+ import type { GenericParamsParseError, parseGenericParams } from "./parser/generic.js";
5
+ import type { Type, inferTypeRoot, validateTypeRoot } from "./type.js";
6
+ export type validateParameterString<params extends string> = parseGenericParams<params> extends GenericParamsParseError<infer message> ? message : params;
7
+ export type GenericTypeInstantiation<params extends string[] = string[], def = any, $ = any> = <args>(...args: conform<args, {
8
+ [i in keyof params]: validateTypeRoot<args[i & keyof args], $>;
9
+ }>) => Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>;
10
+ export type GenericInstantiation<params extends string[] = string[], def = any, $ = any> = GenericTypeInstantiation<params, def, $> & GenericNodeInstantiation<params, def, $>;
11
+ type bindGenericInstantiation<params extends string[], $, args> = {
12
+ [i in keyof params & `${number}` as params[i]]: inferTypeRoot<args[i & keyof args], $>;
13
+ };
14
+ export declare class Generic<params extends string[] = string[], def = unknown, $ = any> extends Callable<GenericInstantiation<params, def, $>> implements GenericProps<params, def, $> {
15
+ params: params;
16
+ def: def;
17
+ $: SchemaScope<$>;
18
+ readonly [arkKind] = "generic";
19
+ constructor(params: params, def: def, $: SchemaScope<$>);
20
+ }
21
+ export {};
package/out/generic.js ADDED
@@ -0,0 +1,24 @@
1
+ import { arkKind } from "@arktype/schema";
2
+ import { Callable } from "@arktype/util";
3
+ export class Generic extends Callable {
4
+ params;
5
+ def;
6
+ $;
7
+ [arkKind] = "generic";
8
+ constructor(params, def,
9
+ // TODO: should be Scope<$>, but breaks inference
10
+ $) {
11
+ super((...args) => {
12
+ // const argNodes = flatMorph(params, (i, param: string) => [
13
+ // param,
14
+ // $.parseRoot(args[i])
15
+ // ])
16
+ // { args: argNodes }
17
+ args;
18
+ return $.parseRoot(def);
19
+ });
20
+ this.params = params;
21
+ this.def = def;
22
+ this.$ = $;
23
+ }
24
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generic.js","sourceRoot":"","sources":["../generic.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,OAAO,EACP,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,QAAQ,EAAgB,MAAM,eAAe,CAAA;AAwCtD,MAAM,OAAO,OACZ,SAAQ,QAA8C;IAM9C;IACA;IAEA;IANC,CAAC,OAAO,CAAC,GAAG,SAAS,CAAA;IAE9B,YACQ,MAAc,EACd,GAAQ;IACf,iDAAiD;IAC1C,CAAiB;QAExB,KAAK,CAAC,CAAC,GAAG,IAAe,EAAE,EAAE;YAC5B,6DAA6D;YAC7D,UAAU;YACV,wBAAwB;YACxB,KAAK;YACL,qBAAqB;YACrB,IAAI,CAAA;YACJ,OAAO,CAAC,CAAC,SAAS,CAAC,GAAG,CAAU,CAAA;QACjC,CAAC,CAAC,CAAA;QAbK,WAAM,GAAN,MAAM,CAAQ;QACd,QAAG,GAAH,GAAG,CAAK;QAER,MAAC,GAAD,CAAC,CAAgB;IAWzB,CAAC;CACD"}
package/out/main.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- export { ArkErrors as ArkErrors, type ArkTypeError as ArkTypeError } from "@arktype/schema";
2
- export type { Out, is } from "@arktype/schema";
3
- export { ark, arktypes, declare, define, match, scope, type, type Ark } from "./ark.js";
4
- export type { Module, Scope } from "./scope.js";
5
- export { Type } from "./type.js";
6
- export type { inferTypeRoot, validateTypeRoot } from "./type.js";
7
- //# sourceMappingURL=main.d.ts.map
1
+ export { ArkError, ArkErrors, ArkTypeError } from "@arktype/schema";
2
+ export type { Ark, ArkConfig, Out } from "@arktype/schema";
3
+ export { ambient, ark, declare, define, match, type } from "./ark.js";
4
+ export { Module } from "./module.js";
5
+ export { scope, type Scope } from "./scope.js";
6
+ export { Type, type inferTypeRoot, type validateTypeRoot } from "./type.js";
package/out/main.js CHANGED
@@ -1,4 +1,5 @@
1
- export { ArkErrors as ArkErrors } from "@arktype/schema";
2
- export { ark, arktypes, declare, define, match, scope, type } from "./ark.js";
1
+ export { ArkError, ArkErrors, ArkTypeError } from "@arktype/schema";
2
+ export { ambient, ark, declare, define, match, type } from "./ark.js";
3
+ export { Module } from "./module.js";
4
+ export { scope } from "./scope.js";
3
5
  export { Type } from "./type.js";
4
- //# sourceMappingURL=main.js.map
package/out/main.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.js","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,IAAI,SAAS,EAEtB,MAAM,iBAAiB,CAAA;AAExB,OAAO,EACN,GAAG,EACH,QAAQ,EACR,OAAO,EACP,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EAEJ,MAAM,UAAU,CAAA;AAEjB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA"}
1
+ {"version":3,"file":"main.js","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAEnE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AACrE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,KAAK,EAAc,MAAM,YAAY,CAAA;AAC9C,OAAO,EAAE,IAAI,EAA6C,MAAM,WAAW,CAAA"}