@sinclair/typebox 0.34.16 → 0.34.18
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.
- package/build/cjs/compiler/compiler.js +5 -0
- package/build/cjs/errors/errors.js +3 -0
- package/build/cjs/index.d.ts +3 -0
- package/build/cjs/index.js +3 -0
- package/build/cjs/syntax/runtime.d.ts +3 -1
- package/build/cjs/syntax/runtime.js +33 -3
- package/build/cjs/syntax/static.d.ts +22 -2
- package/build/cjs/type/argument/argument.d.ts +9 -0
- package/build/cjs/type/argument/argument.js +10 -0
- package/build/cjs/type/argument/index.d.ts +1 -0
- package/build/cjs/type/argument/index.js +18 -0
- package/build/cjs/type/guard/kind.d.ts +9 -6
- package/build/cjs/type/guard/kind.js +6 -0
- package/build/cjs/type/guard/type.d.ts +9 -6
- package/build/cjs/type/guard/type.js +9 -0
- package/build/cjs/type/index.d.ts +2 -0
- package/build/cjs/type/index.js +2 -0
- package/build/cjs/type/instantiate/index.d.ts +1 -0
- package/build/cjs/type/instantiate/index.js +18 -0
- package/build/cjs/type/instantiate/instantiate.d.ts +25 -0
- package/build/cjs/type/instantiate/instantiate.js +32 -0
- package/build/cjs/type/record/record.d.ts +16 -1
- package/build/cjs/type/record/record.js +53 -23
- package/build/cjs/type/remap/index.d.ts +1 -0
- package/build/cjs/type/remap/index.js +18 -0
- package/build/cjs/type/remap/remap.d.ts +30 -0
- package/build/cjs/type/remap/remap.js +47 -0
- package/build/cjs/type/type/javascript.d.ts +6 -0
- package/build/cjs/type/type/javascript.js +44 -34
- package/build/cjs/type/type/type.d.ts +3 -0
- package/build/cjs/type/type/type.js +123 -117
- package/build/cjs/value/check/check.js +5 -0
- package/build/cjs/value/create/create.js +5 -0
- package/build/esm/compiler/compiler.mjs +5 -0
- package/build/esm/errors/errors.mjs +3 -0
- package/build/esm/index.d.mts +3 -0
- package/build/esm/index.mjs +3 -0
- package/build/esm/syntax/runtime.d.mts +3 -1
- package/build/esm/syntax/runtime.mjs +33 -3
- package/build/esm/syntax/static.d.mts +22 -2
- package/build/esm/type/argument/argument.d.mts +9 -0
- package/build/esm/type/argument/argument.mjs +6 -0
- package/build/esm/type/argument/index.d.mts +1 -0
- package/build/esm/type/argument/index.mjs +1 -0
- package/build/esm/type/guard/kind.d.mts +9 -6
- package/build/esm/type/guard/kind.mjs +5 -0
- package/build/esm/type/guard/type.d.mts +9 -6
- package/build/esm/type/guard/type.mjs +8 -0
- package/build/esm/type/index.d.mts +2 -0
- package/build/esm/type/index.mjs +2 -0
- package/build/esm/type/instantiate/index.d.mts +1 -0
- package/build/esm/type/instantiate/index.mjs +1 -0
- package/build/esm/type/instantiate/instantiate.d.mts +25 -0
- package/build/esm/type/instantiate/instantiate.mjs +28 -0
- package/build/esm/type/record/record.d.mts +16 -1
- package/build/esm/type/record/record.mjs +35 -8
- package/build/esm/type/remap/index.d.mts +1 -0
- package/build/esm/type/remap/index.mjs +1 -0
- package/build/esm/type/remap/remap.d.mts +30 -0
- package/build/esm/type/remap/remap.mjs +43 -0
- package/build/esm/type/type/javascript.d.mts +6 -0
- package/build/esm/type/type/javascript.mjs +10 -0
- package/build/esm/type/type/type.d.mts +3 -0
- package/build/esm/type/type/type.mjs +3 -0
- package/build/esm/value/check/check.mjs +5 -0
- package/build/esm/value/create/create.mjs +5 -0
- package/package.json +1 -1
- 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 {
|
|
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';
|
package/build/esm/type/index.mjs
CHANGED
|
@@ -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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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,30 @@
|
|
|
1
|
+
import { TSchema } from '../schema/index.mjs';
|
|
2
|
+
import { TObject, TProperties } from '../object/index.mjs';
|
|
3
|
+
import { TConstructor } from '../constructor/index.mjs';
|
|
4
|
+
import { TFunction } from '../function/index.mjs';
|
|
5
|
+
import { TIntersect } from '../intersect/index.mjs';
|
|
6
|
+
import { TUnion } from '../union/index.mjs';
|
|
7
|
+
import { TTuple } from '../tuple/index.mjs';
|
|
8
|
+
import { TArray } from '../array/index.mjs';
|
|
9
|
+
import { TAsyncIterator } from '../async-iterator/index.mjs';
|
|
10
|
+
import { TIterator } from '../iterator/index.mjs';
|
|
11
|
+
import { TPromise } from '../promise/index.mjs';
|
|
12
|
+
import { TRecord, TRecordOrObject } from '../record/index.mjs';
|
|
13
|
+
export type TCallback = (schema: TSchema) => TSchema;
|
|
14
|
+
export interface TMapping {
|
|
15
|
+
input: unknown;
|
|
16
|
+
output: unknown;
|
|
17
|
+
}
|
|
18
|
+
type TApply<Type extends TSchema, Mapping extends TMapping, Mapped = (Mapping & {
|
|
19
|
+
input: Type;
|
|
20
|
+
})['output'], Result = Mapped extends TSchema ? Mapped : never> = Result;
|
|
21
|
+
type TFromProperties<Properties extends TProperties, Mapping extends TMapping, Result extends TProperties = {
|
|
22
|
+
[Key in keyof Properties]: TRemap<Properties[Key], Mapping>;
|
|
23
|
+
}> = Result;
|
|
24
|
+
type TFromTypes<Types extends TSchema[], Mapping extends TMapping, Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromTypes<Right, Mapping, [...Result, TRemap<Left, Mapping>]> : Result);
|
|
25
|
+
type TFromType<Type extends TSchema, Mapping extends TMapping, Result extends TSchema = (TApply<Type, Mapping>)> = Result;
|
|
26
|
+
/** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
|
|
27
|
+
export type TRemap<Type extends TSchema, Mapping extends TMapping, Mapped extends TSchema = TFromType<Type, Mapping>, Result extends TSchema = (Mapped extends TConstructor<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TConstructor<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromTypes<Types, Mapping>> : Mapped extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromTypes<Types, Mapping>> : Mapped extends TTuple<infer Types extends TSchema[]> ? TTuple<TFromTypes<Types, Mapping>> : Mapped extends TArray<infer Type extends TSchema> ? TArray<TFromType<Type, Mapping>> : Mapped extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TFromType<Type, Mapping>> : Mapped extends TIterator<infer Type extends TSchema> ? TIterator<TFromType<Type, Mapping>> : Mapped extends TPromise<infer Type extends TSchema> ? TPromise<TFromType<Type, Mapping>> : Mapped extends TObject<infer Properties extends TProperties> ? TObject<TFromProperties<Properties, Mapping>> : Mapped extends TRecord<infer Key extends TSchema, infer Value extends TSchema> ? TRecordOrObject<TFromType<Key, Mapping>, TFromType<Value, Mapping>> : Mapped)> = Result;
|
|
28
|
+
/** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
|
|
29
|
+
export declare function Remap(type: TSchema, callback: TCallback): TSchema;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Object } from '../object/index.mjs';
|
|
2
|
+
import { Constructor } from '../constructor/index.mjs';
|
|
3
|
+
import { Function } from '../function/index.mjs';
|
|
4
|
+
import { Intersect } from '../intersect/index.mjs';
|
|
5
|
+
import { Union } from '../union/index.mjs';
|
|
6
|
+
import { Tuple } from '../tuple/index.mjs';
|
|
7
|
+
import { Array } from '../array/index.mjs';
|
|
8
|
+
import { AsyncIterator } from '../async-iterator/index.mjs';
|
|
9
|
+
import { Iterator } from '../iterator/index.mjs';
|
|
10
|
+
import { Promise } from '../promise/index.mjs';
|
|
11
|
+
import { Record, RecordKey, RecordValue } from '../record/index.mjs';
|
|
12
|
+
import * as KindGuard from '../guard/kind.mjs';
|
|
13
|
+
import { CloneType } from '../clone/type.mjs';
|
|
14
|
+
function FromProperties(properties, func) {
|
|
15
|
+
return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
|
|
16
|
+
return { ...result, [key]: Remap(properties[key], func) };
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
function FromTypes(types, callback) {
|
|
20
|
+
return types.map((type) => Remap(type, callback));
|
|
21
|
+
}
|
|
22
|
+
function FromType(type, callback) {
|
|
23
|
+
return callback(type);
|
|
24
|
+
}
|
|
25
|
+
/** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
|
|
26
|
+
// prettier-ignore
|
|
27
|
+
export function Remap(type, callback) {
|
|
28
|
+
// Map incoming type
|
|
29
|
+
const mapped = CloneType(FromType(type, callback));
|
|
30
|
+
// Return remapped interior
|
|
31
|
+
return (KindGuard.IsConstructor(type) ? Constructor(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
|
|
32
|
+
KindGuard.IsFunction(type) ? Function(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
|
|
33
|
+
KindGuard.IsIntersect(type) ? Intersect(FromTypes(type.allOf, callback), mapped) :
|
|
34
|
+
KindGuard.IsUnion(type) ? Union(FromTypes(type.anyOf, callback), mapped) :
|
|
35
|
+
KindGuard.IsTuple(type) ? Tuple(FromTypes(type.items || [], callback), mapped) :
|
|
36
|
+
KindGuard.IsArray(type) ? Array(FromType(type.items, callback), mapped) :
|
|
37
|
+
KindGuard.IsAsyncIterator(type) ? AsyncIterator(FromType(type.items, callback), mapped) :
|
|
38
|
+
KindGuard.IsIterator(type) ? Iterator(FromType(type.items, callback), mapped) :
|
|
39
|
+
KindGuard.IsPromise(type) ? Promise(FromType(type.items, callback), mapped) :
|
|
40
|
+
KindGuard.IsObject(type) ? Object(FromProperties(type.properties, callback), mapped) :
|
|
41
|
+
KindGuard.IsRecord(type) ? Record(FromType(RecordKey(type), callback), FromType(RecordValue(type), callback), mapped) :
|
|
42
|
+
CloneType(mapped));
|
|
43
|
+
}
|
|
@@ -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':
|