@sinclair/typebox 0.34.0 → 0.34.2

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 (146) hide show
  1. package/build/cjs/syntax/runtime.d.ts +4 -1
  2. package/build/cjs/syntax/runtime.js +3 -1
  3. package/build/cjs/syntax/static.d.ts +3 -3
  4. package/build/cjs/type/array/array.d.ts +1 -1
  5. package/build/cjs/type/awaited/awaited.d.ts +8 -3
  6. package/build/cjs/type/awaited/awaited.js +20 -20
  7. package/build/cjs/type/computed/computed.d.ts +9 -0
  8. package/build/cjs/type/computed/computed.js +10 -0
  9. package/build/cjs/type/computed/index.d.ts +1 -0
  10. package/build/cjs/type/computed/index.js +18 -0
  11. package/build/cjs/type/guard/kind.d.ts +9 -4
  12. package/build/cjs/type/guard/kind.js +10 -0
  13. package/build/cjs/type/guard/type.d.ts +6 -3
  14. package/build/cjs/type/guard/type.js +6 -0
  15. package/build/cjs/type/indexed/indexed-from-mapped-key.d.ts +6 -6
  16. package/build/cjs/type/indexed/indexed-from-mapped-key.js +10 -10
  17. package/build/cjs/type/indexed/indexed-from-mapped-result.d.ts +5 -5
  18. package/build/cjs/type/indexed/indexed-from-mapped-result.js +11 -10
  19. package/build/cjs/type/indexed/indexed-property-keys.d.ts +6 -6
  20. package/build/cjs/type/indexed/indexed-property-keys.js +14 -14
  21. package/build/cjs/type/indexed/indexed.d.ts +34 -20
  22. package/build/cjs/type/indexed/indexed.js +59 -46
  23. package/build/cjs/type/intersect/intersect-evaluated.d.ts +6 -6
  24. package/build/cjs/type/intersect/intersect-evaluated.js +16 -16
  25. package/build/cjs/type/intersect/intersect.d.ts +2 -2
  26. package/build/cjs/type/intersect/intersect.js +6 -6
  27. package/build/cjs/type/keyof/keyof-from-mapped-result.d.ts +6 -6
  28. package/build/cjs/type/keyof/keyof-from-mapped-result.js +10 -10
  29. package/build/cjs/type/keyof/keyof-property-keys.d.ts +7 -7
  30. package/build/cjs/type/keyof/keyof-property-keys.js +22 -22
  31. package/build/cjs/type/keyof/keyof.d.ts +11 -6
  32. package/build/cjs/type/keyof/keyof.js +22 -13
  33. package/build/cjs/type/module/compute.d.ts +54 -0
  34. package/build/cjs/type/module/compute.js +154 -0
  35. package/build/cjs/type/module/infer.d.ts +43 -0
  36. package/build/cjs/type/module/infer.js +3 -0
  37. package/build/cjs/type/module/module.d.ts +8 -52
  38. package/build/cjs/type/module/module.js +14 -11
  39. package/build/cjs/type/not/not.d.ts +1 -1
  40. package/build/cjs/type/not/not.js +2 -2
  41. package/build/cjs/type/omit/omit-from-mapped-key.d.ts +6 -6
  42. package/build/cjs/type/omit/omit-from-mapped-key.js +10 -12
  43. package/build/cjs/type/omit/omit-from-mapped-result.d.ts +5 -5
  44. package/build/cjs/type/omit/omit-from-mapped-result.js +10 -10
  45. package/build/cjs/type/omit/omit.d.ts +26 -14
  46. package/build/cjs/type/omit/omit.js +46 -35
  47. package/build/cjs/type/partial/partial.d.ts +11 -7
  48. package/build/cjs/type/partial/partial.js +36 -24
  49. package/build/cjs/type/pick/pick-from-mapped-key.d.ts +6 -6
  50. package/build/cjs/type/pick/pick-from-mapped-key.js +10 -10
  51. package/build/cjs/type/pick/pick-from-mapped-result.d.ts +5 -5
  52. package/build/cjs/type/pick/pick-from-mapped-result.js +10 -10
  53. package/build/cjs/type/pick/pick.d.ts +25 -15
  54. package/build/cjs/type/pick/pick.js +47 -36
  55. package/build/cjs/type/record/record.d.ts +4 -2
  56. package/build/cjs/type/record/record.js +31 -28
  57. package/build/cjs/type/required/required.d.ts +12 -8
  58. package/build/cjs/type/required/required.js +37 -25
  59. package/build/cjs/type/tuple/tuple.d.ts +1 -1
  60. package/build/cjs/type/tuple/tuple.js +4 -4
  61. package/build/cjs/type/type/javascript.d.ts +10 -10
  62. package/build/cjs/type/type/javascript.js +6 -6
  63. package/build/cjs/type/type/json.d.ts +41 -51
  64. package/build/cjs/type/type/json.js +40 -40
  65. package/build/cjs/type/union/union-evaluated.d.ts +6 -6
  66. package/build/cjs/type/union/union-evaluated.js +11 -10
  67. package/build/cjs/type/union/union.d.ts +1 -1
  68. package/build/cjs/type/union/union.js +4 -4
  69. package/build/cjs/value/check/check.js +3 -3
  70. package/build/cjs/value/transform/decode.js +7 -7
  71. package/build/cjs/value/transform/encode.js +7 -7
  72. package/build/cjs/value/transform/has.js +18 -18
  73. package/build/esm/syntax/runtime.d.mts +4 -1
  74. package/build/esm/syntax/runtime.mjs +3 -1
  75. package/build/esm/syntax/static.d.mts +3 -3
  76. package/build/esm/type/array/array.d.mts +1 -1
  77. package/build/esm/type/awaited/awaited.d.mts +8 -3
  78. package/build/esm/type/awaited/awaited.mjs +20 -20
  79. package/build/esm/type/computed/computed.d.mts +9 -0
  80. package/build/esm/type/computed/computed.mjs +6 -0
  81. package/build/esm/type/computed/index.d.mts +1 -0
  82. package/build/esm/type/computed/index.mjs +1 -0
  83. package/build/esm/type/guard/kind.d.mts +9 -4
  84. package/build/esm/type/guard/kind.mjs +8 -0
  85. package/build/esm/type/guard/type.d.mts +6 -3
  86. package/build/esm/type/guard/type.mjs +5 -0
  87. package/build/esm/type/indexed/indexed-from-mapped-key.d.mts +6 -6
  88. package/build/esm/type/indexed/indexed-from-mapped-key.mjs +10 -10
  89. package/build/esm/type/indexed/indexed-from-mapped-result.d.mts +5 -5
  90. package/build/esm/type/indexed/indexed-from-mapped-result.mjs +11 -10
  91. package/build/esm/type/indexed/indexed-property-keys.d.mts +6 -6
  92. package/build/esm/type/indexed/indexed-property-keys.mjs +14 -14
  93. package/build/esm/type/indexed/indexed.d.mts +34 -20
  94. package/build/esm/type/indexed/indexed.mjs +55 -42
  95. package/build/esm/type/intersect/intersect-evaluated.d.mts +6 -6
  96. package/build/esm/type/intersect/intersect-evaluated.mjs +16 -16
  97. package/build/esm/type/intersect/intersect.d.mts +2 -2
  98. package/build/esm/type/intersect/intersect.mjs +6 -6
  99. package/build/esm/type/keyof/keyof-from-mapped-result.d.mts +6 -6
  100. package/build/esm/type/keyof/keyof-from-mapped-result.mjs +10 -10
  101. package/build/esm/type/keyof/keyof-property-keys.d.mts +7 -7
  102. package/build/esm/type/keyof/keyof-property-keys.mjs +22 -22
  103. package/build/esm/type/keyof/keyof.d.mts +11 -6
  104. package/build/esm/type/keyof/keyof.mjs +22 -13
  105. package/build/esm/type/module/compute.d.mts +54 -0
  106. package/build/esm/type/module/compute.mjs +148 -0
  107. package/build/esm/type/module/infer.d.mts +43 -0
  108. package/build/esm/type/module/infer.mjs +1 -0
  109. package/build/esm/type/module/module.d.mts +8 -52
  110. package/build/esm/type/module/module.mjs +14 -11
  111. package/build/esm/type/not/not.d.mts +1 -1
  112. package/build/esm/type/not/not.mjs +2 -2
  113. package/build/esm/type/omit/omit-from-mapped-key.d.mts +6 -6
  114. package/build/esm/type/omit/omit-from-mapped-key.mjs +10 -12
  115. package/build/esm/type/omit/omit-from-mapped-result.d.mts +5 -5
  116. package/build/esm/type/omit/omit-from-mapped-result.mjs +10 -10
  117. package/build/esm/type/omit/omit.d.mts +26 -14
  118. package/build/esm/type/omit/omit.mjs +43 -32
  119. package/build/esm/type/partial/partial.d.mts +11 -7
  120. package/build/esm/type/partial/partial.mjs +29 -17
  121. package/build/esm/type/pick/pick-from-mapped-key.d.mts +6 -6
  122. package/build/esm/type/pick/pick-from-mapped-key.mjs +10 -10
  123. package/build/esm/type/pick/pick-from-mapped-result.d.mts +5 -5
  124. package/build/esm/type/pick/pick-from-mapped-result.mjs +10 -10
  125. package/build/esm/type/pick/pick.d.mts +25 -15
  126. package/build/esm/type/pick/pick.mjs +43 -32
  127. package/build/esm/type/record/record.d.mts +4 -2
  128. package/build/esm/type/record/record.mjs +15 -12
  129. package/build/esm/type/required/required.d.mts +12 -8
  130. package/build/esm/type/required/required.mjs +33 -21
  131. package/build/esm/type/tuple/tuple.d.mts +1 -1
  132. package/build/esm/type/tuple/tuple.mjs +4 -4
  133. package/build/esm/type/type/javascript.d.mts +10 -10
  134. package/build/esm/type/type/javascript.mjs +6 -6
  135. package/build/esm/type/type/json.d.mts +41 -51
  136. package/build/esm/type/type/json.mjs +40 -40
  137. package/build/esm/type/union/union-evaluated.d.mts +6 -6
  138. package/build/esm/type/union/union-evaluated.mjs +11 -10
  139. package/build/esm/type/union/union.d.mts +1 -1
  140. package/build/esm/type/union/union.mjs +4 -4
  141. package/build/esm/value/check/check.mjs +2 -2
  142. package/build/esm/value/transform/decode.mjs +2 -2
  143. package/build/esm/value/transform/encode.mjs +2 -2
  144. package/build/esm/value/transform/has.mjs +2 -2
  145. package/package.json +1 -1
  146. package/readme.md +30 -19
@@ -1,60 +1,71 @@
1
1
  import { CreateType } from '../create/type.mjs';
2
2
  import { Discard } from '../discard/discard.mjs';
3
+ import { TransformKind } from '../symbols/symbols.mjs';
4
+ import { Computed } from '../computed/index.mjs';
5
+ import { Literal } from '../literal/index.mjs';
6
+ import { IndexPropertyKeys } from '../indexed/index.mjs';
3
7
  import { Intersect } from '../intersect/index.mjs';
4
8
  import { Union } from '../union/index.mjs';
5
9
  import { Object } from '../object/index.mjs';
6
- import { IndexPropertyKeys } from '../indexed/index.mjs';
10
+ // ------------------------------------------------------------------
11
+ // Mapped
12
+ // ------------------------------------------------------------------
7
13
  import { OmitFromMappedKey } from './omit-from-mapped-key.mjs';
8
14
  import { OmitFromMappedResult } from './omit-from-mapped-result.mjs';
9
- import { TransformKind } from '../symbols/symbols.mjs';
10
15
  // ------------------------------------------------------------------
11
16
  // TypeGuard
12
17
  // ------------------------------------------------------------------
13
- import { IsMappedKey, IsIntersect, IsUnion, IsObject, IsSchema, IsMappedResult } from '../guard/kind.mjs';
18
+ import { IsMappedKey, IsIntersect, IsUnion, IsObject, IsSchema, IsMappedResult, IsLiteralValue, IsRef } from '../guard/kind.mjs';
19
+ import { IsArray as IsArrayValue } from '../guard/value.mjs';
14
20
  // prettier-ignore
15
- function FromIntersect(T, K) {
16
- return T.map((T) => OmitResolve(T, K));
21
+ function FromIntersect(types, propertyKeys) {
22
+ return types.map((type) => OmitResolve(type, propertyKeys));
17
23
  }
18
24
  // prettier-ignore
19
- function FromUnion(T, K) {
20
- return T.map((T) => OmitResolve(T, K));
25
+ function FromUnion(types, propertyKeys) {
26
+ return types.map((type) => OmitResolve(type, propertyKeys));
21
27
  }
22
28
  // ------------------------------------------------------------------
23
29
  // FromProperty
24
30
  // ------------------------------------------------------------------
25
31
  // prettier-ignore
26
- function FromProperty(T, K) {
27
- const { [K]: _, ...R } = T;
32
+ function FromProperty(properties, key) {
33
+ const { [key]: _, ...R } = properties;
28
34
  return R;
29
35
  }
30
36
  // prettier-ignore
31
- function FromProperties(T, K) {
32
- return K.reduce((T, K2) => FromProperty(T, K2), T);
37
+ function FromProperties(properties, propertyKeys) {
38
+ return propertyKeys.reduce((T, K2) => FromProperty(T, K2), properties);
33
39
  }
34
40
  // prettier-ignore
35
- function FromObject(T, K) {
36
- const options = Discard(T, [TransformKind, '$id', 'required', 'properties']);
37
- const properties = FromProperties(T['properties'], K);
38
- return Object(properties, options);
41
+ function FromObject(properties, propertyKeys) {
42
+ const options = Discard(properties, [TransformKind, '$id', 'required', 'properties']);
43
+ const omittedProperties = FromProperties(properties['properties'], propertyKeys);
44
+ return Object(omittedProperties, options);
39
45
  }
40
- // ------------------------------------------------------------------
41
- // OmitResolve
42
- // ------------------------------------------------------------------
43
46
  // prettier-ignore
44
- function OmitResolve(T, K) {
45
- return (IsIntersect(T) ? Intersect(FromIntersect(T.allOf, K)) :
46
- IsUnion(T) ? Union(FromUnion(T.anyOf, K)) :
47
- IsObject(T) ? FromObject(T, K) :
47
+ function UnionFromPropertyKeys(propertyKeys) {
48
+ const result = propertyKeys.reduce((result, key) => IsLiteralValue(key) ? [...result, Literal(key)] : result, []);
49
+ return Union(result);
50
+ }
51
+ // prettier-ignore
52
+ function OmitResolve(properties, propertyKeys) {
53
+ return (IsIntersect(properties) ? Intersect(FromIntersect(properties.allOf, propertyKeys)) :
54
+ IsUnion(properties) ? Union(FromUnion(properties.anyOf, propertyKeys)) :
55
+ IsObject(properties) ? FromObject(properties, propertyKeys) :
48
56
  Object({}));
49
57
  }
50
- export function Omit(T, K, options) {
51
- // mapped
52
- if (IsMappedKey(K))
53
- return OmitFromMappedKey(T, K, options);
54
- if (IsMappedResult(T))
55
- return OmitFromMappedResult(T, K, options);
56
- // non-mapped
57
- const I = IsSchema(K) ? IndexPropertyKeys(K) : K;
58
- // special: mapping types require overridable options
59
- return CreateType({ ...OmitResolve(T, I), ...options });
58
+ /** `[Json]` Constructs a type whose keys are picked from the given type */
59
+ // prettier-ignore
60
+ export function Omit(type, key, options) {
61
+ const typeKey = IsArrayValue(key) ? UnionFromPropertyKeys(key) : key;
62
+ const propertyKeys = IsSchema(key) ? IndexPropertyKeys(key) : key;
63
+ const isTypeRef = IsRef(type);
64
+ const isKeyRef = IsRef(key);
65
+ return (IsMappedResult(type) ? OmitFromMappedResult(type, propertyKeys, options) :
66
+ IsMappedKey(key) ? OmitFromMappedKey(type, key, options) :
67
+ (isTypeRef && isKeyRef) ? Computed('Omit', [type, typeKey], options) :
68
+ (!isTypeRef && isKeyRef) ? Computed('Omit', [type, typeKey], options) :
69
+ (isTypeRef && !isKeyRef) ? Computed('Omit', [type, typeKey], options) :
70
+ CreateType({ ...OmitResolve(type, propertyKeys), ...options }));
60
71
  }
@@ -2,21 +2,25 @@ import type { TSchema, SchemaOptions } from '../schema/index.mjs';
2
2
  import type { Evaluate, Ensure } from '../helpers/index.mjs';
3
3
  import type { TMappedResult } from '../mapped/index.mjs';
4
4
  import { type TReadonlyOptional } from '../readonly-optional/index.mjs';
5
+ import { type TComputed } from '../computed/index.mjs';
5
6
  import { type TOptional } from '../optional/index.mjs';
6
7
  import { type TReadonly } from '../readonly/index.mjs';
7
8
  import { type TRecursive } from '../recursive/index.mjs';
8
9
  import { type TObject, type TProperties } from '../object/index.mjs';
9
10
  import { type TIntersect } from '../intersect/index.mjs';
10
11
  import { type TUnion } from '../union/index.mjs';
12
+ import { type TRef } from '../ref/index.mjs';
11
13
  import { type TPartialFromMappedResult } from './partial-from-mapped-result.mjs';
12
- type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Acc, TPartial<L>]> : Acc);
13
- type TFromProperties<T extends TProperties> = Evaluate<{
14
- [K in keyof T]: T[K] extends (TReadonlyOptional<infer S>) ? TReadonlyOptional<S> : T[K] extends (TReadonly<infer S>) ? TReadonlyOptional<S> : T[K] extends (TOptional<infer S>) ? TOptional<S> : TOptional<T[K]>;
14
+ type TFromComputed<Target extends string, Parameters extends TSchema[]> = Ensure<TComputed<'Partial', [TComputed<Target, Parameters>]>>;
15
+ type TFromRef<Ref extends string> = Ensure<TComputed<'Partial', [TRef<Ref>]>>;
16
+ type TFromProperties<Properties extends TProperties> = Evaluate<{
17
+ [K in keyof Properties]: Properties[K] extends (TReadonlyOptional<infer S>) ? TReadonlyOptional<S> : Properties[K] extends (TReadonly<infer S>) ? TReadonlyOptional<S> : Properties[K] extends (TOptional<infer S>) ? TOptional<S> : TOptional<Properties[K]>;
15
18
  }>;
16
- type TFromObject<T extends TObject, Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
17
- export type TPartial<T extends TSchema> = (T extends TRecursive<infer S extends TSchema> ? TRecursive<TPartial<S>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>> : TObject<{}>);
19
+ type TFromObject<Type extends TObject, Properties extends TProperties = Type['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
20
+ type TFromRest<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Result, TPartial<L>]> : Result);
21
+ export type TPartial<T extends TSchema> = (T extends TRecursive<infer Type extends TSchema> ? TRecursive<TPartial<Type>> : T extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<Target, Parameters> : T extends TRef<infer Ref extends string> ? TFromRef<Ref> : T extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromRest<Types>> : T extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromRest<Types>> : T extends TObject<infer Properties extends TProperties> ? TFromObject<TObject<Properties>> : TObject<{}>);
18
22
  /** `[Json]` Constructs a type where all properties are optional */
19
- export declare function Partial<T extends TMappedResult>(T: T, options?: SchemaOptions): TPartialFromMappedResult<T>;
23
+ export declare function Partial<MappedResult extends TMappedResult>(type: MappedResult, options?: SchemaOptions): TPartialFromMappedResult<MappedResult>;
20
24
  /** `[Json]` Constructs a type where all properties are optional */
21
- export declare function Partial<T extends TSchema>(T: T, options?: SchemaOptions): TPartial<T>;
25
+ export declare function Partial<Type extends TSchema>(type: Type, options?: SchemaOptions): TPartial<Type>;
22
26
  export {};
@@ -1,25 +1,31 @@
1
1
  import { CreateType } from '../create/type.mjs';
2
+ import { Computed } from '../computed/index.mjs';
2
3
  import { Optional } from '../optional/index.mjs';
3
4
  import { Object } from '../object/index.mjs';
4
5
  import { Intersect } from '../intersect/index.mjs';
5
6
  import { Union } from '../union/index.mjs';
7
+ import { Ref } from '../ref/index.mjs';
6
8
  import { Discard } from '../discard/index.mjs';
7
9
  import { TransformKind } from '../symbols/index.mjs';
8
10
  import { PartialFromMappedResult } from './partial-from-mapped-result.mjs';
9
11
  // ------------------------------------------------------------------
10
12
  // TypeGuard
11
13
  // ------------------------------------------------------------------
12
- import { IsMappedResult, IsIntersect, IsUnion, IsObject } from '../guard/kind.mjs';
14
+ import { IsMappedResult, IsIntersect, IsUnion, IsObject, IsRef, IsComputed } from '../guard/kind.mjs';
13
15
  // prettier-ignore
14
- function FromRest(T) {
15
- return T.map(L => PartialResolve(L));
16
+ function FromComputed(target, parameters) {
17
+ return Computed('Partial', [Computed(target, parameters)]);
16
18
  }
17
19
  // prettier-ignore
18
- function FromProperties(T) {
19
- const Acc = {};
20
- for (const K of globalThis.Object.getOwnPropertyNames(T))
21
- Acc[K] = Optional(T[K]);
22
- return Acc;
20
+ function FromRef($ref) {
21
+ return Computed('Partial', [Ref($ref)]);
22
+ }
23
+ // prettier-ignore
24
+ function FromProperties(properties) {
25
+ const partialProperties = {};
26
+ for (const K of globalThis.Object.getOwnPropertyNames(properties))
27
+ partialProperties[K] = Optional(properties[K]);
28
+ return partialProperties;
23
29
  }
24
30
  // prettier-ignore
25
31
  function FromObject(T) {
@@ -27,23 +33,29 @@ function FromObject(T) {
27
33
  const properties = FromProperties(T['properties']);
28
34
  return Object(properties, options);
29
35
  }
36
+ // prettier-ignore
37
+ function FromRest(types) {
38
+ return types.map(type => PartialResolve(type));
39
+ }
30
40
  // ------------------------------------------------------------------
31
41
  // PartialResolve
32
42
  // ------------------------------------------------------------------
33
43
  // prettier-ignore
34
- function PartialResolve(T) {
35
- return (IsIntersect(T) ? Intersect(FromRest(T.allOf)) :
36
- IsUnion(T) ? Union(FromRest(T.anyOf)) :
37
- IsObject(T) ? FromObject(T) :
38
- Object({}));
44
+ function PartialResolve(type) {
45
+ return (IsComputed(type) ? FromComputed(type.target, type.parameters) :
46
+ IsRef(type) ? FromRef(type.$ref) :
47
+ IsIntersect(type) ? Intersect(FromRest(type.allOf)) :
48
+ IsUnion(type) ? Union(FromRest(type.anyOf)) :
49
+ IsObject(type) ? FromObject(type) :
50
+ Object({}));
39
51
  }
40
52
  /** `[Json]` Constructs a type where all properties are optional */
41
- export function Partial(T, options) {
42
- if (IsMappedResult(T)) {
43
- return PartialFromMappedResult(T, options);
53
+ export function Partial(type, options) {
54
+ if (IsMappedResult(type)) {
55
+ return PartialFromMappedResult(type, options);
44
56
  }
45
57
  else {
46
58
  // special: mapping types require overridable options
47
- return CreateType({ ...PartialResolve(T), ...options });
59
+ return CreateType({ ...PartialResolve(type), ...options });
48
60
  }
49
61
  }
@@ -2,11 +2,11 @@ import type { TSchema, SchemaOptions } from '../schema/index.mjs';
2
2
  import type { TProperties } from '../object/index.mjs';
3
3
  import { type TMappedResult, type TMappedKey } from '../mapped/index.mjs';
4
4
  import { type TPick } from './pick.mjs';
5
- type TFromPropertyKey<T extends TSchema, K extends PropertyKey> = {
6
- [_ in K]: TPick<T, [K]>;
5
+ type TFromPropertyKey<Type extends TSchema, Key extends PropertyKey> = {
6
+ [_ in Key]: TPick<Type, [Key]>;
7
7
  };
8
- type TFromPropertyKeys<T extends TSchema, K extends PropertyKey[], Acc extends TProperties = {}> = (K extends [infer LK extends PropertyKey, ...infer RK extends PropertyKey[]] ? TFromPropertyKeys<T, RK, Acc & TFromPropertyKey<T, LK>> : Acc);
9
- type TFromMappedKey<T extends TSchema, K extends TMappedKey> = (TFromPropertyKeys<T, K['keys']>);
10
- export type TPickFromMappedKey<T extends TSchema, K extends TMappedKey, P extends TProperties = TFromMappedKey<T, K>> = (TMappedResult<P>);
11
- export declare function PickFromMappedKey<T extends TSchema, K extends TMappedKey, P extends TProperties = TFromMappedKey<T, K>>(T: T, K: K, options?: SchemaOptions): TMappedResult<P>;
8
+ type TFromPropertyKeys<Type extends TSchema, PropertyKeys extends PropertyKey[], Result extends TProperties = {}> = (PropertyKeys extends [infer LeftKey extends PropertyKey, ...infer RightKeys extends PropertyKey[]] ? TFromPropertyKeys<Type, RightKeys, Result & TFromPropertyKey<Type, LeftKey>> : Result);
9
+ type TFromMappedKey<Type extends TSchema, MappedKey extends TMappedKey> = (TFromPropertyKeys<Type, MappedKey['keys']>);
10
+ export type TPickFromMappedKey<Type extends TSchema, MappedKey extends TMappedKey, Properties extends TProperties = TFromMappedKey<Type, MappedKey>> = (TMappedResult<Properties>);
11
+ export declare function PickFromMappedKey<Type extends TSchema, MappedKey extends TMappedKey, Properties extends TProperties = TFromMappedKey<Type, MappedKey>>(type: Type, mappedKey: MappedKey, options?: SchemaOptions): TMappedResult<Properties>;
12
12
  export {};
@@ -2,23 +2,23 @@ import { MappedResult } from '../mapped/index.mjs';
2
2
  import { Pick } from './pick.mjs';
3
3
  import { Clone } from '../clone/value.mjs';
4
4
  // prettier-ignore
5
- function FromPropertyKey(T, K, options) {
5
+ function FromPropertyKey(type, key, options) {
6
6
  return {
7
- [K]: Pick(T, [K], Clone(options))
7
+ [key]: Pick(type, [key], Clone(options))
8
8
  };
9
9
  }
10
10
  // prettier-ignore
11
- function FromPropertyKeys(T, K, options) {
12
- return K.reduce((Acc, LK) => {
13
- return { ...Acc, ...FromPropertyKey(T, LK, options) };
11
+ function FromPropertyKeys(type, propertyKeys, options) {
12
+ return propertyKeys.reduce((result, leftKey) => {
13
+ return { ...result, ...FromPropertyKey(type, leftKey, options) };
14
14
  }, {});
15
15
  }
16
16
  // prettier-ignore
17
- function FromMappedKey(T, K, options) {
18
- return FromPropertyKeys(T, K.keys, options);
17
+ function FromMappedKey(type, mappedKey, options) {
18
+ return FromPropertyKeys(type, mappedKey.keys, options);
19
19
  }
20
20
  // prettier-ignore
21
- export function PickFromMappedKey(T, K, options) {
22
- const P = FromMappedKey(T, K, options);
23
- return MappedResult(P);
21
+ export function PickFromMappedKey(type, mappedKey, options) {
22
+ const properties = FromMappedKey(type, mappedKey, options);
23
+ return MappedResult(properties);
24
24
  }
@@ -3,10 +3,10 @@ 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
5
  import { type TPick } from './pick.mjs';
6
- type TFromProperties<P extends TProperties, K extends PropertyKey[]> = ({
7
- [K2 in keyof P]: TPick<P[K2], K>;
6
+ type TFromProperties<Properties extends TProperties, PropertyKeys extends PropertyKey[]> = ({
7
+ [K2 in keyof Properties]: TPick<Properties[K2], PropertyKeys>;
8
8
  });
9
- type TFromMappedResult<R extends TMappedResult, K extends PropertyKey[]> = (Evaluate<TFromProperties<R['properties'], K>>);
10
- export type TPickFromMappedResult<T extends TMappedResult, K extends PropertyKey[], P extends TProperties = TFromMappedResult<T, K>> = (Ensure<TMappedResult<P>>);
11
- export declare function PickFromMappedResult<R extends TMappedResult, K extends PropertyKey[], P extends TProperties = TFromMappedResult<R, K>>(R: R, K: [...K], options?: SchemaOptions): TMappedResult<P>;
9
+ type TFromMappedResult<MappedResult extends TMappedResult, PropertyKeys extends PropertyKey[]> = (Evaluate<TFromProperties<MappedResult['properties'], PropertyKeys>>);
10
+ export type TPickFromMappedResult<MappedResult extends TMappedResult, PropertyKeys extends PropertyKey[], Properties extends TProperties = TFromMappedResult<MappedResult, PropertyKeys>> = (Ensure<TMappedResult<Properties>>);
11
+ export declare function PickFromMappedResult<MappedResult extends TMappedResult, PropertyKeys extends PropertyKey[], Properties extends TProperties = TFromMappedResult<MappedResult, PropertyKeys>>(mappedResult: MappedResult, propertyKeys: [...PropertyKeys], options?: SchemaOptions): TMappedResult<Properties>;
12
12
  export {};
@@ -2,18 +2,18 @@ import { MappedResult } from '../mapped/index.mjs';
2
2
  import { Pick } from './pick.mjs';
3
3
  import { Clone } from '../clone/value.mjs';
4
4
  // prettier-ignore
5
- function FromProperties(P, K, options) {
6
- const Acc = {};
7
- for (const K2 of globalThis.Object.getOwnPropertyNames(P))
8
- Acc[K2] = Pick(P[K2], K, Clone(options));
9
- return Acc;
5
+ function FromProperties(properties, propertyKeys, options) {
6
+ const result = {};
7
+ for (const K2 of globalThis.Object.getOwnPropertyNames(properties))
8
+ result[K2] = Pick(properties[K2], propertyKeys, Clone(options));
9
+ return result;
10
10
  }
11
11
  // prettier-ignore
12
- function FromMappedResult(R, K, options) {
13
- return FromProperties(R.properties, K, options);
12
+ function FromMappedResult(mappedResult, propertyKeys, options) {
13
+ return FromProperties(mappedResult.properties, propertyKeys, options);
14
14
  }
15
15
  // prettier-ignore
16
- export function PickFromMappedResult(R, K, options) {
17
- const P = FromMappedResult(R, K, options);
18
- return MappedResult(P);
16
+ export function PickFromMappedResult(mappedResult, propertyKeys, options) {
17
+ const properties = FromMappedResult(mappedResult, propertyKeys, options);
18
+ return MappedResult(properties);
19
19
  }
@@ -1,26 +1,36 @@
1
1
  import type { TSchema, SchemaOptions } from '../schema/index.mjs';
2
2
  import type { TupleToUnion, Evaluate, Ensure } from '../helpers/index.mjs';
3
3
  import { type TRecursive } from '../recursive/index.mjs';
4
+ import { type TComputed } from '../computed/index.mjs';
4
5
  import { type TIntersect } from '../intersect/index.mjs';
5
- import { type TUnion } from '../union/index.mjs';
6
+ import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
6
7
  import { type TObject, type TProperties } from '../object/index.mjs';
7
- import type { TMappedKey, TMappedResult } from '../mapped/index.mjs';
8
+ import { type TUnion } from '../union/index.mjs';
9
+ import { type TMappedKey, type TMappedResult } from '../mapped/index.mjs';
10
+ import { type TRef } from '../ref/index.mjs';
8
11
  import { type TIndexPropertyKeys } from '../indexed/index.mjs';
9
12
  import { type TPickFromMappedKey } from './pick-from-mapped-key.mjs';
10
13
  import { type TPickFromMappedResult } from './pick-from-mapped-result.mjs';
11
- type FromIntersect<T extends TSchema[], K extends PropertyKey[], Acc extends TSchema[] = []> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? FromIntersect<R, K, [...Acc, TPick<L, K>]> : Acc;
12
- declare function FromIntersect<T extends TSchema[], K extends PropertyKey[]>(T: T, K: K): FromIntersect<T, K>;
13
- type FromUnion<T extends TSchema[], K extends PropertyKey[], Acc extends TSchema[] = []> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? FromUnion<R, K, [...Acc, TPick<L, K>]> : Acc;
14
- declare function FromUnion<T extends TSchema[], K extends PropertyKey[]>(T: T, K: K): FromUnion<T, K>;
15
- type TFromProperties<T extends TProperties, K extends PropertyKey[], I extends PropertyKey = TupleToUnion<K>> = Evaluate<Pick<T, I & keyof T>>;
16
- type TFromObject<T extends TObject, K extends PropertyKey[], Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties, K>)>>;
17
- export type TPick<T extends TProperties, K extends PropertyKey[]> = T extends TRecursive<infer S extends TSchema> ? TRecursive<TPick<S, K>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<FromIntersect<S, K>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<FromUnion<S, K>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>, K> : TObject<{}>;
18
- /** `[Json]` Constructs a type whose keys are picked from the given type */
19
- export declare function Pick<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K], options?: SchemaOptions): TPickFromMappedResult<T, K>;
20
- /** `[Json]` Constructs a type whose keys are picked from the given type */
21
- export declare function Pick<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TPickFromMappedKey<T, K>;
14
+ type TFromIntersect<Types extends TSchema[], PropertyKeys extends PropertyKey[], Result extends TSchema[] = []> = Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromIntersect<R, PropertyKeys, [...Result, TPick<L, PropertyKeys>]> : Result;
15
+ type TFromUnion<Types extends TSchema[], PropertyKeys extends PropertyKey[], Result extends TSchema[] = []> = Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, PropertyKeys, [...Result, TPick<L, PropertyKeys>]> : Result;
16
+ type TFromProperties<Properties extends TProperties, PropertyKeys extends PropertyKey[], UnionKeys extends PropertyKey = TupleToUnion<PropertyKeys>> = (Evaluate<Pick<Properties, UnionKeys & keyof Properties>>);
17
+ type TFromObject<Type extends TObject, Key extends PropertyKey[], Properties extends TProperties = Type['properties']> = Ensure<TObject<(TFromProperties<Properties, Key>)>>;
18
+ 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]> : TUnion<Result>);
19
+ export type TPickResolve<Properties extends TProperties, PropertyKeys extends PropertyKey[]> = (Properties extends TRecursive<infer Types extends TSchema> ? TRecursive<TPickResolve<Types, PropertyKeys>> : Properties extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromIntersect<Types, PropertyKeys>> : Properties extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromUnion<Types, PropertyKeys>> : Properties extends TObject<infer Types extends TProperties> ? TFromObject<TObject<Types>, PropertyKeys> : TObject<{}>);
20
+ type TResolvePropertyKeys<Key extends TSchema | PropertyKey[]> = Key extends TSchema ? TIndexPropertyKeys<Key> : Key;
21
+ type TResolveTypeKey<Key extends TSchema | PropertyKey[]> = Key extends PropertyKey[] ? TUnionFromPropertyKeys<Key> : Key;
22
+ export type TPick<Type extends TSchema, Key extends TSchema | PropertyKey[], IsTypeRef extends boolean = Type extends TRef ? true : false, IsKeyRef extends boolean = Key extends TRef ? true : false> = (Type extends TMappedResult ? TPickFromMappedResult<Type, TResolvePropertyKeys<Key>> : Key extends TMappedKey ? TPickFromMappedKey<Type, Key> : [
23
+ IsTypeRef,
24
+ IsKeyRef
25
+ ] extends [true, true] ? TComputed<'Pick', [Type, TResolveTypeKey<Key>]> : [
26
+ IsTypeRef,
27
+ IsKeyRef
28
+ ] extends [false, true] ? TComputed<'Pick', [Type, TResolveTypeKey<Key>]> : [
29
+ IsTypeRef,
30
+ IsKeyRef
31
+ ] extends [true, false] ? TComputed<'Pick', [Type, TResolveTypeKey<Key>]> : TPickResolve<Type, TResolvePropertyKeys<Key>>);
22
32
  /** `[Json]` Constructs a type whose keys are picked from the given type */
23
- export declare function Pick<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TPick<T, I>;
33
+ export declare function Pick<Type extends TSchema, Key extends PropertyKey[]>(type: Type, key: readonly [...Key], options?: SchemaOptions): TPick<Type, Key>;
24
34
  /** `[Json]` Constructs a type whose keys are picked from the given type */
25
- export declare function Pick<T extends TSchema, K extends PropertyKey[]>(T: T, K: readonly [...K], options?: SchemaOptions): TPick<T, K>;
35
+ export declare function Pick<Type extends TSchema, Key extends TSchema>(type: Type, key: Key, options?: SchemaOptions): TPick<Type, Key>;
26
36
  export {};
@@ -1,30 +1,36 @@
1
1
  import { CreateType } from '../create/type.mjs';
2
2
  import { Discard } from '../discard/discard.mjs';
3
+ import { Computed } from '../computed/index.mjs';
3
4
  import { Intersect } from '../intersect/index.mjs';
4
- import { Union } from '../union/index.mjs';
5
+ import { Literal } from '../literal/index.mjs';
5
6
  import { Object } from '../object/index.mjs';
7
+ import { Union } from '../union/index.mjs';
6
8
  import { IndexPropertyKeys } from '../indexed/index.mjs';
7
- import { PickFromMappedKey } from './pick-from-mapped-key.mjs';
8
- import { PickFromMappedResult } from './pick-from-mapped-result.mjs';
9
9
  import { TransformKind } from '../symbols/symbols.mjs';
10
10
  // ------------------------------------------------------------------
11
- // TypeGuard
11
+ // Guards
12
12
  // ------------------------------------------------------------------
13
- import { IsMappedKey, IsMappedResult, IsIntersect, IsUnion, IsObject, IsSchema } from '../guard/kind.mjs';
14
- function FromIntersect(T, K) {
15
- return T.map((T) => PickResolve(T, K));
13
+ import { IsMappedKey, IsMappedResult, IsIntersect, IsUnion, IsObject, IsSchema, IsLiteralValue, IsRef } from '../guard/kind.mjs';
14
+ import { IsArray as IsArrayValue } from '../guard/value.mjs';
15
+ // ------------------------------------------------------------------
16
+ // Infrastructure
17
+ // ------------------------------------------------------------------
18
+ import { PickFromMappedKey } from './pick-from-mapped-key.mjs';
19
+ import { PickFromMappedResult } from './pick-from-mapped-result.mjs';
20
+ function FromIntersect(types, propertyKeys) {
21
+ return types.map((type) => PickResolve(type, propertyKeys));
16
22
  }
17
23
  // prettier-ignore
18
- function FromUnion(T, K) {
19
- return T.map((T) => PickResolve(T, K));
24
+ function FromUnion(types, propertyKeys) {
25
+ return types.map((type) => PickResolve(type, propertyKeys));
20
26
  }
21
27
  // prettier-ignore
22
- function FromProperties(T, K) {
23
- const Acc = {};
24
- for (const K2 of K)
25
- if (K2 in T)
26
- Acc[K2] = T[K2];
27
- return Acc;
28
+ function FromProperties(properties, propertyKeys) {
29
+ const result = {};
30
+ for (const K2 of propertyKeys)
31
+ if (K2 in properties)
32
+ result[K2] = properties[K2];
33
+ return result;
28
34
  }
29
35
  // prettier-ignore
30
36
  function FromObject(T, K) {
@@ -32,24 +38,29 @@ function FromObject(T, K) {
32
38
  const properties = FromProperties(T['properties'], K);
33
39
  return Object(properties, options);
34
40
  }
35
- // ------------------------------------------------------------------
36
- // PickResolve
37
- // ------------------------------------------------------------------
38
41
  // prettier-ignore
39
- function PickResolve(T, K) {
40
- return (IsIntersect(T) ? Intersect(FromIntersect(T.allOf, K)) :
41
- IsUnion(T) ? Union(FromUnion(T.anyOf, K)) :
42
- IsObject(T) ? FromObject(T, K) :
42
+ function UnionFromPropertyKeys(propertyKeys) {
43
+ const result = propertyKeys.reduce((result, key) => IsLiteralValue(key) ? [...result, Literal(key)] : result, []);
44
+ return Union(result);
45
+ }
46
+ // prettier-ignore
47
+ function PickResolve(properties, propertyKeys) {
48
+ return (IsIntersect(properties) ? Intersect(FromIntersect(properties.allOf, propertyKeys)) :
49
+ IsUnion(properties) ? Union(FromUnion(properties.anyOf, propertyKeys)) :
50
+ IsObject(properties) ? FromObject(properties, propertyKeys) :
43
51
  Object({}));
44
52
  }
45
- export function Pick(T, K, options) {
46
- // mapped
47
- if (IsMappedKey(K))
48
- return PickFromMappedKey(T, K, options);
49
- if (IsMappedResult(T))
50
- return PickFromMappedResult(T, K, options);
51
- // non-mapped
52
- const I = IsSchema(K) ? IndexPropertyKeys(K) : K;
53
- // special: mapping types require overridable options
54
- return CreateType({ ...PickResolve(T, I), ...options });
53
+ /** `[Json]` Constructs a type whose keys are picked from the given type */
54
+ // prettier-ignore
55
+ export function Pick(type, key, options) {
56
+ const typeKey = IsArrayValue(key) ? UnionFromPropertyKeys(key) : key;
57
+ const propertyKeys = IsSchema(key) ? IndexPropertyKeys(key) : key;
58
+ const isTypeRef = IsRef(type);
59
+ const isKeyRef = IsRef(key);
60
+ return (IsMappedResult(type) ? PickFromMappedResult(type, propertyKeys, options) :
61
+ IsMappedKey(key) ? PickFromMappedKey(type, key, options) :
62
+ (isTypeRef && isKeyRef) ? Computed('Pick', [type, typeKey], options) :
63
+ (!isTypeRef && isKeyRef) ? Computed('Pick', [type, typeKey], options) :
64
+ (isTypeRef && !isKeyRef) ? Computed('Pick', [type, typeKey], options) :
65
+ CreateType({ ...PickResolve(type, propertyKeys), ...options }));
55
66
  }
@@ -2,6 +2,7 @@ import type { TSchema } from '../schema/index.mjs';
2
2
  import type { Static } from '../static/index.mjs';
3
3
  import type { Evaluate, Ensure, Assert } from '../helpers/index.mjs';
4
4
  import { type TAny } from '../any/index.mjs';
5
+ import { type TComputed } from '../computed/index.mjs';
5
6
  import { type TObject, type TProperties, type TAdditionalProperties, type ObjectOptions } from '../object/index.mjs';
6
7
  import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
7
8
  import { type TNever } from '../never/index.mjs';
@@ -11,6 +12,7 @@ import { type TString } from '../string/index.mjs';
11
12
  import { type TInteger } from '../integer/index.mjs';
12
13
  import { type TNumber } from '../number/index.mjs';
13
14
  import { type TEnum } from '../enum/index.mjs';
15
+ import { type TRef } from '../ref/index.mjs';
14
16
  import { TIsTemplateLiteralFinite, type TTemplateLiteral } from '../template-literal/index.mjs';
15
17
  import { Kind } from '../symbols/index.mjs';
16
18
  type TFromTemplateLiteralKeyInfinite<K extends TTemplateLiteral, T extends TSchema> = Ensure<TRecord<K, T>>;
@@ -50,7 +52,7 @@ export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchem
50
52
  };
51
53
  additionalProperties: TAdditionalProperties;
52
54
  }
53
- export type TRecordOrObject<K extends TSchema, T extends TSchema> = K extends TTemplateLiteral ? TFromTemplateLiteralKey<K, T> : K extends TEnum<infer S> ? TFromEnumKey<S, T> : K extends TUnion<infer S> ? TFromUnionKey<S, T> : K extends TLiteral<infer S> ? TFromLiteralKey<S, T> : K extends TInteger ? TFromIntegerKey<K, T> : K extends TNumber ? TFromNumberKey<K, T> : K extends TRegExp ? TFromRegExpKey<K, T> : K extends TString ? TFromStringKey<K, T> : K extends TAny ? TFromAnyKey<K, T> : K extends TNever ? TFromNeverKey<K, T> : TNever;
55
+ export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = Type extends TRef ? TComputed<'Record', [Key, Type]> : Key extends TRef ? TComputed<'Record', [Key, Type]> : Key extends TTemplateLiteral ? TFromTemplateLiteralKey<Key, Type> : Key extends TEnum<infer S> ? TFromEnumKey<S, Type> : Key extends TUnion<infer S> ? TFromUnionKey<S, Type> : Key extends TLiteral<infer S> ? TFromLiteralKey<S, Type> : Key extends TInteger ? TFromIntegerKey<Key, Type> : Key extends TNumber ? TFromNumberKey<Key, Type> : Key extends TRegExp ? TFromRegExpKey<Key, Type> : Key extends TString ? TFromStringKey<Key, Type> : Key extends TAny ? TFromAnyKey<Key, Type> : Key extends TNever ? TFromNeverKey<Key, Type> : TNever;
54
56
  /** `[Json]` Creates a Record type */
55
- export declare function Record<K extends TSchema, T extends TSchema>(K: K, T: T, options?: ObjectOptions): TRecordOrObject<K, T>;
57
+ export declare function Record<Key extends TSchema, Type extends TSchema>(key: Key, type: Type, options?: ObjectOptions): TRecordOrObject<Key, Type>;
56
58
  export {};
@@ -1,4 +1,5 @@
1
1
  import { CreateType } from '../create/type.mjs';
2
+ import { Computed } from '../computed/index.mjs';
2
3
  import { Object } from '../object/index.mjs';
3
4
  import { Never } from '../never/index.mjs';
4
5
  import { Union } from '../union/index.mjs';
@@ -13,7 +14,7 @@ import { IsUndefined } from '../guard/value.mjs';
13
14
  // ------------------------------------------------------------------
14
15
  // TypeGuard
15
16
  // ------------------------------------------------------------------
16
- import { IsInteger, IsLiteral, IsAny, IsNever, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion } from '../guard/kind.mjs';
17
+ import { IsInteger, IsLiteral, IsAny, IsNever, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion, IsRef } from '../guard/kind.mjs';
17
18
  // ------------------------------------------------------------------
18
19
  // RecordCreateFromPattern
19
20
  // ------------------------------------------------------------------
@@ -78,16 +79,18 @@ function FromNumberKey(_, T, options) {
78
79
  // TRecordOrObject
79
80
  // ------------------------------------------------------------------
80
81
  /** `[Json]` Creates a Record type */
81
- export function Record(K, T, options = {}) {
82
+ export function Record(key, type, options = {}) {
82
83
  // prettier-ignore
83
- return (IsUnion(K) ? FromUnionKey(K.anyOf, T, options) :
84
- IsTemplateLiteral(K) ? FromTemplateLiteralKey(K, T, options) :
85
- IsLiteral(K) ? FromLiteralKey(K.const, T, options) :
86
- IsInteger(K) ? FromIntegerKey(K, T, options) :
87
- IsNumber(K) ? FromNumberKey(K, T, options) :
88
- IsRegExp(K) ? FromRegExpKey(K, T, options) :
89
- IsString(K) ? FromStringKey(K, T, options) :
90
- IsAny(K) ? FromAnyKey(K, T, options) :
91
- IsNever(K) ? FromNeverKey(K, T, options) :
92
- Never(options));
84
+ return (IsRef(type) ? Computed('Record', [key, type]) :
85
+ IsRef(key) ? Computed('Record', [key, type]) :
86
+ IsUnion(key) ? FromUnionKey(key.anyOf, type, options) :
87
+ IsTemplateLiteral(key) ? FromTemplateLiteralKey(key, type, options) :
88
+ IsLiteral(key) ? FromLiteralKey(key.const, type, options) :
89
+ IsInteger(key) ? FromIntegerKey(key, type, options) :
90
+ IsNumber(key) ? FromNumberKey(key, type, options) :
91
+ IsRegExp(key) ? FromRegExpKey(key, type, options) :
92
+ IsString(key) ? FromStringKey(key, type, options) :
93
+ IsAny(key) ? FromAnyKey(key, type, options) :
94
+ IsNever(key) ? FromNeverKey(key, type, options) :
95
+ Never(options));
93
96
  }
@@ -2,21 +2,25 @@ import type { TSchema, SchemaOptions } from '../schema/index.mjs';
2
2
  import type { Evaluate, Ensure } from '../helpers/index.mjs';
3
3
  import type { TMappedResult } from '../mapped/index.mjs';
4
4
  import { type TReadonlyOptional } from '../readonly-optional/index.mjs';
5
+ import { type TComputed } from '../computed/index.mjs';
5
6
  import { type TOptional } from '../optional/index.mjs';
6
7
  import { type TReadonly } from '../readonly/index.mjs';
7
8
  import { type TRecursive } from '../recursive/index.mjs';
9
+ import { type TObject, type TProperties } from '../object/index.mjs';
8
10
  import { type TIntersect } from '../intersect/index.mjs';
9
11
  import { type TUnion } from '../union/index.mjs';
10
- import { type TObject, type TProperties } from '../object/index.mjs';
12
+ import { type TRef } from '../ref/index.mjs';
11
13
  import { type TRequiredFromMappedResult } from './required-from-mapped-result.mjs';
12
- type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Acc, TRequired<L>]> : Acc);
13
- type TFromProperties<T extends TProperties> = Evaluate<{
14
- [K in keyof T]: T[K] extends (TReadonlyOptional<infer S>) ? TReadonly<S> : T[K] extends (TReadonly<infer S>) ? TReadonly<S> : T[K] extends (TOptional<infer S>) ? S : T[K];
14
+ type TFromComputed<Target extends string, Parameters extends TSchema[]> = Ensure<TComputed<'Required', [TComputed<Target, Parameters>]>>;
15
+ type TFromRef<Ref extends string> = Ensure<TComputed<'Required', [TRef<Ref>]>>;
16
+ type TFromProperties<Properties extends TProperties> = Evaluate<{
17
+ [K in keyof Properties]: Properties[K] extends (TReadonlyOptional<infer S>) ? TReadonly<S> : Properties[K] extends (TReadonly<infer S>) ? TReadonly<S> : Properties[K] extends (TOptional<infer S>) ? S : Properties[K];
15
18
  }>;
16
- type TFromObject<T extends TObject, Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
17
- export type TRequired<T extends TSchema> = (T extends TRecursive<infer S extends TSchema> ? TRecursive<TRequired<S>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>> : TObject<{}>);
19
+ type TFromObject<Type extends TObject, Properties extends TProperties = Type['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
20
+ type TFromRest<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Result, TRequired<L>]> : Result);
21
+ export type TRequired<Type extends TSchema> = (Type extends TRecursive<infer Type extends TSchema> ? TRecursive<TRequired<Type>> : 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 TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromRest<Types>> : Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromRest<Types>> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<TObject<Properties>> : TObject<{}>);
18
22
  /** `[Json]` Constructs a type where all properties are required */
19
- export declare function Required<T extends TMappedResult>(T: T, options?: SchemaOptions): TRequiredFromMappedResult<T>;
23
+ export declare function Required<MappedResult extends TMappedResult>(type: MappedResult, options?: SchemaOptions): TRequiredFromMappedResult<MappedResult>;
20
24
  /** `[Json]` Constructs a type where all properties are required */
21
- export declare function Required<T extends TSchema>(T: T, options?: SchemaOptions): TRequired<T>;
25
+ export declare function Required<Type extends TSchema>(type: Type, options?: SchemaOptions): TRequired<Type>;
22
26
  export {};