@sinclair/typebox 0.33.22 → 0.34.1

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 (202) hide show
  1. package/build/cjs/compiler/compiler.d.ts +2 -2
  2. package/build/cjs/compiler/compiler.js +13 -2
  3. package/build/cjs/errors/errors.js +7 -0
  4. package/build/cjs/index.d.ts +1 -2
  5. package/build/cjs/index.js +1 -2
  6. package/build/cjs/syntax/parse.d.ts +14 -15
  7. package/build/cjs/syntax/parse.js +6 -5
  8. package/build/cjs/syntax/runtime.d.ts +20 -4
  9. package/build/cjs/syntax/runtime.js +183 -28
  10. package/build/cjs/syntax/static.d.ts +107 -23
  11. package/build/cjs/type/array/array.d.ts +1 -1
  12. package/build/cjs/type/awaited/awaited.d.ts +8 -3
  13. package/build/cjs/type/awaited/awaited.js +20 -20
  14. package/build/cjs/type/computed/computed.d.ts +9 -0
  15. package/build/cjs/type/computed/computed.js +10 -0
  16. package/build/cjs/type/computed/index.d.ts +1 -0
  17. package/build/cjs/type/{deref → computed}/index.js +1 -1
  18. package/build/cjs/type/guard/kind.d.ts +12 -4
  19. package/build/cjs/type/guard/kind.js +15 -0
  20. package/build/cjs/type/guard/type.d.ts +9 -3
  21. package/build/cjs/type/guard/type.js +19 -0
  22. package/build/cjs/type/index.d.ts +1 -2
  23. package/build/cjs/type/index.js +1 -2
  24. package/build/cjs/type/indexed/indexed-from-mapped-key.d.ts +6 -6
  25. package/build/cjs/type/indexed/indexed-from-mapped-key.js +10 -10
  26. package/build/cjs/type/indexed/indexed-from-mapped-result.d.ts +5 -5
  27. package/build/cjs/type/indexed/indexed-from-mapped-result.js +11 -10
  28. package/build/cjs/type/indexed/indexed-property-keys.d.ts +6 -6
  29. package/build/cjs/type/indexed/indexed-property-keys.js +14 -14
  30. package/build/cjs/type/indexed/indexed.d.ts +34 -20
  31. package/build/cjs/type/indexed/indexed.js +59 -46
  32. package/build/cjs/type/intersect/intersect-evaluated.d.ts +6 -6
  33. package/build/cjs/type/intersect/intersect-evaluated.js +16 -16
  34. package/build/cjs/type/intersect/intersect.d.ts +2 -2
  35. package/build/cjs/type/intersect/intersect.js +6 -6
  36. package/build/cjs/type/keyof/keyof-from-mapped-result.d.ts +6 -6
  37. package/build/cjs/type/keyof/keyof-from-mapped-result.js +10 -10
  38. package/build/cjs/type/keyof/keyof-property-keys.d.ts +7 -7
  39. package/build/cjs/type/keyof/keyof-property-keys.js +22 -22
  40. package/build/cjs/type/keyof/keyof.d.ts +11 -6
  41. package/build/cjs/type/keyof/keyof.js +22 -13
  42. package/build/cjs/type/module/compute.d.ts +54 -0
  43. package/build/cjs/type/module/compute.js +154 -0
  44. package/build/cjs/type/module/index.d.ts +1 -0
  45. package/build/cjs/type/{strict → module}/index.js +1 -1
  46. package/build/cjs/type/module/infer.d.ts +43 -0
  47. package/build/cjs/type/module/infer.js +3 -0
  48. package/build/cjs/type/module/module.d.ts +27 -0
  49. package/build/cjs/type/module/module.js +37 -0
  50. package/build/cjs/type/not/not.d.ts +1 -1
  51. package/build/cjs/type/not/not.js +2 -2
  52. package/build/cjs/type/omit/omit-from-mapped-key.d.ts +6 -6
  53. package/build/cjs/type/omit/omit-from-mapped-key.js +10 -12
  54. package/build/cjs/type/omit/omit-from-mapped-result.d.ts +5 -5
  55. package/build/cjs/type/omit/omit-from-mapped-result.js +10 -10
  56. package/build/cjs/type/omit/omit.d.ts +26 -14
  57. package/build/cjs/type/omit/omit.js +46 -35
  58. package/build/cjs/type/partial/partial.d.ts +11 -7
  59. package/build/cjs/type/partial/partial.js +36 -24
  60. package/build/cjs/type/pick/pick-from-mapped-key.d.ts +6 -6
  61. package/build/cjs/type/pick/pick-from-mapped-key.js +10 -10
  62. package/build/cjs/type/pick/pick-from-mapped-result.d.ts +5 -5
  63. package/build/cjs/type/pick/pick-from-mapped-result.js +10 -10
  64. package/build/cjs/type/pick/pick.d.ts +25 -15
  65. package/build/cjs/type/pick/pick.js +47 -36
  66. package/build/cjs/type/record/record.d.ts +4 -2
  67. package/build/cjs/type/record/record.js +31 -28
  68. package/build/cjs/type/ref/ref.d.ts +4 -7
  69. package/build/cjs/type/ref/ref.js +3 -11
  70. package/build/cjs/type/required/required.d.ts +12 -8
  71. package/build/cjs/type/required/required.js +37 -25
  72. package/build/cjs/type/static/static.d.ts +1 -1
  73. package/build/cjs/type/tuple/tuple.d.ts +1 -1
  74. package/build/cjs/type/tuple/tuple.js +4 -4
  75. package/build/cjs/type/type/javascript.d.ts +10 -10
  76. package/build/cjs/type/type/javascript.js +6 -6
  77. package/build/cjs/type/type/json.d.ts +45 -68
  78. package/build/cjs/type/type/json.js +82 -98
  79. package/build/cjs/type/type/type.d.ts +1 -2
  80. package/build/cjs/type/type/type.js +55 -57
  81. package/build/cjs/type/union/union-evaluated.d.ts +6 -6
  82. package/build/cjs/type/union/union-evaluated.js +11 -10
  83. package/build/cjs/type/union/union.d.ts +1 -1
  84. package/build/cjs/type/union/union.js +4 -4
  85. package/build/cjs/value/cast/cast.js +8 -1
  86. package/build/cjs/value/check/check.js +11 -4
  87. package/build/cjs/value/clean/clean.js +8 -1
  88. package/build/cjs/value/convert/convert.js +7 -0
  89. package/build/cjs/value/create/create.js +7 -0
  90. package/build/cjs/value/default/default.js +7 -0
  91. package/build/cjs/value/transform/decode.js +18 -7
  92. package/build/cjs/value/transform/encode.js +18 -7
  93. package/build/cjs/value/transform/has.js +18 -18
  94. package/build/esm/compiler/compiler.d.mts +2 -2
  95. package/build/esm/compiler/compiler.mjs +13 -2
  96. package/build/esm/errors/errors.mjs +7 -0
  97. package/build/esm/index.d.mts +1 -2
  98. package/build/esm/index.mjs +1 -2
  99. package/build/esm/syntax/parse.d.mts +14 -15
  100. package/build/esm/syntax/parse.mjs +6 -5
  101. package/build/esm/syntax/runtime.d.mts +20 -4
  102. package/build/esm/syntax/runtime.mjs +183 -28
  103. package/build/esm/syntax/static.d.mts +107 -23
  104. package/build/esm/type/array/array.d.mts +1 -1
  105. package/build/esm/type/awaited/awaited.d.mts +8 -3
  106. package/build/esm/type/awaited/awaited.mjs +20 -20
  107. package/build/esm/type/computed/computed.d.mts +9 -0
  108. package/build/esm/type/computed/computed.mjs +6 -0
  109. package/build/esm/type/computed/index.d.mts +1 -0
  110. package/build/esm/type/computed/index.mjs +1 -0
  111. package/build/esm/type/guard/kind.d.mts +12 -4
  112. package/build/esm/type/guard/kind.mjs +12 -0
  113. package/build/esm/type/guard/type.d.mts +9 -3
  114. package/build/esm/type/guard/type.mjs +17 -0
  115. package/build/esm/type/index.d.mts +1 -2
  116. package/build/esm/type/index.mjs +1 -2
  117. package/build/esm/type/indexed/indexed-from-mapped-key.d.mts +6 -6
  118. package/build/esm/type/indexed/indexed-from-mapped-key.mjs +10 -10
  119. package/build/esm/type/indexed/indexed-from-mapped-result.d.mts +5 -5
  120. package/build/esm/type/indexed/indexed-from-mapped-result.mjs +11 -10
  121. package/build/esm/type/indexed/indexed-property-keys.d.mts +6 -6
  122. package/build/esm/type/indexed/indexed-property-keys.mjs +14 -14
  123. package/build/esm/type/indexed/indexed.d.mts +34 -20
  124. package/build/esm/type/indexed/indexed.mjs +55 -42
  125. package/build/esm/type/intersect/intersect-evaluated.d.mts +6 -6
  126. package/build/esm/type/intersect/intersect-evaluated.mjs +16 -16
  127. package/build/esm/type/intersect/intersect.d.mts +2 -2
  128. package/build/esm/type/intersect/intersect.mjs +6 -6
  129. package/build/esm/type/keyof/keyof-from-mapped-result.d.mts +6 -6
  130. package/build/esm/type/keyof/keyof-from-mapped-result.mjs +10 -10
  131. package/build/esm/type/keyof/keyof-property-keys.d.mts +7 -7
  132. package/build/esm/type/keyof/keyof-property-keys.mjs +22 -22
  133. package/build/esm/type/keyof/keyof.d.mts +11 -6
  134. package/build/esm/type/keyof/keyof.mjs +22 -13
  135. package/build/esm/type/module/compute.d.mts +54 -0
  136. package/build/esm/type/module/compute.mjs +148 -0
  137. package/build/esm/type/module/index.d.mts +1 -0
  138. package/build/esm/type/module/index.mjs +1 -0
  139. package/build/esm/type/module/infer.d.mts +43 -0
  140. package/build/esm/type/module/infer.mjs +1 -0
  141. package/build/esm/type/module/module.d.mts +27 -0
  142. package/build/esm/type/module/module.mjs +31 -0
  143. package/build/esm/type/not/not.d.mts +1 -1
  144. package/build/esm/type/not/not.mjs +2 -2
  145. package/build/esm/type/omit/omit-from-mapped-key.d.mts +6 -6
  146. package/build/esm/type/omit/omit-from-mapped-key.mjs +10 -12
  147. package/build/esm/type/omit/omit-from-mapped-result.d.mts +5 -5
  148. package/build/esm/type/omit/omit-from-mapped-result.mjs +10 -10
  149. package/build/esm/type/omit/omit.d.mts +26 -14
  150. package/build/esm/type/omit/omit.mjs +43 -32
  151. package/build/esm/type/partial/partial.d.mts +11 -7
  152. package/build/esm/type/partial/partial.mjs +29 -17
  153. package/build/esm/type/pick/pick-from-mapped-key.d.mts +6 -6
  154. package/build/esm/type/pick/pick-from-mapped-key.mjs +10 -10
  155. package/build/esm/type/pick/pick-from-mapped-result.d.mts +5 -5
  156. package/build/esm/type/pick/pick-from-mapped-result.mjs +10 -10
  157. package/build/esm/type/pick/pick.d.mts +25 -15
  158. package/build/esm/type/pick/pick.mjs +43 -32
  159. package/build/esm/type/record/record.d.mts +4 -2
  160. package/build/esm/type/record/record.mjs +15 -12
  161. package/build/esm/type/ref/ref.d.mts +4 -7
  162. package/build/esm/type/ref/ref.mjs +3 -11
  163. package/build/esm/type/required/required.d.mts +12 -8
  164. package/build/esm/type/required/required.mjs +33 -21
  165. package/build/esm/type/static/static.d.mts +1 -1
  166. package/build/esm/type/tuple/tuple.d.mts +1 -1
  167. package/build/esm/type/tuple/tuple.mjs +4 -4
  168. package/build/esm/type/type/javascript.d.mts +10 -10
  169. package/build/esm/type/type/javascript.mjs +6 -6
  170. package/build/esm/type/type/json.d.mts +45 -68
  171. package/build/esm/type/type/json.mjs +47 -63
  172. package/build/esm/type/type/type.d.mts +1 -2
  173. package/build/esm/type/type/type.mjs +1 -2
  174. package/build/esm/type/union/union-evaluated.d.mts +6 -6
  175. package/build/esm/type/union/union-evaluated.mjs +11 -10
  176. package/build/esm/type/union/union.d.mts +1 -1
  177. package/build/esm/type/union/union.mjs +4 -4
  178. package/build/esm/value/cast/cast.mjs +9 -2
  179. package/build/esm/value/check/check.mjs +11 -4
  180. package/build/esm/value/clean/clean.mjs +9 -2
  181. package/build/esm/value/convert/convert.mjs +7 -0
  182. package/build/esm/value/create/create.mjs +7 -0
  183. package/build/esm/value/default/default.mjs +7 -0
  184. package/build/esm/value/transform/decode.mjs +13 -2
  185. package/build/esm/value/transform/encode.mjs +13 -2
  186. package/build/esm/value/transform/has.mjs +2 -2
  187. package/package.json +1 -1
  188. package/readme.md +230 -97
  189. package/build/cjs/type/deref/deref.d.ts +0 -22
  190. package/build/cjs/type/deref/deref.js +0 -106
  191. package/build/cjs/type/deref/index.d.ts +0 -1
  192. package/build/cjs/type/strict/index.d.ts +0 -1
  193. package/build/cjs/type/strict/strict.d.ts +0 -12
  194. package/build/cjs/type/strict/strict.js +0 -16
  195. package/build/esm/type/deref/deref.d.mts +0 -22
  196. package/build/esm/type/deref/deref.mjs +0 -102
  197. package/build/esm/type/deref/index.d.mts +0 -1
  198. package/build/esm/type/deref/index.mjs +0 -1
  199. package/build/esm/type/strict/index.d.mts +0 -1
  200. package/build/esm/type/strict/index.mjs +0 -1
  201. package/build/esm/type/strict/strict.d.mts +0 -12
  202. package/build/esm/type/strict/strict.mjs +0 -12
@@ -2,19 +2,20 @@ import { MappedResult } from '../mapped/index.mjs';
2
2
  import { IndexPropertyKeys } from './indexed-property-keys.mjs';
3
3
  import { Index } from './index.mjs';
4
4
  // prettier-ignore
5
- function FromProperties(T, P, options) {
6
- const Acc = {};
7
- for (const K2 of Object.getOwnPropertyNames(P)) {
8
- Acc[K2] = Index(T, IndexPropertyKeys(P[K2]), options);
5
+ function FromProperties(type, properties, options) {
6
+ const result = {};
7
+ for (const K2 of Object.getOwnPropertyNames(properties)) {
8
+ const keys = IndexPropertyKeys(properties[K2]);
9
+ result[K2] = Index(type, keys, options);
9
10
  }
10
- return Acc;
11
+ return result;
11
12
  }
12
13
  // prettier-ignore
13
- function FromMappedResult(T, R, options) {
14
- return FromProperties(T, R.properties, options);
14
+ function FromMappedResult(type, mappedResult, options) {
15
+ return FromProperties(type, mappedResult.properties, options);
15
16
  }
16
17
  // prettier-ignore
17
- export function IndexFromMappedResult(T, R, options) {
18
- const P = FromMappedResult(T, R, options);
19
- return MappedResult(P);
18
+ export function IndexFromMappedResult(type, mappedResult, options) {
19
+ const properties = FromMappedResult(type, mappedResult, options);
20
+ return MappedResult(properties);
20
21
  }
@@ -4,11 +4,11 @@ import type { TInteger } from '../integer/index.mjs';
4
4
  import type { TNumber } from '../number/index.mjs';
5
5
  import type { TSchema } from '../schema/index.mjs';
6
6
  import type { TUnion } from '../union/index.mjs';
7
- type TFromTemplateLiteral<T extends TTemplateLiteral, R extends string[] = TTemplateLiteralGenerate<T>> = (R);
8
- type TFromUnion<T extends TSchema[], Acc extends string[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, [...Acc, ...TIndexPropertyKeys<L>]> : Acc);
9
- type TFromLiteral<T extends TLiteralValue> = (T extends PropertyKey ? [`${T}`] : []);
10
- export type TIndexPropertyKeys<T extends TSchema> = (T extends TTemplateLiteral ? TFromTemplateLiteral<T> : T extends TUnion<infer S> ? TFromUnion<S> : T extends TLiteral<infer S> ? TFromLiteral<S> : T extends TNumber ? ['[number]'] : T extends TInteger ? ['[number]'] : [
11
- ]);
7
+ type TFromTemplateLiteral<TemplateLiteral extends TTemplateLiteral, Result extends string[] = TTemplateLiteralGenerate<TemplateLiteral>> = Result;
8
+ type TFromUnion<Types extends TSchema[], Result extends string[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromUnion<Right, [...Result, ...TIndexPropertyKeys<Left>]> : Result);
9
+ type TFromLiteral<LiteralValue extends TLiteralValue> = (LiteralValue extends PropertyKey ? [`${LiteralValue}`] : []);
10
+ export type TIndexPropertyKeys<Type extends TSchema, Result extends PropertyKey[] = (Type extends TTemplateLiteral ? TFromTemplateLiteral<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<Types> : Type extends TLiteral<infer LiteralValue extends TLiteralValue> ? TFromLiteral<LiteralValue> : Type extends TNumber ? ['[number]'] : Type extends TInteger ? ['[number]'] : [
11
+ ])> = Result;
12
12
  /** Returns a tuple of PropertyKeys derived from the given TSchema */
13
- export declare function IndexPropertyKeys<T extends TSchema>(T: T): TIndexPropertyKeys<T>;
13
+ export declare function IndexPropertyKeys<Type extends TSchema>(type: Type): TIndexPropertyKeys<Type>;
14
14
  export {};
@@ -4,16 +4,16 @@ import { TemplateLiteralGenerate } from '../template-literal/index.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  import { IsTemplateLiteral, IsUnion, IsLiteral, IsNumber, IsInteger } from '../guard/kind.mjs';
6
6
  // prettier-ignore
7
- function FromTemplateLiteral(T) {
8
- const R = TemplateLiteralGenerate(T);
9
- return R.map(S => S.toString());
7
+ function FromTemplateLiteral(templateLiteral) {
8
+ const result = TemplateLiteralGenerate(templateLiteral);
9
+ return result.map(S => S.toString());
10
10
  }
11
11
  // prettier-ignore
12
- function FromUnion(T) {
13
- const Acc = [];
14
- for (const L of T)
15
- Acc.push(...IndexPropertyKeys(L));
16
- return Acc;
12
+ function FromUnion(type) {
13
+ const result = [];
14
+ for (const left of type)
15
+ result.push(...IndexPropertyKeys(left));
16
+ return result;
17
17
  }
18
18
  // prettier-ignore
19
19
  function FromLiteral(T) {
@@ -22,11 +22,11 @@ function FromLiteral(T) {
22
22
  }
23
23
  /** Returns a tuple of PropertyKeys derived from the given TSchema */
24
24
  // prettier-ignore
25
- export function IndexPropertyKeys(T) {
26
- return [...new Set((IsTemplateLiteral(T) ? FromTemplateLiteral(T) :
27
- IsUnion(T) ? FromUnion(T.anyOf) :
28
- IsLiteral(T) ? FromLiteral(T.const) :
29
- IsNumber(T) ? ['[number]'] :
30
- IsInteger(T) ? ['[number]'] :
25
+ export function IndexPropertyKeys(type) {
26
+ return [...new Set((IsTemplateLiteral(type) ? FromTemplateLiteral(type) :
27
+ IsUnion(type) ? FromUnion(type.anyOf) :
28
+ IsLiteral(type) ? FromLiteral(type.const) :
29
+ IsNumber(type) ? ['[number]'] :
30
+ IsInteger(type) ? ['[number]'] :
31
31
  []))];
32
32
  }
@@ -1,4 +1,6 @@
1
1
  import { type TSchema, SchemaOptions } from '../schema/index.mjs';
2
+ import { type TComputed } from '../computed/index.mjs';
3
+ import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
2
4
  import { type TObject, type TProperties } from '../object/index.mjs';
3
5
  import { type Assert } from '../helpers/index.mjs';
4
6
  import { type TNever } from '../never/index.mjs';
@@ -8,33 +10,45 @@ import { TMappedResult, type TMappedKey } from '../mapped/index.mjs';
8
10
  import { type TUnion } from '../union/index.mjs';
9
11
  import { type TTuple } from '../tuple/index.mjs';
10
12
  import { type TArray } from '../array/index.mjs';
13
+ import { type TRef } from '../ref/index.mjs';
11
14
  import { type TIntersectEvaluated } from '../intersect/index.mjs';
12
15
  import { type TUnionEvaluated } from '../union/index.mjs';
13
16
  import { type TIndexPropertyKeys } from './indexed-property-keys.mjs';
14
17
  import { type TIndexFromMappedKey } from './indexed-from-mapped-key.mjs';
15
18
  import { type TIndexFromMappedResult } from './indexed-from-mapped-result.mjs';
16
- type TFromRest<T extends TSchema[], K extends PropertyKey, Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, K, [...Acc, Assert<TIndexFromPropertyKey<L, K>, TSchema>]> : Acc);
17
- type TFromIntersectRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? L extends TNever ? TFromIntersectRest<R, [...Acc]> : TFromIntersectRest<R, [...Acc, L]> : Acc);
18
- type TFromIntersect<T extends TSchema[], K extends PropertyKey> = (TIntersectEvaluated<TFromIntersectRest<TFromRest<T, K>>>);
19
- type TFromUnionRest<T extends TSchema[], Acc extends TSchema[] = []> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? L extends TNever ? [] : TFromUnionRest<R, [L, ...Acc]> : Acc;
20
- type TFromUnion<T extends TSchema[], K extends PropertyKey> = (TUnionEvaluated<TFromUnionRest<TFromRest<T, K>>>);
21
- type TFromTuple<T extends TSchema[], K extends PropertyKey> = (K extends keyof T ? T[K] : K extends '[number]' ? TUnionEvaluated<T> : TNever);
22
- type TFromArray<T extends TSchema, K extends PropertyKey> = (K extends '[number]' ? T : TNever);
23
- type AssertPropertyKey<T> = Assert<T, string | number>;
24
- type TFromProperty<T extends TProperties, K extends PropertyKey> = (K extends keyof T ? T[K] : `${AssertPropertyKey<K>}` extends `${AssertPropertyKey<keyof T>}` ? T[AssertPropertyKey<K>] : TNever);
25
- export type TIndexFromPropertyKey<T extends TSchema, K extends PropertyKey> = (T extends TRecursive<infer S> ? TIndexFromPropertyKey<S, K> : T extends TIntersect<infer S> ? TFromIntersect<S, K> : T extends TUnion<infer S> ? TFromUnion<S, K> : T extends TTuple<infer S> ? TFromTuple<S, K> : T extends TArray<infer S> ? TFromArray<S, K> : T extends TObject<infer S> ? TFromProperty<S, K> : TNever);
26
- export declare function IndexFromPropertyKey<T extends TSchema, K extends PropertyKey>(T: T, K: K): TIndexFromPropertyKey<T, K>;
27
- export type TIndexFromPropertyKeys<T extends TSchema, K extends PropertyKey[], Acc extends TSchema[] = []> = (K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? TIndexFromPropertyKeys<T, R, [...Acc, Assert<TIndexFromPropertyKey<T, L>, TSchema>]> : Acc);
28
- export declare function IndexFromPropertyKeys<T extends TSchema, K extends PropertyKey[]>(T: T, K: [...K]): TIndexFromPropertyKeys<T, K>;
29
- type FromSchema<T extends TSchema, K extends PropertyKey[]> = (TUnionEvaluated<TIndexFromPropertyKeys<T, K>>);
30
- declare function FromSchema<T extends TSchema, K extends PropertyKey[]>(T: T, K: [...K]): FromSchema<T, K>;
31
- export type TIndex<T extends TSchema, K extends PropertyKey[]> = (FromSchema<T, K>);
19
+ type TFromRest<T extends TSchema[], K extends PropertyKey, Result extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, K, [...Result, Assert<TIndexFromPropertyKey<L, K>, TSchema>]> : Result);
20
+ type TFromIntersectRest<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TNever ? TFromIntersectRest<Right, [...Result]> : TFromIntersectRest<Right, [...Result, Left]> : Result);
21
+ type TFromIntersect<Types extends TSchema[], Key extends PropertyKey> = (TIntersectEvaluated<TFromIntersectRest<TFromRest<Types, Key>>>);
22
+ type TFromUnionRest<Types extends TSchema[], Result extends TSchema[] = []> = Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TNever ? [] : TFromUnionRest<Right, [Left, ...Result]> : Result;
23
+ type TFromUnion<Types extends TSchema[], Key extends PropertyKey> = (TUnionEvaluated<TFromUnionRest<TFromRest<Types, Key>>>);
24
+ type TFromTuple<Types extends TSchema[], Key extends PropertyKey, Result extends TSchema = (Key extends '[number]' ? TUnionEvaluated<Types> : Key extends keyof Types ? Types[Key] extends infer Type extends TSchema ? Type : TNever : TNever)> = Result;
25
+ type TFromArray<Type extends TSchema, Key extends PropertyKey> = (Key extends '[number]' ? Type : TNever);
26
+ type AssertPropertyKey<T extends unknown> = Assert<T, string | number>;
27
+ type TFromProperty<Properties extends TProperties, Key extends PropertyKey, Result extends TSchema = (Key extends keyof Properties ? Properties[Key] : `${AssertPropertyKey<Key>}` extends `${AssertPropertyKey<keyof Properties>}` ? Properties[AssertPropertyKey<Key>] : TNever)> = Result;
28
+ export type TIndexFromPropertyKey<Type extends TSchema, Key extends PropertyKey> = (Type extends TRecursive<infer S extends TSchema> ? TIndexFromPropertyKey<S, Key> : Type extends TIntersect<infer S extends TSchema[]> ? TFromIntersect<S, Key> : Type extends TUnion<infer S extends TSchema[]> ? TFromUnion<S, Key> : Type extends TTuple<infer S extends TSchema[]> ? TFromTuple<S, Key> : Type extends TArray<infer S extends TSchema> ? TFromArray<S, Key> : Type extends TObject<infer S extends TProperties> ? TFromProperty<S, Key> : TNever);
29
+ export declare function IndexFromPropertyKey<Type extends TSchema, Key extends PropertyKey>(type: Type, key: Key): TIndexFromPropertyKey<Type, Key>;
30
+ export type TIndexFromPropertyKeys<Type extends TSchema, PropertyKeys extends PropertyKey[], Result extends TSchema[] = []> = (PropertyKeys extends [infer Left extends PropertyKey, ...infer Right extends PropertyKey[]] ? TIndexFromPropertyKeys<Type, Right, [...Result, Assert<TIndexFromPropertyKey<Type, Left>, TSchema>]> : Result);
31
+ export declare function IndexFromPropertyKeys<Type extends TSchema, PropertyKeys extends PropertyKey[]>(type: Type, propertyKeys: [...PropertyKeys]): TIndexFromPropertyKeys<Type, PropertyKeys>;
32
+ type TFromType<Type extends TSchema, PropertyKeys extends PropertyKey[], Result extends TSchema[] = TIndexFromPropertyKeys<Type, PropertyKeys>> = TUnionEvaluated<Result>;
33
+ type TUnionFromPropertyKeys<PropertyKeys extends PropertyKey[], Result extends TLiteral[] = []> = (PropertyKeys extends [infer Key extends PropertyKey, ...infer Rest extends PropertyKey[]] ? Key extends TLiteralValue ? TUnionFromPropertyKeys<Rest, [...Result, TLiteral<Key>]> : TUnionFromPropertyKeys<Rest, [...Result]> : TUnionEvaluated<Result>);
34
+ type TResolvePropertyKeys<Key extends TSchema | PropertyKey[]> = Key extends TSchema ? TIndexPropertyKeys<Key> : Key;
35
+ type TResolveTypeKey<Key extends TSchema | PropertyKey[]> = Key extends PropertyKey[] ? TUnionFromPropertyKeys<Key> : Key;
36
+ export type TIndex<Type extends TSchema, Key extends TSchema | PropertyKey[], IsTypeRef extends boolean = Type extends TRef ? true : false, IsKeyRef extends boolean = Key extends TRef ? true : false> = (Key extends TMappedResult ? TIndexFromMappedResult<Type, Key> : Key extends TMappedKey ? TIndexFromMappedKey<Type, Key> : [
37
+ IsTypeRef,
38
+ IsKeyRef
39
+ ] extends [true, true] ? TComputed<'Index', [Type, TResolveTypeKey<Key>]> : [
40
+ IsTypeRef,
41
+ IsKeyRef
42
+ ] extends [false, true] ? TComputed<'Index', [Type, TResolveTypeKey<Key>]> : [
43
+ IsTypeRef,
44
+ IsKeyRef
45
+ ] extends [true, false] ? TComputed<'Index', [Type, TResolveTypeKey<Key>]> : TFromType<Type, TResolvePropertyKeys<Key>>);
32
46
  /** `[Json]` Returns an Indexed property type for the given keys */
33
- export declare function Index<T extends TSchema, K extends TMappedResult>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedResult<T, K>;
47
+ export declare function Index<Type extends TSchema, Key extends PropertyKey[]>(type: Type, key: readonly [...Key], options?: SchemaOptions): TIndex<Type, Key>;
34
48
  /** `[Json]` Returns an Indexed property type for the given keys */
35
- export declare function Index<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedKey<T, K>;
49
+ export declare function Index<Type extends TSchema, Key extends TMappedKey>(type: Type, key: Key, options?: SchemaOptions): TIndex<Type, Key>;
36
50
  /** `[Json]` Returns an Indexed property type for the given keys */
37
- export declare function Index<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TIndex<T, I>;
51
+ export declare function Index<Type extends TSchema, Key extends TMappedResult>(type: Type, key: Key, options?: SchemaOptions): TIndex<Type, Key>;
38
52
  /** `[Json]` Returns an Indexed property type for the given keys */
39
- export declare function Index<T extends TSchema, K extends PropertyKey[]>(T: T, K: readonly [...K], options?: SchemaOptions): TIndex<T, K>;
53
+ export declare function Index<Type extends TSchema, Key extends TSchema>(type: Type, key: Key, options?: SchemaOptions): TIndex<Type, Key>;
40
54
  export {};
@@ -1,78 +1,91 @@
1
1
  import { CreateType } from '../create/type.mjs';
2
+ import { Computed } from '../computed/index.mjs';
3
+ import { Literal } from '../literal/index.mjs';
2
4
  import { Never } from '../never/index.mjs';
3
5
  import { IntersectEvaluated } from '../intersect/index.mjs';
4
6
  import { UnionEvaluated } from '../union/index.mjs';
7
+ // ------------------------------------------------------------------
8
+ // Infrastructure
9
+ // ------------------------------------------------------------------
5
10
  import { IndexPropertyKeys } from './indexed-property-keys.mjs';
6
11
  import { IndexFromMappedKey } from './indexed-from-mapped-key.mjs';
7
12
  import { IndexFromMappedResult } from './indexed-from-mapped-result.mjs';
8
13
  // ------------------------------------------------------------------
9
- // TypeGuard
14
+ // KindGuard
10
15
  // ------------------------------------------------------------------
11
- import { IsArray, IsIntersect, IsObject, IsMappedKey, IsMappedResult, IsNever, IsSchema, IsTuple, IsUnion } from '../guard/kind.mjs';
16
+ import { IsArray, IsIntersect, IsObject, IsMappedKey, IsMappedResult, IsNever, IsSchema, IsTuple, IsUnion, IsLiteralValue, IsRef } from '../guard/kind.mjs';
17
+ import { IsArray as IsArrayValue } from '../guard/value.mjs';
12
18
  // prettier-ignore
13
- function FromRest(T, K) {
14
- return T.map(L => IndexFromPropertyKey(L, K));
19
+ function FromRest(types, key) {
20
+ return types.map(left => IndexFromPropertyKey(left, key));
15
21
  }
16
22
  // prettier-ignore
17
- function FromIntersectRest(T) {
18
- return T.filter(L => !IsNever(L));
23
+ function FromIntersectRest(types) {
24
+ return types.filter(left => !IsNever(left));
19
25
  }
20
26
  // prettier-ignore
21
- function FromIntersect(T, K) {
22
- return (IntersectEvaluated(FromIntersectRest(FromRest(T, K))));
27
+ function FromIntersect(types, key) {
28
+ return (IntersectEvaluated(FromIntersectRest(FromRest(types, key))));
23
29
  }
24
30
  // prettier-ignore
25
- function FromUnionRest(T) {
26
- return (T.some(L => IsNever(L))
31
+ function FromUnionRest(types) {
32
+ return (types.some(L => IsNever(L))
27
33
  ? []
28
- : T);
34
+ : types);
29
35
  }
30
36
  // prettier-ignore
31
- function FromUnion(T, K) {
32
- return (UnionEvaluated(FromUnionRest(FromRest(T, K))));
37
+ function FromUnion(types, key) {
38
+ return (UnionEvaluated(FromUnionRest(FromRest(types, key))));
33
39
  }
34
40
  // prettier-ignore
35
- function FromTuple(T, K) {
36
- return (K in T ? T[K] :
37
- K === '[number]' ? UnionEvaluated(T) :
41
+ function FromTuple(types, key) {
42
+ return (key === '[number]' ? UnionEvaluated(types) :
43
+ key in types ? types[key] :
38
44
  Never());
39
45
  }
40
46
  // prettier-ignore
41
- function FromArray(T, K) {
42
- return (K === '[number]'
43
- ? T
44
- : Never());
47
+ function FromArray(type, key) {
48
+ // ... ?
49
+ return (key === '[number]' ? type : Never());
45
50
  }
46
51
  // prettier-ignore
47
- function FromProperty(T, K) {
48
- return (K in T ? T[K] : Never());
52
+ function FromProperty(properties, key) {
53
+ return (key in properties ? properties[key] : Never());
49
54
  }
50
55
  // prettier-ignore
51
- export function IndexFromPropertyKey(T, K) {
52
- return (IsIntersect(T) ? FromIntersect(T.allOf, K) :
53
- IsUnion(T) ? FromUnion(T.anyOf, K) :
54
- IsTuple(T) ? FromTuple(T.items ?? [], K) :
55
- IsArray(T) ? FromArray(T.items, K) :
56
- IsObject(T) ? FromProperty(T.properties, K) :
56
+ export function IndexFromPropertyKey(type, key) {
57
+ return (IsIntersect(type) ? FromIntersect(type.allOf, key) :
58
+ IsUnion(type) ? FromUnion(type.anyOf, key) :
59
+ IsTuple(type) ? FromTuple(type.items ?? [], key) :
60
+ IsArray(type) ? FromArray(type.items, key) :
61
+ IsObject(type) ? FromProperty(type.properties, key) :
57
62
  Never());
58
63
  }
59
64
  // prettier-ignore
60
- export function IndexFromPropertyKeys(T, K) {
61
- return K.map(L => IndexFromPropertyKey(T, L));
65
+ export function IndexFromPropertyKeys(type, propertyKeys) {
66
+ return propertyKeys.map(left => IndexFromPropertyKey(type, left));
62
67
  }
63
68
  // prettier-ignore
64
- function FromSchema(T, K) {
65
- return (UnionEvaluated(IndexFromPropertyKeys(T, K)));
69
+ function FromType(type, propertyKeys) {
70
+ const result = IndexFromPropertyKeys(type, propertyKeys);
71
+ return UnionEvaluated(result);
72
+ }
73
+ // prettier-ignore
74
+ function UnionFromPropertyKeys(propertyKeys) {
75
+ const result = propertyKeys.reduce((result, key) => IsLiteralValue(key) ? [...result, Literal(key)] : result, []);
76
+ return UnionEvaluated(result);
66
77
  }
67
78
  /** `[Json]` Returns an Indexed property type for the given keys */
68
- export function Index(T, K, options) {
69
- // mapped-types
70
- if (IsMappedResult(K))
71
- return IndexFromMappedResult(T, K, options);
72
- if (IsMappedKey(K))
73
- return IndexFromMappedKey(T, K, options);
74
- // prettier-ignore
75
- return CreateType(IsSchema(K)
76
- ? FromSchema(T, IndexPropertyKeys(K))
77
- : FromSchema(T, K), options);
79
+ // prettier-ignore
80
+ export function Index(type, key, options) {
81
+ const typeKey = IsArrayValue(key) ? UnionFromPropertyKeys(key) : key;
82
+ const propertyKeys = IsSchema(key) ? IndexPropertyKeys(key) : key;
83
+ const isTypeRef = IsRef(type);
84
+ const isKeyRef = IsRef(key);
85
+ return (IsMappedResult(key) ? IndexFromMappedResult(type, key, options) :
86
+ IsMappedKey(key) ? IndexFromMappedKey(type, key, options) :
87
+ (isTypeRef && isKeyRef) ? Computed('Index', [type, typeKey], options) :
88
+ (!isTypeRef && isKeyRef) ? Computed('Index', [type, typeKey], options) :
89
+ (isTypeRef && !isKeyRef) ? Computed('Index', [type, typeKey], options) :
90
+ CreateType(FromType(type, propertyKeys), options));
78
91
  }
@@ -3,11 +3,11 @@ import { type TNever } from '../never/index.mjs';
3
3
  import { type TOptional } from '../optional/index.mjs';
4
4
  import type { TReadonly } from '../readonly/index.mjs';
5
5
  import { TIntersect, IntersectOptions } from './intersect-type.mjs';
6
- type TIsIntersectOptional<T extends TSchema[]> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? L extends TOptional<TSchema> ? TIsIntersectOptional<R> : false : true);
7
- type TRemoveOptionalFromType<T extends TSchema> = (T extends TReadonly<infer S extends TSchema> ? TReadonly<TRemoveOptionalFromType<S>> : T extends TOptional<infer S extends TSchema> ? TRemoveOptionalFromType<S> : T);
8
- type TRemoveOptionalFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? L extends TOptional<infer S extends TSchema> ? TRemoveOptionalFromRest<R, [...Acc, TRemoveOptionalFromType<S>]> : TRemoveOptionalFromRest<R, [...Acc, L]> : Acc);
9
- type TResolveIntersect<T extends TSchema[]> = (TIsIntersectOptional<T> extends true ? TOptional<TIntersect<TRemoveOptionalFromRest<T>>> : TIntersect<TRemoveOptionalFromRest<T>>);
10
- export type TIntersectEvaluated<T extends TSchema[]> = (T extends [] ? TNever : T extends [TSchema] ? T[0] : TResolveIntersect<T>);
6
+ type TIsIntersectOptional<Types extends TSchema[]> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TOptional<TSchema> ? TIsIntersectOptional<Right> : false : true);
7
+ type TRemoveOptionalFromType<Type extends TSchema> = (Type extends TReadonly<infer Type extends TSchema> ? TReadonly<TRemoveOptionalFromType<Type>> : Type extends TOptional<infer Type extends TSchema> ? TRemoveOptionalFromType<Type> : Type);
8
+ type TRemoveOptionalFromRest<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TOptional<infer Type extends TSchema> ? TRemoveOptionalFromRest<Right, [...Result, TRemoveOptionalFromType<Type>]> : TRemoveOptionalFromRest<Right, [...Result, Left]> : Result);
9
+ type TResolveIntersect<Types extends TSchema[]> = (TIsIntersectOptional<Types> extends true ? TOptional<TIntersect<TRemoveOptionalFromRest<Types>>> : TIntersect<TRemoveOptionalFromRest<Types>>);
10
+ export type TIntersectEvaluated<Types extends TSchema[]> = (Types extends [TSchema] ? Types[0] : Types extends [] ? TNever : TResolveIntersect<Types>);
11
11
  /** `[Json]` Creates an evaluated Intersect type */
12
- export declare function IntersectEvaluated<T extends TSchema[], R = TIntersectEvaluated<T>>(T: [...T], options?: IntersectOptions): R;
12
+ export declare function IntersectEvaluated<Types extends TSchema[], Result extends TSchema = TIntersectEvaluated<Types>>(types: [...Types], options?: IntersectOptions): Result;
13
13
  export {};
@@ -9,30 +9,30 @@ import { IntersectCreate } from './intersect-create.mjs';
9
9
  // ------------------------------------------------------------------
10
10
  import { IsOptional, IsTransform } from '../guard/kind.mjs';
11
11
  // prettier-ignore
12
- function IsIntersectOptional(T) {
13
- return T.every(L => IsOptional(L));
12
+ function IsIntersectOptional(types) {
13
+ return types.every(left => IsOptional(left));
14
14
  }
15
15
  // prettier-ignore
16
- function RemoveOptionalFromType(T) {
17
- return (Discard(T, [OptionalKind]));
16
+ function RemoveOptionalFromType(type) {
17
+ return (Discard(type, [OptionalKind]));
18
18
  }
19
19
  // prettier-ignore
20
- function RemoveOptionalFromRest(T) {
21
- return T.map(L => IsOptional(L) ? RemoveOptionalFromType(L) : L);
20
+ function RemoveOptionalFromRest(types) {
21
+ return types.map(left => IsOptional(left) ? RemoveOptionalFromType(left) : left);
22
22
  }
23
23
  // prettier-ignore
24
- function ResolveIntersect(T, options) {
25
- return (IsIntersectOptional(T)
26
- ? Optional(IntersectCreate(RemoveOptionalFromRest(T), options))
27
- : IntersectCreate(RemoveOptionalFromRest(T), options));
24
+ function ResolveIntersect(types, options) {
25
+ return (IsIntersectOptional(types)
26
+ ? Optional(IntersectCreate(RemoveOptionalFromRest(types), options))
27
+ : IntersectCreate(RemoveOptionalFromRest(types), options));
28
28
  }
29
29
  /** `[Json]` Creates an evaluated Intersect type */
30
- export function IntersectEvaluated(T, options = {}) {
31
- if (T.length === 0)
30
+ export function IntersectEvaluated(types, options = {}) {
31
+ if (types.length === 1)
32
+ return CreateType(types[0], options);
33
+ if (types.length === 0)
32
34
  return Never(options);
33
- if (T.length === 1)
34
- return CreateType(T[0], options);
35
- if (T.some((schema) => IsTransform(schema)))
35
+ if (types.some((schema) => IsTransform(schema)))
36
36
  throw new Error('Cannot intersect transform types');
37
- return ResolveIntersect(T, options);
37
+ return ResolveIntersect(types, options);
38
38
  }
@@ -1,6 +1,6 @@
1
1
  import type { TSchema } from '../schema/index.mjs';
2
2
  import { type TNever } from '../never/index.mjs';
3
3
  import { TIntersect, IntersectOptions } from './intersect-type.mjs';
4
- export type Intersect<T extends TSchema[]> = (T extends [] ? TNever : T extends [TSchema] ? T[0] : TIntersect<T>);
4
+ export type Intersect<Types extends TSchema[]> = (Types extends [TSchema] ? Types[0] : Types extends [] ? TNever : TIntersect<Types>);
5
5
  /** `[Json]` Creates an evaluated Intersect type */
6
- export declare function Intersect<T extends TSchema[]>(T: [...T], options?: IntersectOptions): Intersect<T>;
6
+ export declare function Intersect<Types extends TSchema[]>(types: [...Types], options?: IntersectOptions): Intersect<Types>;
@@ -6,12 +6,12 @@ import { IntersectCreate } from './intersect-create.mjs';
6
6
  // ------------------------------------------------------------------
7
7
  import { IsTransform } from '../guard/kind.mjs';
8
8
  /** `[Json]` Creates an evaluated Intersect type */
9
- export function Intersect(T, options) {
10
- if (T.length === 0)
9
+ export function Intersect(types, options) {
10
+ if (types.length === 1)
11
+ return CreateType(types[0], options);
12
+ if (types.length === 0)
11
13
  return Never(options);
12
- if (T.length === 1)
13
- return CreateType(T[0], options);
14
- if (T.some((schema) => IsTransform(schema)))
14
+ if (types.some((schema) => IsTransform(schema)))
15
15
  throw new Error('Cannot intersect transform types');
16
- return IntersectCreate(T, options);
16
+ return IntersectCreate(types, options);
17
17
  }
@@ -2,11 +2,11 @@ import type { SchemaOptions } from '../schema/index.mjs';
2
2
  import type { Ensure, Evaluate } from '../helpers/index.mjs';
3
3
  import type { TProperties } from '../object/index.mjs';
4
4
  import { type TMappedResult } from '../mapped/index.mjs';
5
- import { type TKeyOf } from './keyof.mjs';
6
- type TFromProperties<K extends TProperties> = ({
7
- [K2 in keyof K]: TKeyOf<K[K2]>;
5
+ import { type TKeyOfFromType } from './keyof.mjs';
6
+ type TFromProperties<Properties extends TProperties> = ({
7
+ [K2 in keyof Properties]: TKeyOfFromType<Properties[K2]>;
8
8
  });
9
- type TFromMappedResult<R extends TMappedResult> = (Evaluate<TFromProperties<R['properties']>>);
10
- export type TKeyOfFromMappedResult<R extends TMappedResult, P extends TProperties = TFromMappedResult<R>> = (Ensure<TMappedResult<P>>);
11
- export declare function KeyOfFromMappedResult<R extends TMappedResult, P extends TProperties = TFromMappedResult<R>>(R: R, options?: SchemaOptions): TMappedResult<P>;
9
+ type TFromMappedResult<MappedResult extends TMappedResult> = (Evaluate<TFromProperties<MappedResult['properties']>>);
10
+ export type TKeyOfFromMappedResult<MappedResult extends TMappedResult, Properties extends TProperties = TFromMappedResult<MappedResult>> = (Ensure<TMappedResult<Properties>>);
11
+ export declare function KeyOfFromMappedResult<MappedResult extends TMappedResult, Properties extends TProperties = TFromMappedResult<MappedResult>>(mappedResult: MappedResult, options?: SchemaOptions): TMappedResult<Properties>;
12
12
  export {};
@@ -2,18 +2,18 @@ import { MappedResult } from '../mapped/index.mjs';
2
2
  import { KeyOf } from './keyof.mjs';
3
3
  import { Clone } from '../clone/value.mjs';
4
4
  // prettier-ignore
5
- function FromProperties(K, options) {
6
- const Acc = {};
7
- for (const K2 of globalThis.Object.getOwnPropertyNames(K))
8
- Acc[K2] = KeyOf(K[K2], Clone(options));
9
- return Acc;
5
+ function FromProperties(properties, options) {
6
+ const result = {};
7
+ for (const K2 of globalThis.Object.getOwnPropertyNames(properties))
8
+ result[K2] = KeyOf(properties[K2], Clone(options));
9
+ return result;
10
10
  }
11
11
  // prettier-ignore
12
- function FromMappedResult(R, options) {
13
- return FromProperties(R.properties, options);
12
+ function FromMappedResult(mappedResult, options) {
13
+ return FromProperties(mappedResult.properties, options);
14
14
  }
15
15
  // prettier-ignore
16
- export function KeyOfFromMappedResult(R, options) {
17
- const P = FromMappedResult(R, options);
18
- return MappedResult(P);
16
+ export function KeyOfFromMappedResult(mappedResult, options) {
17
+ const properties = FromMappedResult(mappedResult, options);
18
+ return MappedResult(properties);
19
19
  }
@@ -7,18 +7,18 @@ import type { TTuple } from '../tuple/index.mjs';
7
7
  import type { TArray } from '../array/index.mjs';
8
8
  import type { TObject, TProperties } from '../object/index.mjs';
9
9
  import { type TSetUnionMany, type TSetIntersectMany } from '../sets/index.mjs';
10
- type TFromRest<T extends TSchema[], Acc extends PropertyKey[][] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Acc, TKeyOfPropertyKeys<L>]> : Acc);
11
- type TFromIntersect<T extends TSchema[], C extends PropertyKey[][] = TFromRest<T>, R extends PropertyKey[] = TSetUnionMany<C>> = R;
12
- type TFromUnion<T extends TSchema[], C extends PropertyKey[][] = TFromRest<T>, R extends PropertyKey[] = TSetIntersectMany<C>> = R;
13
- type TFromTuple<T extends TSchema[], I extends string = ZeroString, Acc extends PropertyKey[] = []> = T extends [infer _ extends TSchema, ...infer R extends TSchema[]] ? TFromTuple<R, TIncrement<I>, [...Acc, I]> : Acc;
10
+ type TFromRest<Types extends TSchema[], Result extends PropertyKey[][] = []> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Result, TKeyOfPropertyKeys<L>]> : Result);
11
+ type TFromIntersect<Types extends TSchema[], PropertyKeysArray extends PropertyKey[][] = TFromRest<Types>, PropertyKeys extends PropertyKey[] = TSetUnionMany<PropertyKeysArray>> = PropertyKeys;
12
+ type TFromUnion<Types extends TSchema[], PropertyKeysArray extends PropertyKey[][] = TFromRest<Types>, PropertyKeys extends PropertyKey[] = TSetIntersectMany<PropertyKeysArray>> = PropertyKeys;
13
+ type TFromTuple<Types extends TSchema[], Indexer extends string = ZeroString, Acc extends PropertyKey[] = []> = Types extends [infer _ extends TSchema, ...infer R extends TSchema[]] ? TFromTuple<R, TIncrement<Indexer>, [...Acc, Indexer]> : Acc;
14
14
  type TFromArray<_ extends TSchema> = ([
15
15
  '[number]'
16
16
  ]);
17
- type TFromProperties<T extends TProperties> = (UnionToTuple<keyof T>);
18
- export type TKeyOfPropertyKeys<T extends TSchema> = (T extends TRecursive<infer S> ? TKeyOfPropertyKeys<S> : T extends TIntersect<infer S> ? TFromIntersect<S> : T extends TUnion<infer S> ? TFromUnion<S> : T extends TTuple<infer S> ? TFromTuple<S> : T extends TArray<infer S> ? TFromArray<S> : T extends TObject<infer S> ? TFromProperties<S> : [
17
+ type TFromProperties<Properties extends TProperties> = (UnionToTuple<keyof Properties>);
18
+ export type TKeyOfPropertyKeys<Type extends TSchema> = (Type extends TRecursive<infer Type extends TSchema> ? TKeyOfPropertyKeys<Type> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<Types> : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<Types> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<Types> : Type extends TArray<infer Type extends TSchema> ? TFromArray<Type> : Type extends TObject<infer Properties extends TProperties> ? TFromProperties<Properties> : [
19
19
  ]);
20
20
  /** Returns a tuple of PropertyKeys derived from the given TSchema. */
21
- export declare function KeyOfPropertyKeys<T extends TSchema>(T: T): TKeyOfPropertyKeys<T>;
21
+ export declare function KeyOfPropertyKeys<Type extends TSchema>(type: Type): TKeyOfPropertyKeys<Type>;
22
22
  /** Returns a regular expression pattern derived from the given TSchema */
23
23
  export declare function KeyOfPattern(schema: TSchema): string;
24
24
  export {};
@@ -4,27 +4,27 @@ import { SetUnionMany, SetIntersectMany } from '../sets/index.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  import { IsIntersect, IsUnion, IsTuple, IsArray, IsObject, IsRecord } from '../guard/kind.mjs';
6
6
  // prettier-ignore
7
- function FromRest(T) {
8
- const Acc = [];
9
- for (const L of T)
10
- Acc.push(KeyOfPropertyKeys(L));
11
- return Acc;
7
+ function FromRest(types) {
8
+ const result = [];
9
+ for (const L of types)
10
+ result.push(KeyOfPropertyKeys(L));
11
+ return result;
12
12
  }
13
13
  // prettier-ignore
14
- function FromIntersect(T) {
15
- const C = FromRest(T);
16
- const R = SetUnionMany(C);
17
- return R;
14
+ function FromIntersect(types) {
15
+ const propertyKeysArray = FromRest(types);
16
+ const propertyKeys = SetUnionMany(propertyKeysArray);
17
+ return propertyKeys;
18
18
  }
19
19
  // prettier-ignore
20
- function FromUnion(T) {
21
- const C = FromRest(T);
22
- const R = SetIntersectMany(C);
23
- return R;
20
+ function FromUnion(types) {
21
+ const propertyKeysArray = FromRest(types);
22
+ const propertyKeys = SetIntersectMany(propertyKeysArray);
23
+ return propertyKeys;
24
24
  }
25
25
  // prettier-ignore
26
- function FromTuple(T) {
27
- return T.map((_, I) => I.toString());
26
+ function FromTuple(types) {
27
+ return types.map((_, indexer) => indexer.toString());
28
28
  }
29
29
  // prettier-ignore
30
30
  function FromArray(_) {
@@ -50,13 +50,13 @@ function FromPatternProperties(patternProperties) {
50
50
  }
51
51
  /** Returns a tuple of PropertyKeys derived from the given TSchema. */
52
52
  // prettier-ignore
53
- export function KeyOfPropertyKeys(T) {
54
- return (IsIntersect(T) ? FromIntersect(T.allOf) :
55
- IsUnion(T) ? FromUnion(T.anyOf) :
56
- IsTuple(T) ? FromTuple(T.items ?? []) :
57
- IsArray(T) ? FromArray(T.items) :
58
- IsObject(T) ? FromProperties(T.properties) :
59
- IsRecord(T) ? FromPatternProperties(T.patternProperties) :
53
+ export function KeyOfPropertyKeys(type) {
54
+ return (IsIntersect(type) ? FromIntersect(type.allOf) :
55
+ IsUnion(type) ? FromUnion(type.anyOf) :
56
+ IsTuple(type) ? FromTuple(type.items ?? []) :
57
+ IsArray(type) ? FromArray(type.items) :
58
+ IsObject(type) ? FromProperties(type.properties) :
59
+ IsRecord(type) ? FromPatternProperties(type.patternProperties) :
60
60
  []);
61
61
  }
62
62
  // ----------------------------------------------------------------
@@ -4,13 +4,18 @@ import type { TMappedResult } from '../mapped/index.mjs';
4
4
  import type { SchemaOptions } from '../schema/index.mjs';
5
5
  import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
6
6
  import { type TNumber } from '../number/index.mjs';
7
+ import { TComputed } from '../computed/index.mjs';
8
+ import { type TRef } from '../ref/index.mjs';
7
9
  import { type TKeyOfPropertyKeys } from './keyof-property-keys.mjs';
8
10
  import { type TUnionEvaluated } from '../union/index.mjs';
9
11
  import { type TKeyOfFromMappedResult } from './keyof-from-mapped-result.mjs';
10
- export type TKeyOfPropertyKeysToRest<T extends PropertyKey[], Acc extends TSchema[] = []> = (T extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? L extends '[number]' ? TKeyOfPropertyKeysToRest<R, [...Acc, TNumber]> : TKeyOfPropertyKeysToRest<R, [...Acc, TLiteral<Assert<L, TLiteralValue>>]> : Acc);
11
- export declare function KeyOfPropertyKeysToRest<T extends PropertyKey[]>(T: [...T]): TKeyOfPropertyKeysToRest<T>;
12
- export type TKeyOf<T extends TSchema, K extends PropertyKey[] = TKeyOfPropertyKeys<T>, S extends TSchema[] = TKeyOfPropertyKeysToRest<K>, U = TUnionEvaluated<S>> = (Ensure<U>);
12
+ type TFromComputed<Target extends string, Parameters extends TSchema[]> = Ensure<TComputed<'KeyOf', [TComputed<Target, Parameters>]>>;
13
+ type TFromRef<Ref extends string> = Ensure<TComputed<'KeyOf', [TRef<Ref>]>>;
14
+ /** `[Internal]` Used by KeyOfFromMappedResult */
15
+ export type TKeyOfFromType<Type extends TSchema, PropertyKeys extends PropertyKey[] = TKeyOfPropertyKeys<Type>, PropertyKeyTypes extends TSchema[] = TKeyOfPropertyKeysToRest<PropertyKeys>, Result = TUnionEvaluated<PropertyKeyTypes>> = Ensure<Result>;
16
+ export type TKeyOfPropertyKeysToRest<PropertyKeys extends PropertyKey[], Result extends TSchema[] = []> = (PropertyKeys extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? L extends '[number]' ? TKeyOfPropertyKeysToRest<R, [...Result, TNumber]> : TKeyOfPropertyKeysToRest<R, [...Result, TLiteral<Assert<L, TLiteralValue>>]> : Result);
17
+ export declare function KeyOfPropertyKeysToRest<PropertyKeys extends PropertyKey[]>(propertyKeys: [...PropertyKeys]): TKeyOfPropertyKeysToRest<PropertyKeys>;
18
+ export type TKeyOf<Type extends TSchema> = (Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<Target, Parameters> : Type extends TRef<infer Ref extends string> ? TFromRef<Ref> : Type extends TMappedResult ? TKeyOfFromMappedResult<Type> : TKeyOfFromType<Type>);
13
19
  /** `[Json]` Creates a KeyOf type */
14
- export declare function KeyOf<T extends TMappedResult>(T: T, options?: SchemaOptions): TKeyOfFromMappedResult<T>;
15
- /** `[Json]` Creates a KeyOf type */
16
- export declare function KeyOf<T extends TSchema>(T: T, options?: SchemaOptions): TKeyOf<T>;
20
+ export declare function KeyOf<Type extends TSchema>(type: Type, options?: SchemaOptions): TKeyOf<Type>;
21
+ export {};