@sinclair/typebox 0.34.16 → 0.34.17

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 (68) hide show
  1. package/build/cjs/compiler/compiler.js +5 -0
  2. package/build/cjs/errors/errors.js +3 -0
  3. package/build/cjs/index.d.ts +3 -0
  4. package/build/cjs/index.js +3 -0
  5. package/build/cjs/syntax/runtime.d.ts +3 -1
  6. package/build/cjs/syntax/runtime.js +33 -3
  7. package/build/cjs/syntax/static.d.ts +22 -2
  8. package/build/cjs/type/argument/argument.d.ts +9 -0
  9. package/build/cjs/type/argument/argument.js +10 -0
  10. package/build/cjs/type/argument/index.d.ts +1 -0
  11. package/build/cjs/type/argument/index.js +18 -0
  12. package/build/cjs/type/guard/kind.d.ts +9 -6
  13. package/build/cjs/type/guard/kind.js +6 -0
  14. package/build/cjs/type/guard/type.d.ts +9 -6
  15. package/build/cjs/type/guard/type.js +9 -0
  16. package/build/cjs/type/index.d.ts +2 -0
  17. package/build/cjs/type/index.js +2 -0
  18. package/build/cjs/type/instantiate/index.d.ts +1 -0
  19. package/build/cjs/type/instantiate/index.js +18 -0
  20. package/build/cjs/type/instantiate/instantiate.d.ts +25 -0
  21. package/build/cjs/type/instantiate/instantiate.js +32 -0
  22. package/build/cjs/type/record/record.d.ts +16 -1
  23. package/build/cjs/type/record/record.js +53 -23
  24. package/build/cjs/type/remap/index.d.ts +1 -0
  25. package/build/cjs/type/remap/index.js +18 -0
  26. package/build/cjs/type/remap/remap.d.ts +19 -0
  27. package/build/cjs/type/remap/remap.js +35 -0
  28. package/build/cjs/type/type/javascript.d.ts +6 -0
  29. package/build/cjs/type/type/javascript.js +44 -34
  30. package/build/cjs/type/type/type.d.ts +3 -0
  31. package/build/cjs/type/type/type.js +123 -117
  32. package/build/cjs/value/check/check.js +5 -0
  33. package/build/cjs/value/create/create.js +5 -0
  34. package/build/esm/compiler/compiler.mjs +5 -0
  35. package/build/esm/errors/errors.mjs +3 -0
  36. package/build/esm/index.d.mts +3 -0
  37. package/build/esm/index.mjs +3 -0
  38. package/build/esm/syntax/runtime.d.mts +3 -1
  39. package/build/esm/syntax/runtime.mjs +33 -3
  40. package/build/esm/syntax/static.d.mts +22 -2
  41. package/build/esm/type/argument/argument.d.mts +9 -0
  42. package/build/esm/type/argument/argument.mjs +6 -0
  43. package/build/esm/type/argument/index.d.mts +1 -0
  44. package/build/esm/type/argument/index.mjs +1 -0
  45. package/build/esm/type/guard/kind.d.mts +9 -6
  46. package/build/esm/type/guard/kind.mjs +5 -0
  47. package/build/esm/type/guard/type.d.mts +9 -6
  48. package/build/esm/type/guard/type.mjs +8 -0
  49. package/build/esm/type/index.d.mts +2 -0
  50. package/build/esm/type/index.mjs +2 -0
  51. package/build/esm/type/instantiate/index.d.mts +1 -0
  52. package/build/esm/type/instantiate/index.mjs +1 -0
  53. package/build/esm/type/instantiate/instantiate.d.mts +25 -0
  54. package/build/esm/type/instantiate/instantiate.mjs +28 -0
  55. package/build/esm/type/record/record.d.mts +16 -1
  56. package/build/esm/type/record/record.mjs +35 -8
  57. package/build/esm/type/remap/index.d.mts +1 -0
  58. package/build/esm/type/remap/index.mjs +1 -0
  59. package/build/esm/type/remap/remap.d.mts +19 -0
  60. package/build/esm/type/remap/remap.mjs +31 -0
  61. package/build/esm/type/type/javascript.d.mts +6 -0
  62. package/build/esm/type/type/javascript.mjs +10 -0
  63. package/build/esm/type/type/type.d.mts +3 -0
  64. package/build/esm/type/type/type.mjs +3 -0
  65. package/build/esm/value/check/check.mjs +5 -0
  66. package/build/esm/value/create/create.mjs +5 -0
  67. package/package.json +1 -1
  68. package/readme.md +36 -39
@@ -12,6 +12,10 @@ export function IsOptional(value) {
12
12
  export function IsAny(value) {
13
13
  return IsKindOf(value, 'Any');
14
14
  }
15
+ /** `[Kind-Only]` Returns true if the given value is TArgument */
16
+ export function IsArgument(value) {
17
+ return IsKindOf(value, 'Argument');
18
+ }
15
19
  /** `[Kind-Only]` Returns true if the given value is TArray */
16
20
  export function IsArray(value) {
17
21
  return IsKindOf(value, 'Array');
@@ -192,6 +196,7 @@ export function IsKind(value) {
192
196
  export function IsSchema(value) {
193
197
  // prettier-ignore
194
198
  return (IsAny(value) ||
199
+ IsArgument(value) ||
195
200
  IsArray(value) ||
196
201
  IsBoolean(value) ||
197
202
  IsBigInt(value) ||
@@ -1,15 +1,12 @@
1
1
  import { Kind, Hint, TransformKind } from '../symbols/index.mjs';
2
2
  import { TypeBoxError } from '../error/index.mjs';
3
3
  import { TransformOptions } from '../transform/index.mjs';
4
- import type { TTemplateLiteral } from '../template-literal/index.mjs';
4
+ import type { TAny } from '../any/index.mjs';
5
+ import type { TArgument } from '../argument/index.mjs';
5
6
  import type { TArray } from '../array/index.mjs';
7
+ import type { TAsyncIterator } from '../async-iterator/index.mjs';
6
8
  import type { TBoolean } from '../boolean/index.mjs';
7
9
  import type { TComputed } from '../computed/index.mjs';
8
- import type { TRecord } from '../record/index.mjs';
9
- import type { TString } from '../string/index.mjs';
10
- import type { TUnion } from '../union/index.mjs';
11
- import type { TAny } from '../any/index.mjs';
12
- import type { TAsyncIterator } from '../async-iterator/index.mjs';
13
10
  import type { TBigInt } from '../bigint/index.mjs';
14
11
  import type { TConstructor } from '../constructor/index.mjs';
15
12
  import type { TFunction } from '../function/index.mjs';
@@ -27,13 +24,17 @@ import type { TObject, TProperties } from '../object/index.mjs';
27
24
  import type { TOptional } from '../optional/index.mjs';
28
25
  import type { TPromise } from '../promise/index.mjs';
29
26
  import type { TReadonly } from '../readonly/index.mjs';
27
+ import type { TRecord } from '../record/index.mjs';
30
28
  import type { TRef } from '../ref/index.mjs';
31
29
  import type { TRegExp } from '../regexp/index.mjs';
32
30
  import type { TSchema } from '../schema/index.mjs';
31
+ import type { TString } from '../string/index.mjs';
33
32
  import type { TSymbol } from '../symbol/index.mjs';
33
+ import type { TTemplateLiteral } from '../template-literal/index.mjs';
34
34
  import type { TTuple } from '../tuple/index.mjs';
35
35
  import type { TUint8Array } from '../uint8array/index.mjs';
36
36
  import type { TUndefined } from '../undefined/index.mjs';
37
+ import type { TUnion } from '../union/index.mjs';
37
38
  import type { TUnknown } from '../unknown/index.mjs';
38
39
  import type { TUnsafe } from '../unsafe/index.mjs';
39
40
  import type { TVoid } from '../void/index.mjs';
@@ -47,6 +48,8 @@ export declare function IsReadonly<T extends TSchema>(value: T): value is TReado
47
48
  export declare function IsOptional<T extends TSchema>(value: T): value is TOptional<T>;
48
49
  /** Returns true if the given value is TAny */
49
50
  export declare function IsAny(value: unknown): value is TAny;
51
+ /** Returns true if the given value is TArgument */
52
+ export declare function IsArgument(value: unknown): value is TArgument;
50
53
  /** Returns true if the given value is TArray */
51
54
  export declare function IsArray(value: unknown): value is TArray;
52
55
  /** Returns true if the given value is TAsyncIterator */
@@ -4,6 +4,7 @@ import { TypeBoxError } from '../error/index.mjs';
4
4
  export class TypeGuardUnknownTypeError extends TypeBoxError {
5
5
  }
6
6
  const KnownTypes = [
7
+ 'Argument',
7
8
  'Any',
8
9
  'Array',
9
10
  'AsyncIterator',
@@ -103,6 +104,12 @@ export function IsAny(value) {
103
104
  return (IsKindOf(value, 'Any') &&
104
105
  IsOptionalString(value.$id));
105
106
  }
107
+ /** Returns true if the given value is TArgument */
108
+ export function IsArgument(value) {
109
+ // prettier-ignore
110
+ return (IsKindOf(value, 'Argument') &&
111
+ ValueGuard.IsNumber(value.index));
112
+ }
106
113
  /** Returns true if the given value is TArray */
107
114
  export function IsArray(value) {
108
115
  return (IsKindOf(value, 'Array') &&
@@ -463,6 +470,7 @@ export function IsKind(value) {
463
470
  export function IsSchema(value) {
464
471
  // prettier-ignore
465
472
  return (ValueGuard.IsObject(value)) && (IsAny(value) ||
473
+ IsArgument(value) ||
466
474
  IsArray(value) ||
467
475
  IsBoolean(value) ||
468
476
  IsBigInt(value) ||
@@ -1,4 +1,5 @@
1
1
  export * from './any/index.mjs';
2
+ export * from './argument/index.mjs';
2
3
  export * from './array/index.mjs';
3
4
  export * from './async-iterator/index.mjs';
4
5
  export * from './awaited/index.mjs';
@@ -21,6 +22,7 @@ export * from './guard/index.mjs';
21
22
  export * from './helpers/index.mjs';
22
23
  export * from './indexed/index.mjs';
23
24
  export * from './instance-type/index.mjs';
25
+ export * from './instantiate/index.mjs';
24
26
  export * from './integer/index.mjs';
25
27
  export * from './intersect/index.mjs';
26
28
  export * from './intrinsic/index.mjs';
@@ -1,4 +1,5 @@
1
1
  export * from './any/index.mjs';
2
+ export * from './argument/index.mjs';
2
3
  export * from './array/index.mjs';
3
4
  export * from './async-iterator/index.mjs';
4
5
  export * from './awaited/index.mjs';
@@ -21,6 +22,7 @@ export * from './guard/index.mjs';
21
22
  export * from './helpers/index.mjs';
22
23
  export * from './indexed/index.mjs';
23
24
  export * from './instance-type/index.mjs';
25
+ export * from './instantiate/index.mjs';
24
26
  export * from './integer/index.mjs';
25
27
  export * from './intersect/index.mjs';
26
28
  export * from './intrinsic/index.mjs';
@@ -0,0 +1 @@
1
+ export * from './instantiate.mjs';
@@ -0,0 +1 @@
1
+ export * from './instantiate.mjs';
@@ -0,0 +1,25 @@
1
+ import type { TSchema } from '../schema/index.mjs';
2
+ import type { TArgument } from '../argument/index.mjs';
3
+ import { type TNever } from '../never/index.mjs';
4
+ import { type TReadonlyOptional } from '../readonly-optional/index.mjs';
5
+ import { type TReadonly } from '../readonly/index.mjs';
6
+ import { type TOptional } from '../optional/index.mjs';
7
+ import { type TRemap, type TMapping as TRemapMapping } from '../remap/index.mjs';
8
+ type TInstantiateArgument<Argument extends TArgument, Type extends TSchema, IsArgumentReadonly extends number = Argument extends TReadonly<TSchema> ? 1 : 0, IsArgumentOptional extends number = Argument extends TOptional<TSchema> ? 1 : 0, Result extends TSchema = ([
9
+ IsArgumentReadonly,
10
+ IsArgumentOptional
11
+ ] extends [1, 1] ? TReadonlyOptional<Type> : [
12
+ IsArgumentReadonly,
13
+ IsArgumentOptional
14
+ ] extends [0, 1] ? TOptional<Type> : [
15
+ IsArgumentReadonly,
16
+ IsArgumentOptional
17
+ ] extends [1, 0] ? TReadonly<Type> : Type)> = Result;
18
+ interface TInstantiateArguments<Arguments extends TSchema[]> extends TRemapMapping {
19
+ output: (this['input'] extends TArgument<infer Index extends number> ? Index extends keyof Arguments ? Arguments[Index] extends TSchema ? TInstantiateArgument<this['input'], Arguments[Index]> : TNever : TNever : this['input']);
20
+ }
21
+ /** `[JavaScript]` Instantiates a type with the given parameters */
22
+ export type TInstantiate<Type extends TSchema, Arguments extends TSchema[]> = (TRemap<Type, TInstantiateArguments<Arguments>>);
23
+ /** `[JavaScript]` Instantiates a type with the given parameters */
24
+ export declare function Instantiate<Type extends TSchema, Arguments extends TSchema[]>(type: Type, args: [...Arguments]): TInstantiate<Type, Arguments>;
25
+ export {};
@@ -0,0 +1,28 @@
1
+ import { Never } from '../never/index.mjs';
2
+ import { ReadonlyOptional } from '../readonly-optional/index.mjs';
3
+ import { Readonly } from '../readonly/index.mjs';
4
+ import { Optional } from '../optional/index.mjs';
5
+ import { Remap } from '../remap/index.mjs';
6
+ import * as KindGuard from '../guard/kind.mjs';
7
+ // prettier-ignore
8
+ function InstantiateArgument(argument, type) {
9
+ const isReadonly = KindGuard.IsReadonly(argument);
10
+ const isOptional = KindGuard.IsOptional(argument);
11
+ return (isReadonly && isOptional ? ReadonlyOptional(type) :
12
+ isReadonly && !isOptional ? Readonly(type) :
13
+ !isReadonly && isOptional ? Optional(type) :
14
+ type);
15
+ }
16
+ /** `[JavaScript]` Instantiates a type with the given parameters */
17
+ // prettier-ignore
18
+ export function Instantiate(type, args) {
19
+ return Remap(type, (type) => {
20
+ return KindGuard.IsArgument(type)
21
+ ? type.index in args
22
+ ? KindGuard.IsSchema(args[type.index])
23
+ ? InstantiateArgument(type, args[type.index])
24
+ : Never()
25
+ : Never()
26
+ : type;
27
+ });
28
+ }
@@ -3,6 +3,7 @@ import type { TSchema } from '../schema/index.mjs';
3
3
  import type { Static } from '../static/index.mjs';
4
4
  import type { Evaluate, Ensure, Assert } from '../helpers/index.mjs';
5
5
  import { type TAny } from '../any/index.mjs';
6
+ import { type TBoolean } from '../boolean/index.mjs';
6
7
  import { type TComputed } from '../computed/index.mjs';
7
8
  import { type TEnumRecord, type TEnum } from '../enum/index.mjs';
8
9
  import { type TInteger } from '../integer/index.mjs';
@@ -38,6 +39,10 @@ type TFromRegExpKey<_Key extends TRegExp, Type extends TSchema> = (Ensure<TRecor
38
39
  type TFromStringKey<_Key extends TString, Type extends TSchema> = (Ensure<TRecord<TString, Type>>);
39
40
  type TFromAnyKey<_Key extends TAny, Type extends TSchema> = (Ensure<TRecord<TAny, Type>>);
40
41
  type TFromNeverKey<_Key extends TNever, Type extends TSchema> = (Ensure<TRecord<TNever, Type>>);
42
+ type TFromBooleanKey<_Key extends TBoolean, Type extends TSchema> = (Ensure<TObject<{
43
+ true: Type;
44
+ false: Type;
45
+ }>>);
41
46
  type TFromIntegerKey<_Key extends TSchema, Type extends TSchema> = (Ensure<TRecord<TNumber, Type>>);
42
47
  type TFromNumberKey<_Key extends TSchema, Type extends TSchema> = (Ensure<TRecord<TNumber, Type>>);
43
48
  type RecordStatic<Key extends TSchema, Type extends TSchema, P extends unknown[]> = (Evaluate<{
@@ -52,7 +57,17 @@ export interface TRecord<Key extends TSchema = TSchema, Type extends TSchema = T
52
57
  };
53
58
  additionalProperties: TAdditionalProperties;
54
59
  }
55
- export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = (Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TComputed<'Record', [Key, TComputed<Target, Parameters>]> : Key extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TComputed<'Record', [TComputed<Target, Parameters>, Type]> : Key extends TRef<infer Ref extends string> ? TComputed<'Record', [TRef<Ref>, Type]> : Key extends TTemplateLiteral ? TFromTemplateLiteralKey<Key, Type> : Key extends TEnum<infer Enum extends TEnumRecord> ? TFromEnumKey<Enum, Type> : Key extends TUnion<infer Types extends TSchema[]> ? TFromUnionKey<Types, Type> : Key extends TLiteral<infer Value extends TLiteralValue> ? TFromLiteralKey<Value, 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);
60
+ export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = (Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TComputed<'Record', [Key, TComputed<Target, Parameters>]> : Key extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TComputed<'Record', [TComputed<Target, Parameters>, Type]> : Key extends TRef<infer Ref extends string> ? TComputed<'Record', [TRef<Ref>, Type]> : Key extends TTemplateLiteral ? TFromTemplateLiteralKey<Key, Type> : Key extends TEnum<infer Enum extends TEnumRecord> ? TFromEnumKey<Enum, Type> : Key extends TUnion<infer Types extends TSchema[]> ? TFromUnionKey<Types, Type> : Key extends TLiteral<infer Value extends TLiteralValue> ? TFromLiteralKey<Value, Type> : Key extends TBoolean ? TFromBooleanKey<Key, 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);
56
61
  /** `[Json]` Creates a Record type */
57
62
  export declare function Record<Key extends TSchema, Type extends TSchema>(key: Key, type: Type, options?: ObjectOptions): TRecordOrObject<Key, Type>;
63
+ /** Gets the Records Pattern */
64
+ export declare function RecordPattern(record: TRecord): string;
65
+ /** Gets the Records Key Type */
66
+ export type TRecordKey<Type extends TRecord> = (Type extends TRecord<infer Key extends TSchema, TSchema> ? (Key extends TNumber ? TNumber : Key extends TString ? TString : TString) : TString);
67
+ /** Gets the Records Key Type */
68
+ export declare function RecordKey<Type extends TRecord>(type: Type): TRecordKey<Type>;
69
+ /** Gets a Record Value Type */
70
+ export type TRecordValue<Type extends TRecord> = (Type extends TRecord<TSchema, infer Value extends TSchema> ? Value : TNever);
71
+ /** Gets a Record Value Type */
72
+ export declare function RecordValue<Type extends TRecord>(type: Type): TRecordValue<Type>;
58
73
  export {};
@@ -2,8 +2,10 @@ import { CreateType } from '../create/type.mjs';
2
2
  import { Kind, Hint } from '../symbols/index.mjs';
3
3
  import { Computed } from '../computed/index.mjs';
4
4
  import { Never } from '../never/index.mjs';
5
+ import { Number } from '../number/index.mjs';
5
6
  import { Object } from '../object/index.mjs';
6
7
  import { Ref } from '../ref/index.mjs';
8
+ import { String } from '../string/index.mjs';
7
9
  import { Union } from '../union/index.mjs';
8
10
  import { IsTemplateLiteralFinite } from '../template-literal/index.mjs';
9
11
  import { PatternStringExact, PatternNumberExact, PatternNeverExact } from '../patterns/index.mjs';
@@ -15,7 +17,7 @@ import { IsUndefined } from '../guard/value.mjs';
15
17
  // ------------------------------------------------------------------
16
18
  // TypeGuard
17
19
  // ------------------------------------------------------------------
18
- import { IsInteger, IsLiteral, IsAny, IsNever, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion, IsRef, IsComputed } from '../guard/kind.mjs';
20
+ import { IsInteger, IsLiteral, IsAny, IsBoolean, IsNever, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion, IsRef, IsComputed } from '../guard/kind.mjs';
19
21
  // ------------------------------------------------------------------
20
22
  // RecordCreateFromPattern
21
23
  // ------------------------------------------------------------------
@@ -65,6 +67,10 @@ function FromNeverKey(_key, type, options) {
65
67
  return RecordCreateFromPattern(PatternNeverExact, type, options);
66
68
  }
67
69
  // prettier-ignore
70
+ function FromBooleanKey(_key, type, options) {
71
+ return Object({ true: type, false: type }, options);
72
+ }
73
+ // prettier-ignore
68
74
  function FromIntegerKey(_key, type, options) {
69
75
  return RecordCreateFromPattern(PatternNumberExact, type, options);
70
76
  }
@@ -84,11 +90,32 @@ export function Record(key, type, options = {}) {
84
90
  IsUnion(key) ? FromUnionKey(key.anyOf, type, options) :
85
91
  IsTemplateLiteral(key) ? FromTemplateLiteralKey(key, type, options) :
86
92
  IsLiteral(key) ? FromLiteralKey(key.const, type, options) :
87
- IsInteger(key) ? FromIntegerKey(key, type, options) :
88
- IsNumber(key) ? FromNumberKey(key, type, options) :
89
- IsRegExp(key) ? FromRegExpKey(key, type, options) :
90
- IsString(key) ? FromStringKey(key, type, options) :
91
- IsAny(key) ? FromAnyKey(key, type, options) :
92
- IsNever(key) ? FromNeverKey(key, type, options) :
93
- Never(options));
93
+ IsBoolean(key) ? FromBooleanKey(key, type, options) :
94
+ IsInteger(key) ? FromIntegerKey(key, type, options) :
95
+ IsNumber(key) ? FromNumberKey(key, type, options) :
96
+ IsRegExp(key) ? FromRegExpKey(key, type, options) :
97
+ IsString(key) ? FromStringKey(key, type, options) :
98
+ IsAny(key) ? FromAnyKey(key, type, options) :
99
+ IsNever(key) ? FromNeverKey(key, type, options) :
100
+ Never(options));
101
+ }
102
+ // ------------------------------------------------------------------
103
+ // Record Utilities
104
+ // ------------------------------------------------------------------
105
+ /** Gets the Records Pattern */
106
+ export function RecordPattern(record) {
107
+ return globalThis.Object.getOwnPropertyNames(record.patternProperties)[0];
108
+ }
109
+ /** Gets the Records Key Type */
110
+ // prettier-ignore
111
+ export function RecordKey(type) {
112
+ const pattern = RecordPattern(type);
113
+ return (pattern === PatternStringExact ? String() :
114
+ pattern === PatternNumberExact ? Number() :
115
+ String({ pattern }));
116
+ }
117
+ /** Gets a Record Value Type */
118
+ // prettier-ignore
119
+ export function RecordValue(type) {
120
+ return type.patternProperties[RecordPattern(type)];
94
121
  }
@@ -0,0 +1 @@
1
+ export * from './remap.mjs';
@@ -0,0 +1 @@
1
+ export * from './remap.mjs';
@@ -0,0 +1,19 @@
1
+ import * as t from '@sinclair/typebox.mjs';
2
+ export type TCallback = (schema: t.TSchema) => t.TSchema;
3
+ export interface TMapping {
4
+ input: unknown;
5
+ output: unknown;
6
+ }
7
+ type TApply<Type extends t.TSchema, Mapping extends TMapping, Mapped = (Mapping & {
8
+ input: Type;
9
+ })['output'], Result = Mapped extends t.TSchema ? Mapped : never> = Result;
10
+ type TFromProperties<Properties extends t.TProperties, Mapping extends TMapping, Result extends t.TProperties = {
11
+ [Key in keyof Properties]: TRemap<Properties[Key], Mapping>;
12
+ }> = Result;
13
+ type TFromTypes<Types extends t.TSchema[], Mapping extends TMapping, Result extends t.TSchema[] = []> = (Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]] ? TFromTypes<Right, Mapping, [...Result, TRemap<Left, Mapping>]> : Result);
14
+ type TFromType<Type extends t.TSchema, Mapping extends TMapping, Result extends t.TSchema = (TApply<Type, Mapping>)> = Result;
15
+ /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
16
+ export type TRemap<Type extends t.TSchema, Mapping extends TMapping, Mapped extends t.TSchema = TFromType<Type, Mapping>, Result extends t.TSchema = (Mapped extends t.TConstructor<infer Parameters extends t.TSchema[], infer ReturnType extends t.TSchema> ? t.TConstructor<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends t.TFunction<infer Parameters extends t.TSchema[], infer ReturnType extends t.TSchema> ? t.TFunction<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends t.TIntersect<infer Types extends t.TSchema[]> ? t.TIntersect<TFromTypes<Types, Mapping>> : Mapped extends t.TUnion<infer Types extends t.TSchema[]> ? t.TUnion<TFromTypes<Types, Mapping>> : Mapped extends t.TTuple<infer Types extends t.TSchema[]> ? t.TTuple<TFromTypes<Types, Mapping>> : Mapped extends t.TArray<infer Type extends t.TSchema> ? t.TArray<TFromType<Type, Mapping>> : Mapped extends t.TAsyncIterator<infer Type extends t.TSchema> ? t.TAsyncIterator<TFromType<Type, Mapping>> : Mapped extends t.TIterator<infer Type extends t.TSchema> ? t.TIterator<TFromType<Type, Mapping>> : Mapped extends t.TPromise<infer Type extends t.TSchema> ? t.TPromise<TFromType<Type, Mapping>> : Mapped extends t.TObject<infer Properties extends t.TProperties> ? t.TObject<TFromProperties<Properties, Mapping>> : Mapped extends t.TRecord<infer Key extends t.TSchema, infer Value extends t.TSchema> ? t.TRecordOrObject<TFromType<Key, Mapping>, TFromType<Value, Mapping>> : Mapped)> = Result;
17
+ /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
18
+ export declare function Remap(type: t.TSchema, callback: TCallback): t.TSchema;
19
+ export {};
@@ -0,0 +1,31 @@
1
+ import * as t from '@sinclair/typebox.mjs';
2
+ function FromProperties(properties, func) {
3
+ return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
4
+ return { ...result, [key]: Remap(properties[key], func) };
5
+ }, {});
6
+ }
7
+ function FromTypes(types, callback) {
8
+ return types.map((type) => Remap(type, callback));
9
+ }
10
+ function FromType(type, callback) {
11
+ return callback(type);
12
+ }
13
+ /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
14
+ // prettier-ignore
15
+ export function Remap(type, callback) {
16
+ // Map incoming type
17
+ const mapped = t.CloneType(FromType(type, callback));
18
+ // Return remapped interior
19
+ return (t.KindGuard.IsConstructor(type) ? t.Constructor(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
20
+ t.KindGuard.IsFunction(type) ? t.Function(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
21
+ t.KindGuard.IsIntersect(type) ? t.Intersect(FromTypes(type.allOf, callback), mapped) :
22
+ t.KindGuard.IsUnion(type) ? t.Union(FromTypes(type.anyOf, callback), mapped) :
23
+ t.KindGuard.IsTuple(type) ? t.Tuple(FromTypes(type.items || [], callback), mapped) :
24
+ t.KindGuard.IsArray(type) ? t.Array(FromType(type.items, callback), mapped) :
25
+ t.KindGuard.IsAsyncIterator(type) ? t.AsyncIterator(FromType(type.items, callback), mapped) :
26
+ t.KindGuard.IsIterator(type) ? t.Iterator(FromType(type.items, callback), mapped) :
27
+ t.KindGuard.IsPromise(type) ? t.Promise(FromType(type.items, callback), mapped) :
28
+ t.KindGuard.IsObject(type) ? t.Object(FromProperties(type.properties, callback), mapped) :
29
+ t.KindGuard.IsRecord(type) ? t.Record(FromType(t.RecordKey(type), callback), FromType(t.RecordValue(type), callback), mapped) :
30
+ t.CloneType(mapped));
31
+ }
@@ -1,4 +1,5 @@
1
1
  import { JsonTypeBuilder } from './json.mjs';
2
+ import { type TArgument } from '../argument/index.mjs';
2
3
  import { type TAsyncIterator } from '../async-iterator/index.mjs';
3
4
  import { type TAwaited } from '../awaited/index.mjs';
4
5
  import { type TBigInt, type BigIntOptions } from '../bigint/index.mjs';
@@ -7,6 +8,7 @@ import { type TConstructorParameters } from '../constructor-parameters/index.mjs
7
8
  import { type TDate, type DateOptions } from '../date/index.mjs';
8
9
  import { type TFunction } from '../function/index.mjs';
9
10
  import { type TInstanceType } from '../instance-type/index.mjs';
11
+ import { type TInstantiate } from '../instantiate/index.mjs';
10
12
  import { type TIterator } from '../iterator/index.mjs';
11
13
  import { type TParameters } from '../parameters/index.mjs';
12
14
  import { type TPromise } from '../promise/index.mjs';
@@ -19,6 +21,8 @@ import { type TUndefined } from '../undefined/index.mjs';
19
21
  import { type TVoid } from '../void/index.mjs';
20
22
  /** JavaScript Type Builder with Static Resolution for TypeScript */
21
23
  export declare class JavaScriptTypeBuilder extends JsonTypeBuilder {
24
+ /** `[JavaScript]` Creates a Generic Argument Type */
25
+ Argument<Index extends number>(index: Index): TArgument<Index>;
22
26
  /** `[JavaScript]` Creates a AsyncIterator type */
23
27
  AsyncIterator<Type extends TSchema>(items: Type, options?: SchemaOptions): TAsyncIterator<Type>;
24
28
  /** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */
@@ -35,6 +39,8 @@ export declare class JavaScriptTypeBuilder extends JsonTypeBuilder {
35
39
  Function<Parameters extends TSchema[], ReturnType extends TSchema>(parameters: [...Parameters], returnType: ReturnType, options?: SchemaOptions): TFunction<Parameters, ReturnType>;
36
40
  /** `[JavaScript]` Extracts the InstanceType from the given Constructor type */
37
41
  InstanceType<Type extends TSchema>(schema: Type, options?: SchemaOptions): TInstanceType<Type>;
42
+ /** `[JavaScript]` Instantiates a type with the given parameters */
43
+ Instantiate<Type extends TSchema, Parameters extends TSchema[]>(schema: Type, parameters: [...Parameters]): TInstantiate<Type, Parameters>;
38
44
  /** `[JavaScript]` Creates an Iterator type */
39
45
  Iterator<Type extends TSchema>(items: Type, options?: SchemaOptions): TIterator<Type>;
40
46
  /** `[JavaScript]` Extracts the Parameters from the given Function type */
@@ -1,4 +1,5 @@
1
1
  import { JsonTypeBuilder } from './json.mjs';
2
+ import { Argument } from '../argument/index.mjs';
2
3
  import { AsyncIterator } from '../async-iterator/index.mjs';
3
4
  import { Awaited } from '../awaited/index.mjs';
4
5
  import { BigInt } from '../bigint/index.mjs';
@@ -7,6 +8,7 @@ import { ConstructorParameters } from '../constructor-parameters/index.mjs';
7
8
  import { Date } from '../date/index.mjs';
8
9
  import { Function as FunctionType } from '../function/index.mjs';
9
10
  import { InstanceType } from '../instance-type/index.mjs';
11
+ import { Instantiate } from '../instantiate/index.mjs';
10
12
  import { Iterator } from '../iterator/index.mjs';
11
13
  import { Parameters } from '../parameters/index.mjs';
12
14
  import { Promise } from '../promise/index.mjs';
@@ -18,6 +20,10 @@ import { Undefined } from '../undefined/index.mjs';
18
20
  import { Void } from '../void/index.mjs';
19
21
  /** JavaScript Type Builder with Static Resolution for TypeScript */
20
22
  export class JavaScriptTypeBuilder extends JsonTypeBuilder {
23
+ /** `[JavaScript]` Creates a Generic Argument Type */
24
+ Argument(index) {
25
+ return Argument(index);
26
+ }
21
27
  /** `[JavaScript]` Creates a AsyncIterator type */
22
28
  AsyncIterator(items, options) {
23
29
  return AsyncIterator(items, options);
@@ -50,6 +56,10 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder {
50
56
  InstanceType(schema, options) {
51
57
  return InstanceType(schema, options);
52
58
  }
59
+ /** `[JavaScript]` Instantiates a type with the given parameters */
60
+ Instantiate(schema, parameters) {
61
+ return Instantiate(schema, parameters);
62
+ }
53
63
  /** `[JavaScript]` Creates an Iterator type */
54
64
  Iterator(items, options) {
55
65
  return Iterator(items, options);
@@ -1,4 +1,5 @@
1
1
  export { Any } from '../any/index.mjs';
2
+ export { Argument } from '../argument/index.mjs';
2
3
  export { Array } from '../array/index.mjs';
3
4
  export { AsyncIterator } from '../async-iterator/index.mjs';
4
5
  export { Awaited } from '../awaited/index.mjs';
@@ -16,6 +17,7 @@ export { Extract } from '../extract/index.mjs';
16
17
  export { Function } from '../function/index.mjs';
17
18
  export { Index } from '../indexed/index.mjs';
18
19
  export { InstanceType } from '../instance-type/index.mjs';
20
+ export { Instantiate } from '../instantiate/index.mjs';
19
21
  export { Integer } from '../integer/index.mjs';
20
22
  export { Intersect } from '../intersect/index.mjs';
21
23
  export { Capitalize, Uncapitalize, Lowercase, Uppercase } from '../intrinsic/index.mjs';
@@ -41,6 +43,7 @@ export { Record } from '../record/index.mjs';
41
43
  export { Recursive } from '../recursive/index.mjs';
42
44
  export { Ref } from '../ref/index.mjs';
43
45
  export { RegExp } from '../regexp/index.mjs';
46
+ export { Remap } from '../remap/index.mjs';
44
47
  export { Required } from '../required/index.mjs';
45
48
  export { Rest } from '../rest/index.mjs';
46
49
  export { ReturnType } from '../return-type/index.mjs';
@@ -2,6 +2,7 @@
2
2
  // Type: Module
3
3
  // ------------------------------------------------------------------
4
4
  export { Any } from '../any/index.mjs';
5
+ export { Argument } from '../argument/index.mjs';
5
6
  export { Array } from '../array/index.mjs';
6
7
  export { AsyncIterator } from '../async-iterator/index.mjs';
7
8
  export { Awaited } from '../awaited/index.mjs';
@@ -19,6 +20,7 @@ export { Extract } from '../extract/index.mjs';
19
20
  export { Function } from '../function/index.mjs';
20
21
  export { Index } from '../indexed/index.mjs';
21
22
  export { InstanceType } from '../instance-type/index.mjs';
23
+ export { Instantiate } from '../instantiate/index.mjs';
22
24
  export { Integer } from '../integer/index.mjs';
23
25
  export { Intersect } from '../intersect/index.mjs';
24
26
  export { Capitalize, Uncapitalize, Lowercase, Uppercase } from '../intrinsic/index.mjs';
@@ -44,6 +46,7 @@ export { Record } from '../record/index.mjs';
44
46
  export { Recursive } from '../recursive/index.mjs';
45
47
  export { Ref } from '../ref/index.mjs';
46
48
  export { RegExp } from '../regexp/index.mjs';
49
+ export { Remap } from '../remap/index.mjs';
47
50
  export { Required } from '../required/index.mjs';
48
51
  export { Rest } from '../rest/index.mjs';
49
52
  export { ReturnType } from '../return-type/index.mjs';
@@ -42,6 +42,9 @@ function IsDefined(value) {
42
42
  function FromAny(schema, references, value) {
43
43
  return true;
44
44
  }
45
+ function FromArgument(schema, references, value) {
46
+ return true;
47
+ }
45
48
  function FromArray(schema, references, value) {
46
49
  if (!IsArray(value))
47
50
  return false;
@@ -390,6 +393,8 @@ function Visit(schema, references, value) {
390
393
  switch (schema_[Kind]) {
391
394
  case 'Any':
392
395
  return FromAny(schema_, references_, value);
396
+ case 'Argument':
397
+ return FromArgument(schema_, references_, value);
393
398
  case 'Array':
394
399
  return FromArray(schema_, references_, value);
395
400
  case 'AsyncIterator':
@@ -34,6 +34,9 @@ function FromAny(schema, references) {
34
34
  return {};
35
35
  }
36
36
  }
37
+ function FromArgument(schema, references) {
38
+ return {};
39
+ }
37
40
  function FromArray(schema, references) {
38
41
  if (schema.uniqueItems === true && !HasPropertyKey(schema, 'default')) {
39
42
  throw new ValueCreateError(schema, 'Array with the uniqueItems constraint requires a default value');
@@ -392,6 +395,8 @@ function Visit(schema, references) {
392
395
  switch (schema_[Kind]) {
393
396
  case 'Any':
394
397
  return FromAny(schema_, references_);
398
+ case 'Argument':
399
+ return FromArgument(schema_, references_);
395
400
  case 'Array':
396
401
  return FromArray(schema_, references_);
397
402
  case 'AsyncIterator':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.34.16",
3
+ "version": "0.34.17",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",