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.
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/parser/generic.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { throwParseError, type nominal } from "@arktype/util"
1
+ import { type keyError, throwParseError } from "@arktype/util"
2
2
  import { writeUnexpectedCharacterMessage } from "./string/shift/operator/operator.js"
3
3
  import { Scanner } from "./string/shift/scanner.js"
4
4
 
@@ -9,60 +9,58 @@ export type GenericDeclaration<
9
9
 
10
10
  // we put the error in a tuple so that parseGenericParams always returns a string[]
11
11
  export type GenericParamsParseError<message extends string = string> = [
12
- nominal<message, "InvalidGenericParameters">
12
+ keyError<message>
13
13
  ]
14
14
 
15
- export const parseGenericParams = (def: string) =>
16
- parseGenericParamsRecurse(new Scanner(def))
15
+ export const parseGenericParams = (def: string): string[] =>
16
+ _parseGenericParams(new Scanner(def))
17
17
 
18
- export type parseGenericParams<def extends string> = parseParamsRecurse<
19
- def,
20
- "",
21
- []
22
- > extends infer result extends string[]
23
- ? "" extends result[number]
24
- ? GenericParamsParseError<emptyGenericParameterMessage>
25
- : result
26
- : never
18
+ export type parseGenericParams<def extends string> =
19
+ _parseParams<def, "", []> extends infer result extends string[] ?
20
+ "" extends result[number] ?
21
+ GenericParamsParseError<emptyGenericParameterMessage>
22
+ : result
23
+ : never
27
24
 
28
- export const emptyGenericParameterMessage = `An empty string is not a valid generic parameter name`
25
+ export const emptyGenericParameterMessage =
26
+ "An empty string is not a valid generic parameter name"
29
27
 
30
28
  export type emptyGenericParameterMessage = typeof emptyGenericParameterMessage
31
29
 
32
- const parseGenericParamsRecurse = (scanner: Scanner): string[] => {
30
+ const _parseGenericParams = (scanner: Scanner): string[] => {
33
31
  const param = scanner.shiftUntilNextTerminator()
34
- if (param === "") {
35
- throwParseError(emptyGenericParameterMessage)
36
- }
32
+ if (param === "") throwParseError(emptyGenericParameterMessage)
33
+
37
34
  scanner.shiftUntilNonWhitespace()
38
35
  const nextNonWhitespace = scanner.shift()
39
- return nextNonWhitespace === ""
40
- ? [param]
41
- : nextNonWhitespace === ","
42
- ? [param, ...parseGenericParamsRecurse(scanner)]
36
+ return (
37
+ nextNonWhitespace === "" ? [param]
38
+ : nextNonWhitespace === "," ? [param, ..._parseGenericParams(scanner)]
43
39
  : throwParseError(writeUnexpectedCharacterMessage(nextNonWhitespace, ","))
40
+ )
44
41
  }
45
42
 
46
- type parseParamsRecurse<
43
+ type _parseParams<
47
44
  unscanned extends string,
48
45
  param extends string,
49
46
  result extends string[]
50
- > = unscanned extends `${infer lookahead}${infer nextUnscanned}`
51
- ? lookahead extends ","
52
- ? parseParamsRecurse<nextUnscanned, "", [...result, param]>
53
- : lookahead extends Scanner.WhiteSpaceToken
54
- ? param extends ""
55
- ? // if the next char is whitespace and we aren't in the middle of a param, skip to the next one
56
- parseParamsRecurse<Scanner.skipWhitespace<nextUnscanned>, "", result>
57
- : Scanner.skipWhitespace<nextUnscanned> extends `${infer nextNonWhitespace}${infer rest}`
58
- ? nextNonWhitespace extends ","
59
- ? parseParamsRecurse<rest, "", [...result, param]>
60
- : GenericParamsParseError<
47
+ > =
48
+ unscanned extends `${infer lookahead}${infer nextUnscanned}` ?
49
+ lookahead extends "," ? _parseParams<nextUnscanned, "", [...result, param]>
50
+ : lookahead extends Scanner.WhiteSpaceToken ?
51
+ param extends "" ?
52
+ // if the next char is whitespace and we aren't in the middle of a param, skip to the next one
53
+ _parseParams<Scanner.skipWhitespace<nextUnscanned>, "", result>
54
+ : Scanner.skipWhitespace<nextUnscanned> extends (
55
+ `${infer nextNonWhitespace}${infer rest}`
56
+ ) ?
57
+ nextNonWhitespace extends "," ?
58
+ _parseParams<rest, "", [...result, param]>
59
+ : GenericParamsParseError<
61
60
  writeUnexpectedCharacterMessage<nextNonWhitespace, ",">
62
- >
63
- : // params end with a single whitespace character, add the current token
64
- [...result, param]
65
- : parseParamsRecurse<nextUnscanned, `${param}${lookahead}`, result>
66
- : param extends ""
67
- ? result
61
+ >
62
+ : // params end with a single whitespace character, add the current token
63
+ [...result, param]
64
+ : _parseParams<nextUnscanned, `${param}${lookahead}`, result>
65
+ : param extends "" ? result
68
66
  : [...result, param]
@@ -1,113 +1,100 @@
1
- import { keywords, schema, type Inner, type TypeNode } from "@arktype/schema"
2
1
  import {
3
- printable,
4
- throwParseError,
2
+ type NodeDef,
3
+ type RawSchema,
4
+ tsKeywords,
5
+ type writeInvalidPropertyKeyMessage
6
+ } from "@arktype/schema"
7
+ import {
5
8
  type Dict,
6
9
  type ErrorMessage,
7
- type evaluate,
8
- type merge
10
+ type Key,
11
+ type merge,
12
+ printable,
13
+ type show,
14
+ spliterate,
15
+ stringAndSymbolicEntriesOf,
16
+ throwParseError
9
17
  } from "@arktype/util"
10
-
11
18
  import type { ParseContext } from "../scope.js"
12
19
  import type { inferDefinition, validateDefinition } from "./definition.js"
13
20
  import type { astToString } from "./semantic/utils.js"
14
21
  import type { validateString } from "./semantic/validate.js"
15
- import {
16
- parseEntry,
17
- type EntryParseResult,
18
- type IndexedKey,
19
- type OptionalValue,
20
- type validateObjectValue
21
- } from "./shared.js"
22
-
23
- const stringAndSymbolicEntriesOf = (o: Record<string | symbol, unknown>) => [
24
- ...Object.entries(o),
25
- ...Object.getOwnPropertySymbols(o).map((k) => [k, o[k]] as const)
26
- ]
27
-
28
- export const parseObjectLiteral = (def: Dict, ctx: ParseContext): TypeNode => {
29
- const required: Inner<"required">[] = []
30
- const optional: Inner<"optional">[] = []
22
+ import { Scanner } from "./string/shift/scanner.js"
31
23
 
24
+ export const parseObjectLiteral = (def: Dict, ctx: ParseContext): RawSchema => {
25
+ const propNodes: NodeDef<"prop">[] = []
26
+ const indexNodes: NodeDef<"index">[] = []
32
27
  // We only allow a spread operator to be used as the first key in an object
33
28
  // because to match JS behavior any keys before the spread are overwritten
34
29
  // by the values in the target object, so there'd be no useful purpose in having it
35
30
  // anywhere except for the beginning.
36
- // Discussion in ArkType Discord:
37
- // https://discord.com/channels/957797212103016458/1103023445035462678/1182814502471860334
38
- let hasSeenFirstKey = false
39
-
40
- const entries = stringAndSymbolicEntriesOf(def)
41
- for (const entry of entries) {
42
- const result = parseEntry(entry)
43
-
44
- if (result.kind === "spread") {
45
- if (hasSeenFirstKey) {
46
- return throwParseError(
47
- "Spread operator may only be used as the first key in an object"
48
- )
49
- }
50
-
51
- const spreadNode = ctx.scope.parse(result.innerValue, ctx)
52
-
53
- if (
54
- spreadNode.kind !== "intersection" ||
55
- !spreadNode.extends(keywords.object)
56
- ) {
57
- return throwParseError(
58
- writeInvalidSpreadTypeMessage(printable(result.innerValue))
59
- )
60
- }
61
-
62
- // For each key on spreadNode, add it to our object.
63
- // We filter out keys from the spreadNode that will be defined later on this same object
64
- // because the currently parsed definition will overwrite them.
65
- const requiredEntriesFromSpread = (spreadNode.required ?? []).filter(
66
- (e) => !entries.some(([k]) => k === e.key)
31
+ const parsedEntries = stringAndSymbolicEntriesOf(def).map(parseEntry)
32
+ if (parsedEntries[0]?.kind === "spread") {
33
+ // remove the spread entry so we can iterate over the remaining entries
34
+ // expecting non-spread entries
35
+ const spreadEntry = parsedEntries.shift()!
36
+ const spreadNode = ctx.$.parse(spreadEntry.value, ctx)
37
+ if (
38
+ !spreadNode.hasKind("intersection") ||
39
+ !spreadNode.extends(tsKeywords.object)
40
+ ) {
41
+ return throwParseError(
42
+ writeInvalidSpreadTypeMessage(printable(spreadEntry.value))
67
43
  )
68
-
69
- const optionalEntriesFromSpread = (spreadNode.optional ?? []).filter(
70
- (e) => !entries.some(([k]) => k === e.key)
71
- )
72
-
73
- required.push(...requiredEntriesFromSpread)
74
- optional.push(...optionalEntriesFromSpread)
75
-
76
- continue
77
44
  }
78
-
79
- ctx.path.push(
80
- `${
81
- typeof result.innerKey === "symbol"
82
- ? `[${printable(result.innerKey)}]`
83
- : result.innerKey
84
- }`
45
+ // TODO: move to props group merge in schema
46
+ // For each key on spreadNode, add it to our object.
47
+ // We filter out keys from the spreadNode that will be defined later on this same object
48
+ // because the currently parsed definition will overwrite them.
49
+ spreadNode.prop?.forEach(
50
+ spreadRequired =>
51
+ !parsedEntries.some(
52
+ ({ inner: innerKey }) => innerKey === spreadRequired.key
53
+ ) && propNodes.push(spreadRequired)
85
54
  )
86
- const valueNode = ctx.scope.parse(result.innerValue, ctx)
55
+ }
56
+ for (const entry of parsedEntries) {
57
+ if (entry.kind === "spread") return throwParseError(nonLeadingSpreadError)
87
58
 
88
- if (result.kind === "optional") {
89
- optional.push({
90
- key: result.innerKey,
91
- value: valueNode
92
- })
59
+ if (entry.kind === "index") {
60
+ // handle key parsing first to match type behavior
61
+ const key = ctx.$.parse(entry.inner, ctx)
62
+ const value = ctx.$.parse(entry.value, ctx)
63
+
64
+ // extract enumerable named props from the index signature
65
+ const [enumerable, nonEnumerable] = spliterate(key.branches, k =>
66
+ k.hasKind("unit")
67
+ )
68
+
69
+ if (enumerable.length) {
70
+ propNodes.push(
71
+ ...enumerable.map(k =>
72
+ ctx.$.node("prop", { key: k.unit as Key, value })
73
+ )
74
+ )
75
+ if (nonEnumerable.length)
76
+ indexNodes.push(ctx.$.node("index", { key: nonEnumerable, value }))
77
+ } else indexNodes.push(ctx.$.node("index", { key, value }))
93
78
  } else {
94
- required.push({
95
- key: result.innerKey,
96
- value: valueNode
79
+ const value = ctx.$.parse(entry.value, ctx)
80
+ propNodes.push({
81
+ key: entry.inner,
82
+ value,
83
+ optional: entry.kind === "optional"
97
84
  })
98
85
  }
99
- ctx.path.pop()
100
-
101
- hasSeenFirstKey ||= true
102
86
  }
103
87
 
104
- return schema({
105
- basis: "object",
106
- required,
107
- optional
88
+ return ctx.$.schema({
89
+ domain: "object",
90
+ prop: propNodes,
91
+ index: indexNodes
108
92
  })
109
93
  }
110
94
 
95
+ export const nonLeadingSpreadError =
96
+ "Spread operator may only be used as the first key in an object"
97
+
111
98
  /**
112
99
  * Infers the contents of an object literal, ignoring a spread definition
113
100
  * You probably want to use {@link inferObjectLiteral} instead.
@@ -116,63 +103,120 @@ type inferObjectLiteralInner<def extends object, $, args> = {
116
103
  // since def is a const parameter, we remove the readonly modifier here
117
104
  // support for builtin readonly tracked here:
118
105
  // https://github.com/arktypeio/arktype/issues/808
119
- -readonly [k in keyof def as nonOptionalKeyFrom<
120
- k,
106
+ -readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: inferDefinition<
121
107
  def[k],
122
108
  $,
123
109
  args
124
- >]: inferDefinition<def[k], $, args>
110
+ >
125
111
  } & {
126
- -readonly [k in keyof def as optionalKeyFrom<k, def[k]>]?: inferDefinition<
127
- def[k] extends OptionalValue<infer inner> ? inner : def[k],
112
+ -readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<
113
+ def[k],
128
114
  $,
129
115
  args
130
116
  >
131
117
  }
132
118
 
133
- export type inferObjectLiteral<def extends object, $, args> = evaluate<
134
- "..." extends keyof def
135
- ? merge<
136
- inferDefinition<def["..."], $, args>,
137
- inferObjectLiteralInner<def, $, args>
138
- >
139
- : inferObjectLiteralInner<def, $, args>
119
+ export type inferObjectLiteral<def extends object, $, args> = show<
120
+ "..." extends keyof def ?
121
+ merge<
122
+ inferDefinition<def["..."], $, args>,
123
+ inferObjectLiteralInner<def, $, args>
124
+ >
125
+ : inferObjectLiteralInner<def, $, args>
140
126
  >
141
127
 
142
128
  export type validateObjectLiteral<def, $, args> = {
143
- [k in keyof def]: k extends IndexedKey<infer indexDef>
144
- ? validateString<indexDef, $, args> extends ErrorMessage<infer message>
145
- ? // add a nominal type here to avoid allowing the error message as input
146
- indexParseError<message>
147
- : inferDefinition<indexDef, $, args> extends PropertyKey
148
- ? // if the indexDef is syntactically and semantically valid,
149
- // move on to the validating the value definition
150
- validateDefinition<def[k], $, args>
151
- : indexParseError<writeInvalidPropertyKeyMessage<indexDef>>
152
- : k extends "..."
153
- ? inferDefinition<def[k], $, args> extends object
154
- ? validateDefinition<def[k], $, args>
155
- : indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>>
156
- : validateObjectValue<def[k], $, args>
129
+ [k in keyof def]: k extends IndexKey<infer indexDef> ?
130
+ validateString<indexDef, $, args> extends ErrorMessage<infer message> ?
131
+ // add a nominal type here to avoid allowing the error message as input
132
+ indexParseError<message>
133
+ : inferDefinition<indexDef, $, args> extends PropertyKey ?
134
+ // if the indexDef is syntactically and semantically valid,
135
+ // move on to the validating the value definition
136
+ validateDefinition<def[k], $, args>
137
+ : indexParseError<writeInvalidPropertyKeyMessage<indexDef>>
138
+ : k extends "..." ?
139
+ inferDefinition<def[k], $, args> extends object ?
140
+ validateDefinition<def[k], $, args>
141
+ : indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>>
142
+ : validateDefinition<def[k], $, args>
157
143
  }
158
144
 
159
- type nonOptionalKeyFrom<k extends PropertyKey, valueDef, $, args> = parseEntry<
160
- k,
161
- valueDef
162
- > extends infer result extends EntryParseResult
163
- ? result["kind"] extends "required"
164
- ? result["innerKey"]
165
- : result["kind"] extends "indexed"
166
- ? inferDefinition<result["innerKey"], $, args> & PropertyKey
167
- : never
168
- : never
169
-
170
- type optionalKeyFrom<k extends PropertyKey, valueDef> = parseEntry<
171
- k,
172
- valueDef
173
- > extends infer result extends EntryParseResult<"optional">
174
- ? result["innerKey"]
175
- : never
145
+ type nonOptionalKeyFrom<k, $, args> =
146
+ parseKey<k> extends PreparsedKey<"required", infer inner> ? inner
147
+ : parseKey<k> extends PreparsedKey<"index", infer inner> ?
148
+ inferDefinition<inner, $, args> & Key
149
+ : // spread key is handled at the type root so is handled neither here nor in optionalKeyFrom
150
+ never
151
+
152
+ type optionalKeyFrom<k> =
153
+ parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never
154
+
155
+ type PreparsedKey<
156
+ kind extends ParsedKeyKind = ParsedKeyKind,
157
+ inner extends Key = Key
158
+ > = {
159
+ kind: kind
160
+ inner: inner
161
+ }
162
+
163
+ namespace PreparsedKey {
164
+ export type from<t extends PreparsedKey> = t
165
+ }
166
+
167
+ type ParsedKeyKind = "required" | "optional" | "index" | "spread"
168
+
169
+ export type IndexKey<def extends string = string> = `[${def}]`
170
+
171
+ type PreparsedEntry = PreparsedKey & { value: unknown }
172
+
173
+ export const parseEntry = (entry: readonly [Key, unknown]): PreparsedEntry =>
174
+ Object.assign(parseKey(entry[0]), { value: entry[1] })
175
+
176
+ const parseKey = (key: Key): PreparsedKey =>
177
+ typeof key === "symbol" ? { inner: key, kind: "required" }
178
+ : key.at(-1) === "?" ?
179
+ key.at(-2) === Scanner.escapeToken ?
180
+ { inner: `${key.slice(0, -2)}?`, kind: "required" }
181
+ : {
182
+ inner: key.slice(0, -1),
183
+ kind: "optional"
184
+ }
185
+ : key[0] === "[" && key.at(-1) === "]" ?
186
+ { inner: key.slice(1, -1), kind: "index" }
187
+ : key[0] === Scanner.escapeToken && key[1] === "[" && key.at(-1) === "]" ?
188
+ { inner: key.slice(1), kind: "required" }
189
+ : key === "..." ? { inner: "...", kind: "spread" }
190
+ : { inner: key === "\\..." ? "..." : key, kind: "required" }
191
+
192
+ type parseKey<k> =
193
+ k extends `${infer inner}?` ?
194
+ inner extends `${infer baseName}${Scanner.EscapeToken}` ?
195
+ PreparsedKey.from<{
196
+ kind: "required"
197
+ inner: `${baseName}?`
198
+ }>
199
+ : PreparsedKey.from<{
200
+ kind: "optional"
201
+ inner: inner
202
+ }>
203
+ : k extends "..." ? PreparsedKey.from<{ kind: "spread"; inner: "..." }>
204
+ : k extends `${Scanner.EscapeToken}...` ?
205
+ PreparsedKey.from<{ kind: "required"; inner: "..." }>
206
+ : k extends IndexKey<infer def> ?
207
+ PreparsedKey.from<{
208
+ kind: "index"
209
+ inner: def
210
+ }>
211
+ : PreparsedKey.from<{
212
+ kind: "required"
213
+ inner: k extends (
214
+ `${Scanner.EscapeToken}${infer escapedIndexKey extends IndexKey}`
215
+ ) ?
216
+ escapedIndexKey
217
+ : k extends Key ? k
218
+ : `${k & number}`
219
+ }>
176
220
 
177
221
  declare const indexParseSymbol: unique symbol
178
222
 
@@ -180,14 +224,6 @@ export type indexParseError<message extends string = string> = {
180
224
  [indexParseSymbol]: message
181
225
  }
182
226
 
183
- export const writeInvalidPropertyKeyMessage = <indexDef extends string>(
184
- indexDef: indexDef
185
- ): writeInvalidPropertyKeyMessage<indexDef> =>
186
- `Indexed key definition '${indexDef}' must be a string, number or symbol`
187
-
188
- type writeInvalidPropertyKeyMessage<indexDef extends string> =
189
- `Indexed key definition '${indexDef}' must be a string, number or symbol`
190
-
191
227
  export const writeInvalidSpreadTypeMessage = <def extends string>(
192
228
  def: def
193
229
  ): writeInvalidSpreadTypeMessage<def> =>
@@ -1,30 +1,23 @@
1
- import type { NumericallyBoundable } from "@arktype/schema"
2
- import type { ErrorMessage } from "@arktype/util"
1
+ import type { LimitLiteral, writeUnboundableMessage } from "@arktype/schema"
2
+ import type { ErrorMessage, array } from "@arktype/util"
3
3
  import type {
4
4
  Comparator,
5
- InvertedComparators,
6
- LimitLiteral
5
+ InvertedComparators
7
6
  } from "../string/reduce/shared.js"
8
7
  import type {
9
8
  BoundExpressionKind,
10
- writeInvalidLimitMessage,
11
- writeUnboundableMessage
9
+ writeInvalidLimitMessage
12
10
  } from "../string/shift/operator/bounds.js"
13
- import type { inferAstBase } from "./semantic.js"
11
+ import type { inferAstIn } from "./infer.js"
14
12
  import type { astToString } from "./utils.js"
15
13
  import type { validateAst } from "./validate.js"
16
14
 
17
- export type validateRange<
18
- l,
19
- comparator extends Comparator,
20
- r,
21
- $,
22
- args
23
- > = l extends LimitLiteral
24
- ? validateBound<r, InvertedComparators[comparator], l, "left", $, args>
25
- : l extends [infer leftAst, Comparator, unknown]
26
- ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>>
27
- : validateBound<l, comparator, r & LimitLiteral, "right", $, args>
15
+ export type validateRange<l, comparator extends Comparator, r, $, args> =
16
+ l extends LimitLiteral ?
17
+ validateBound<r, InvertedComparators[comparator], l, "left", $, args>
18
+ : l extends [infer leftAst, Comparator, unknown] ?
19
+ ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>>
20
+ : validateBound<l, comparator, r & LimitLiteral, "right", $, args>
28
21
 
29
22
  export type validateBound<
30
23
  boundedAst,
@@ -33,24 +26,32 @@ export type validateBound<
33
26
  boundKind extends BoundExpressionKind,
34
27
  $,
35
28
  args
36
- > = inferAstBase<boundedAst, $, args> extends infer bounded
37
- ? [bounded] extends [NumericallyBoundable]
38
- ? limit extends number
39
- ? validateAst<boundedAst, $, args>
40
- : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
41
- : bounded extends Date
42
- ? // allow numeric or date literal as a Date limit
43
- validateAst<boundedAst, $, args>
44
- : ErrorMessage<
29
+ > =
30
+ inferAstIn<boundedAst, $, args> extends infer bounded ?
31
+ isNumericallyBoundable<bounded> extends true ?
32
+ limit extends number ?
33
+ validateAst<boundedAst, $, args>
34
+ : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
35
+ : bounded extends Date ?
36
+ // allow numeric or date literal as a Date limit
37
+ validateAst<boundedAst, $, args>
38
+ : ErrorMessage<
45
39
  writeUnboundableMessage<
46
40
  astToString<
47
- boundKind extends "left"
48
- ? boundedAst[0 & keyof boundedAst]
49
- : boundedAst
41
+ boundKind extends "left" ? boundedAst[0 & keyof boundedAst]
42
+ : boundedAst
50
43
  >
51
44
  >
52
- >
53
- : never
45
+ >
46
+ : never
47
+
48
+ // Check each numerically boundable type individually so an expression comprised
49
+ // of mixed bound kinds like (string|number)<5 isn't allowed
50
+ type isNumericallyBoundable<bounded> =
51
+ [bounded] extends [number] ? true
52
+ : [bounded] extends [string] ? true
53
+ : [bounded] extends [array] ? true
54
+ : false
54
55
 
55
56
  export const writeDoubleRightBoundMessage = <root extends string>(
56
57
  root: root
@@ -1,13 +1,11 @@
1
- import type { writeIndivisibleMessage } from "@arktype/schema"
1
+ import type { Schema, writeIndivisibleMessage } from "@arktype/schema"
2
2
  import type { ErrorMessage } from "@arktype/util"
3
- import type { inferAstBase } from "./semantic.js"
4
- import type { astToString } from "./utils.js"
3
+ import type { inferAstIn } from "./infer.js"
5
4
  import type { validateAst } from "./validate.js"
6
5
 
7
- export type validateDivisor<l, $, args> = isDivisible<
8
- inferAstBase<l, $, args>
9
- > extends true
10
- ? validateAst<l, $, args>
11
- : ErrorMessage<writeIndivisibleMessage<astToString<l>>>
12
-
13
- type isDivisible<data> = [data] extends [number] ? true : false
6
+ export type validateDivisor<l, $, args> =
7
+ inferAstIn<l, $, args> extends infer data ?
8
+ [data] extends [number] ?
9
+ validateAst<l, $, args>
10
+ : ErrorMessage<writeIndivisibleMessage<Schema<data>>>
11
+ : never