@sinclair/typebox 0.30.0-dev-4 → 0.30.0-dev-6
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/package.json +1 -1
- package/readme.md +12 -12
- package/typebox.d.ts +80 -52
- package/typebox.js +73 -37
- package/value/convert.js +2 -2
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -63,7 +63,7 @@ type T = Static<typeof T> // type T = {
|
|
|
63
63
|
|
|
64
64
|
## Overview
|
|
65
65
|
|
|
66
|
-
TypeBox is a runtime type builder that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type assertion rules of the TypeScript
|
|
66
|
+
TypeBox is a runtime type builder that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type assertion rules of the TypeScript language. TypeBox allows one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
|
|
67
67
|
|
|
68
68
|
This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
|
|
69
69
|
|
|
@@ -72,6 +72,7 @@ License MIT
|
|
|
72
72
|
## Contents
|
|
73
73
|
- [Install](#install)
|
|
74
74
|
- [Overview](#overview)
|
|
75
|
+
- [Features](#features)
|
|
75
76
|
- [Usage](#usage)
|
|
76
77
|
- [Types](#types)
|
|
77
78
|
- [Standard](#types-standard)
|
|
@@ -102,7 +103,6 @@ License MIT
|
|
|
102
103
|
- [Errors](#values-errors)
|
|
103
104
|
- [Mutate](#values-mutate)
|
|
104
105
|
- [Pointer](#values-pointer)
|
|
105
|
-
- [Transform](#values-transform)
|
|
106
106
|
- [TypeCheck](#typecheck)
|
|
107
107
|
- [Ajv](#typecheck-ajv)
|
|
108
108
|
- [TypeCompiler](#typecheck-typecompiler)
|
|
@@ -110,7 +110,7 @@ License MIT
|
|
|
110
110
|
- [Types](#typesystem-types)
|
|
111
111
|
- [Formats](#typesystem-formats)
|
|
112
112
|
- [Policies](#typesystem-policies)
|
|
113
|
-
- [
|
|
113
|
+
- [Transform](#Transform)
|
|
114
114
|
- [Ecosystem](#ecosystem)
|
|
115
115
|
- [Benchmark](#benchmark)
|
|
116
116
|
- [Compile](#benchmark-compile)
|
|
@@ -1457,13 +1457,13 @@ TypeSystem.AllowArrayObjects = true
|
|
|
1457
1457
|
TypeSystem.AllowNaN = true
|
|
1458
1458
|
```
|
|
1459
1459
|
|
|
1460
|
-
<a name='
|
|
1460
|
+
<a name='transform'></a>
|
|
1461
1461
|
|
|
1462
|
-
##
|
|
1462
|
+
## TypeBox Transform
|
|
1463
1463
|
|
|
1464
|
-
TypeBox offers a web based code generation tool that can be used to convert TypeScript types into TypeBox types as well as a variety of other runtime type representations.
|
|
1464
|
+
TypeBox offers a small web based code generation tool that can be used to convert TypeScript types into TypeBox types as well as a variety of other runtime type representations.
|
|
1465
1465
|
|
|
1466
|
-
[
|
|
1466
|
+
[TypeBox Transform Link Here](https://sinclairzx81.github.io/typebox-transform/)
|
|
1467
1467
|
|
|
1468
1468
|
<a name='ecosystem'></a>
|
|
1469
1469
|
|
|
@@ -1586,11 +1586,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1586
1586
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1587
1587
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1588
1588
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1589
|
-
│ typebox/compiler │ '
|
|
1590
|
-
│ typebox/errors │ '
|
|
1591
|
-
│ typebox/system │ '
|
|
1592
|
-
│ typebox/value │ '
|
|
1593
|
-
│ typebox │ '
|
|
1589
|
+
│ typebox/compiler │ '129.4 kb' │ ' 58.6 kb' │ '2.21 x' │
|
|
1590
|
+
│ typebox/errors │ '111.6 kb' │ ' 50.1 kb' │ '2.23 x' │
|
|
1591
|
+
│ typebox/system │ ' 76.5 kb' │ ' 31.7 kb' │ '2.41 x' │
|
|
1592
|
+
│ typebox/value │ '180.7 kb' │ ' 79.3 kb' │ '2.28 x' │
|
|
1593
|
+
│ typebox │ ' 75.4 kb' │ ' 31.3 kb' │ '2.41 x' │
|
|
1594
1594
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1595
1595
|
```
|
|
1596
1596
|
|
package/typebox.d.ts
CHANGED
|
@@ -85,12 +85,18 @@ export interface TAny extends TSchema {
|
|
|
85
85
|
static: any;
|
|
86
86
|
}
|
|
87
87
|
export interface ArrayOptions extends SchemaOptions {
|
|
88
|
+
/** The minimum number of items in this array */
|
|
88
89
|
minItems?: number;
|
|
90
|
+
/** The maximum number of items in this array */
|
|
89
91
|
maxItems?: number;
|
|
92
|
+
/** Should this schema contain unique items */
|
|
93
|
+
uniqueItems?: boolean;
|
|
94
|
+
/** A schema for which some elements should match */
|
|
90
95
|
contains?: TSchema;
|
|
96
|
+
/** A minimum number of contains schema matches */
|
|
91
97
|
minContains?: number;
|
|
98
|
+
/** A maximum number of contains schema matches */
|
|
92
99
|
maxContains?: number;
|
|
93
|
-
uniqueItems?: boolean;
|
|
94
100
|
}
|
|
95
101
|
export interface TArray<T extends TSchema = TSchema> extends TSchema, ArrayOptions {
|
|
96
102
|
[Kind]: 'Array';
|
|
@@ -104,6 +110,8 @@ export interface TAsyncIterator<T extends TSchema = TSchema> extends TSchema {
|
|
|
104
110
|
type: 'AsyncIterator';
|
|
105
111
|
items: T;
|
|
106
112
|
}
|
|
113
|
+
export type TAwaitedRest<T extends TSchema[]> = T extends [infer L, ...infer R] ? [TAwaited<AssertType<L>>, ...TAwaitedRest<AssertRest<R>>] : [];
|
|
114
|
+
export type TAwaited<T extends TSchema> = T extends TIntersect<infer S> ? TIntersect<TAwaitedRest<S>> : T extends TUnion<infer S> ? TUnion<TAwaitedRest<S>> : T extends TPromise<infer S> ? TAwaited<S> : T;
|
|
107
115
|
export interface TBigInt extends TSchema, NumericOptions<bigint> {
|
|
108
116
|
[Kind]: 'BigInt';
|
|
109
117
|
static: bigint;
|
|
@@ -133,9 +141,13 @@ export interface TConstructor<T extends TSchema[] = TSchema[], U extends TSchema
|
|
|
133
141
|
returns: U;
|
|
134
142
|
}
|
|
135
143
|
export interface DateOptions extends SchemaOptions {
|
|
144
|
+
/** The exclusive maximum timestamp value */
|
|
136
145
|
exclusiveMaximumTimestamp?: number;
|
|
146
|
+
/** The exclusive minimum timestamp value */
|
|
137
147
|
exclusiveMinimumTimestamp?: number;
|
|
148
|
+
/** The maximum timestamp value */
|
|
138
149
|
maximumTimestamp?: number;
|
|
150
|
+
/** The minimum timestamp value */
|
|
139
151
|
minimumTimestamp?: number;
|
|
140
152
|
}
|
|
141
153
|
export interface TDate extends TSchema, DateOptions {
|
|
@@ -276,29 +288,29 @@ export interface TObject<T extends TProperties = TProperties> extends TSchema, O
|
|
|
276
288
|
properties: T;
|
|
277
289
|
required?: string[];
|
|
278
290
|
}
|
|
279
|
-
export type
|
|
291
|
+
export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<AssertProperties<Omit<T, K>>>;
|
|
292
|
+
export type TOmitRest<T extends TSchema[], K extends keyof any> = AssertRest<{
|
|
280
293
|
[K2 in keyof T]: TOmit<AssertType<T[K2]>, K>;
|
|
281
294
|
}>;
|
|
282
|
-
export type
|
|
283
|
-
export type TOmit<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TOmit<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
|
|
295
|
+
export type TOmit<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TOmit<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitRest<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitRest<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
|
|
284
296
|
export type TParameters<T extends TFunction> = Ensure<TTuple<T['parameters']>>;
|
|
285
297
|
export type TPartialObjectArray<T extends TObject[]> = AssertRest<{
|
|
286
298
|
[K in keyof T]: TPartial<AssertType<T[K], TObject>>;
|
|
287
299
|
}, TObject[]>;
|
|
288
|
-
export type
|
|
300
|
+
export type TPartialRest<T extends TSchema[]> = AssertRest<{
|
|
289
301
|
[K in keyof T]: TPartial<AssertType<T[K]>>;
|
|
290
302
|
}>;
|
|
291
303
|
export type TPartialProperties<T extends TProperties> = Evaluate<AssertProperties<{
|
|
292
304
|
[K in keyof T]: TOptional<T[K]>;
|
|
293
305
|
}>>;
|
|
294
|
-
export type TPartial<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TPartial<S>> : T extends TIntersect<infer S> ? TIntersect<
|
|
295
|
-
export type TPickArray<T extends TSchema[], K extends keyof any> = {
|
|
296
|
-
[K2 in keyof T]: TPick<AssertType<T[K2]>, K>;
|
|
297
|
-
};
|
|
306
|
+
export type TPartial<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TPartial<S>> : T extends TIntersect<infer S> ? TIntersect<TPartialRest<S>> : T extends TUnion<infer S> ? TUnion<TPartialRest<S>> : T extends TObject<infer S> ? TObject<TPartialProperties<S>> : T;
|
|
298
307
|
export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T, Assert<Extract<K, keyof T>, keyof T>> extends infer R ? ({
|
|
299
308
|
[K in keyof R]: AssertType<R[K]> extends TSchema ? R[K] : never;
|
|
300
309
|
}) : never;
|
|
301
|
-
export type
|
|
310
|
+
export type TPickRest<T extends TSchema[], K extends keyof any> = {
|
|
311
|
+
[K2 in keyof T]: TPick<AssertType<T[K2]>, K>;
|
|
312
|
+
};
|
|
313
|
+
export type TPick<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TPick<S, K>> : T extends TIntersect<infer S> ? TIntersect<TPickRest<S, K>> : T extends TUnion<infer S> ? TUnion<TPickRest<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T;
|
|
302
314
|
export interface TPromise<T extends TSchema = TSchema> extends TSchema {
|
|
303
315
|
[Kind]: 'Promise';
|
|
304
316
|
static: Promise<Static<T, this['params']>>;
|
|
@@ -344,21 +356,27 @@ export interface TRef<T extends TSchema = TSchema> extends TSchema {
|
|
|
344
356
|
}
|
|
345
357
|
export type TRest<T extends TSchema> = T extends TTuple<infer R> ? Assert<R, TSchema[]> : Assert<[T], TSchema[]>;
|
|
346
358
|
export type TReturnType<T extends TFunction> = T['returns'];
|
|
347
|
-
export type
|
|
359
|
+
export type TRequiredRest<T extends TSchema[]> = AssertRest<{
|
|
348
360
|
[K in keyof T]: TRequired<AssertType<T[K]>>;
|
|
349
361
|
}>;
|
|
350
362
|
export type TRequiredProperties<T extends TProperties> = Evaluate<AssertProperties<{
|
|
351
363
|
[K in keyof T]: T[K] extends TOptional<infer S> ? S : T[K];
|
|
352
364
|
}>>;
|
|
353
|
-
export type TRequired<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TRequired<S>> : T extends TIntersect<infer S> ? TIntersect<
|
|
365
|
+
export type TRequired<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TRequired<S>> : T extends TIntersect<infer S> ? TIntersect<TRequiredRest<S>> : T extends TUnion<infer S> ? TUnion<TRequiredRest<S>> : T extends TObject<infer S> ? TObject<TRequiredProperties<S>> : T;
|
|
354
366
|
export type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex' | ({} & string);
|
|
355
367
|
export type StringContentEncodingOption = '7bit' | '8bit' | 'binary' | 'quoted-printable' | 'base64' | ({} & string);
|
|
356
368
|
export interface StringOptions extends SchemaOptions {
|
|
357
|
-
|
|
369
|
+
/** The maximum string length */
|
|
358
370
|
maxLength?: number;
|
|
371
|
+
/** The minimum string length */
|
|
372
|
+
minLength?: number;
|
|
373
|
+
/** A regular expression pattern this string should match */
|
|
359
374
|
pattern?: string;
|
|
375
|
+
/** A format this string should match */
|
|
360
376
|
format?: StringFormatOption;
|
|
377
|
+
/** The content encoding for this string */
|
|
361
378
|
contentEncoding?: StringContentEncodingOption;
|
|
379
|
+
/** The content media type for this string */
|
|
362
380
|
contentMediaType?: string;
|
|
363
381
|
}
|
|
364
382
|
export interface TString extends TSchema, StringOptions {
|
|
@@ -394,7 +412,6 @@ export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLi
|
|
|
394
412
|
type: 'string';
|
|
395
413
|
pattern: string;
|
|
396
414
|
}
|
|
397
|
-
export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? AssertRest<R> : never;
|
|
398
415
|
export type TTupleInfer<T extends TSchema[], P extends unknown[]> = T extends [infer L, ...infer R] ? [Static<AssertType<L>, P>, ...TTupleInfer<AssertRest<R>, P>] : [];
|
|
399
416
|
export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema {
|
|
400
417
|
[Kind]: 'Tuple';
|
|
@@ -682,75 +699,80 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
682
699
|
/** `[Standard]` Creates an Any type */
|
|
683
700
|
Any(options?: SchemaOptions): TAny;
|
|
684
701
|
/** `[Standard]` Creates an Array type */
|
|
685
|
-
Array<T extends TSchema>(
|
|
702
|
+
Array<T extends TSchema>(schema: T, options?: ArrayOptions): TArray<T>;
|
|
686
703
|
/** `[Standard]` Creates a Boolean type */
|
|
687
704
|
Boolean(options?: SchemaOptions): TBoolean;
|
|
688
|
-
/** `[Standard]`
|
|
705
|
+
/** `[Standard]` Capitalize a LiteralString type */
|
|
706
|
+
Capitalize<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Capitalize<T['const']>>;
|
|
707
|
+
/** `[Standard]` Creates a Composite object type */
|
|
689
708
|
Composite<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TComposite<T>;
|
|
690
709
|
/** `[Standard]` Creates a Enum type */
|
|
691
710
|
Enum<T extends Record<string, string | number>>(item: T, options?: SchemaOptions): TEnum<T>;
|
|
692
|
-
/** `[Standard]`
|
|
711
|
+
/** `[Standard]` Creates a Conditional type */
|
|
693
712
|
Extends<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema>(left: L, right: R, trueType: T, falseType: U, options?: SchemaOptions): TExtends<L, R, T, U>;
|
|
694
|
-
/** `[Standard]`
|
|
695
|
-
Exclude<L extends TSchema, R extends TSchema>(
|
|
696
|
-
/** `[Standard]`
|
|
697
|
-
Extract<L extends TSchema, R extends TSchema>(
|
|
698
|
-
/** `[Standard]` Returns
|
|
713
|
+
/** `[Standard]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
|
|
714
|
+
Exclude<L extends TSchema, R extends TSchema>(unionType: L, excludedMembers: R, options?: SchemaOptions): TExclude<L, R>;
|
|
715
|
+
/** `[Standard]` Constructs a type by extracting from type all union members that are assignable to union */
|
|
716
|
+
Extract<L extends TSchema, R extends TSchema>(type: L, union: R, options?: SchemaOptions): TExtract<L, R>;
|
|
717
|
+
/** `[Standard]` Returns an Indexed property type for the given keys */
|
|
699
718
|
Index<T extends TTuple, K extends TNumber>(schema: T, keys: K, options?: SchemaOptions): UnionType<Assert<T['items'], TSchema[]>>;
|
|
700
|
-
/** `[Standard]` Returns
|
|
719
|
+
/** `[Standard]` Returns an Indexed property type for the given keys */
|
|
701
720
|
Index<T extends TArray, K extends TNumber>(schema: T, keys: K, options?: SchemaOptions): AssertType<T['items']>;
|
|
702
|
-
/** `[Standard]` Returns
|
|
721
|
+
/** `[Standard]` Returns an Indexed property type for the given keys */
|
|
703
722
|
Index<T extends TSchema, K extends TTemplateLiteral>(schema: T, keys: K, options?: SchemaOptions): TIndex<T, TTemplateLiteralKeyRest<K>>;
|
|
704
|
-
/** `[Standard]` Returns
|
|
723
|
+
/** `[Standard]` Returns an Indexed property type for the given keys */
|
|
705
724
|
Index<T extends TSchema, K extends TLiteral<Key>>(schema: T, keys: K, options?: SchemaOptions): TIndex<T, [K['const']]>;
|
|
706
|
-
/** `[Standard]` Returns
|
|
725
|
+
/** `[Standard]` Returns an Indexed property type for the given keys */
|
|
707
726
|
Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndex<T, Assert<K, Key[]>>;
|
|
708
|
-
/** `[Standard]` Returns
|
|
727
|
+
/** `[Standard]` Returns an Indexed property type for the given keys */
|
|
709
728
|
Index<T extends TSchema, K extends TUnion<TLiteral<Key>[]>>(schema: T, keys: K, options?: SchemaOptions): TIndex<T, TUnionLiteralKeyRest<K>>;
|
|
710
|
-
/** `[Standard]` Returns
|
|
729
|
+
/** `[Standard]` Returns an Indexed property type for the given keys */
|
|
711
730
|
Index<T extends TSchema, K extends TSchema>(schema: T, key: K, options?: SchemaOptions): TSchema;
|
|
712
731
|
/** `[Standard]` Creates an Integer type */
|
|
713
732
|
Integer(options?: NumericOptions<number>): TInteger;
|
|
714
|
-
/** `[Standard]` Creates
|
|
733
|
+
/** `[Standard]` Creates an Intersect type */
|
|
715
734
|
Intersect(allOf: [], options?: SchemaOptions): TNever;
|
|
716
|
-
/** `[Standard]` Creates
|
|
735
|
+
/** `[Standard]` Creates an Intersect type */
|
|
717
736
|
Intersect<T extends [TSchema]>(allOf: [...T], options?: SchemaOptions): T[0];
|
|
737
|
+
/** `[Standard]` Creates an Intersect type */
|
|
718
738
|
Intersect<T extends TSchema[]>(allOf: [...T], options?: IntersectOptions): TIntersect<T>;
|
|
719
739
|
/** `[Standard]` Creates a KeyOf type */
|
|
720
740
|
KeyOf<T extends TSchema>(schema: T, options?: SchemaOptions): TKeyOf<T>;
|
|
721
741
|
/** `[Standard]` Creates a Literal type */
|
|
722
742
|
Literal<T extends TLiteralValue>(value: T, options?: SchemaOptions): TLiteral<T>;
|
|
743
|
+
/** `[Standard]` Lowercase a LiteralString type */
|
|
744
|
+
Lowercase<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Lowercase<T['const']>>;
|
|
723
745
|
/** `[Standard]` Creates a Never type */
|
|
724
746
|
Never(options?: SchemaOptions): TNever;
|
|
725
747
|
/** `[Standard]` Creates a Not type */
|
|
726
|
-
Not<T extends TSchema>(
|
|
748
|
+
Not<T extends TSchema>(schema: T, options?: SchemaOptions): TNot<T>;
|
|
727
749
|
/** `[Standard]` Creates a Null type */
|
|
728
750
|
Null(options?: SchemaOptions): TNull;
|
|
729
751
|
/** `[Standard]` Creates a Number type */
|
|
730
752
|
Number(options?: NumericOptions<number>): TNumber;
|
|
731
753
|
/** `[Standard]` Creates an Object type */
|
|
732
754
|
Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>;
|
|
733
|
-
/** `[Standard]`
|
|
755
|
+
/** `[Standard]` Constructs a type whose keys are omitted from the given type */
|
|
734
756
|
Omit<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TOmit<T, K[number]>;
|
|
735
|
-
/** `[Standard]`
|
|
757
|
+
/** `[Standard]` Constructs a type whose keys are omitted from the given type */
|
|
736
758
|
Omit<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T, TUnionLiteralKeyRest<K>[number]>;
|
|
737
|
-
/** `[Standard]`
|
|
759
|
+
/** `[Standard]` Constructs a type whose keys are omitted from the given type */
|
|
738
760
|
Omit<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TOmit<T, K['const']>;
|
|
739
|
-
/** `[Standard]`
|
|
761
|
+
/** `[Standard]` Constructs a type whose keys are omitted from the given type */
|
|
740
762
|
Omit<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TOmit<T, TTemplateLiteralKeyRest<K>[number]>;
|
|
741
|
-
/** `[Standard]`
|
|
763
|
+
/** `[Standard]` Constructs a type whose keys are omitted from the given type */
|
|
742
764
|
Omit<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TOmit<T, never>;
|
|
743
|
-
/** `[Standard]`
|
|
765
|
+
/** `[Standard]` Constructs a type where all properties are optional */
|
|
744
766
|
Partial<T extends TSchema>(schema: T, options?: ObjectOptions): TPartial<T>;
|
|
745
|
-
/** `[Standard]`
|
|
767
|
+
/** `[Standard]` Constructs a type whose keys are picked from the given type */
|
|
746
768
|
Pick<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TPick<T, K[number]>;
|
|
747
|
-
/** `[Standard]`
|
|
769
|
+
/** `[Standard]` Constructs a type whose keys are picked from the given type */
|
|
748
770
|
Pick<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T, TUnionLiteralKeyRest<K>[number]>;
|
|
749
|
-
/** `[Standard]`
|
|
771
|
+
/** `[Standard]` Constructs a type whose keys are picked from the given type */
|
|
750
772
|
Pick<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TPick<T, K['const']>;
|
|
751
|
-
/** `[Standard]`
|
|
773
|
+
/** `[Standard]` Constructs a type whose keys are picked from the given type */
|
|
752
774
|
Pick<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TPick<T, TTemplateLiteralKeyRest<K>[number]>;
|
|
753
|
-
/** `[Standard]`
|
|
775
|
+
/** `[Standard]` Constructs a type whose keys are picked from the given type */
|
|
754
776
|
Pick<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TPick<T, never>;
|
|
755
777
|
/** `[Standard]` Creates a Record type */
|
|
756
778
|
Record<K extends TUnion, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordUnionLiteralType<K, T>;
|
|
@@ -768,34 +790,40 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
768
790
|
Ref<T extends TSchema>(schema: T, options?: SchemaOptions): TRef<T>;
|
|
769
791
|
/** `[Standard]` Creates a Ref type. */
|
|
770
792
|
Ref<T extends TSchema>($ref: string, options?: SchemaOptions): TRef<T>;
|
|
771
|
-
/** `[Standard]`
|
|
793
|
+
/** `[Standard]` Constructs a type where all properties are required */
|
|
772
794
|
Required<T extends TSchema>(schema: T, options?: SchemaOptions): TRequired<T>;
|
|
773
|
-
/** `[Standard]`
|
|
795
|
+
/** `[Standard]` Extracts the rest array from a Tuple */
|
|
774
796
|
Rest<T extends TSchema>(schema: T): TRest<T>;
|
|
775
797
|
/** `[Standard]` Creates a String type */
|
|
776
798
|
String(options?: StringOptions): TString;
|
|
777
|
-
/** `[Standard]` Creates a
|
|
799
|
+
/** `[Standard]` Creates a TemplateLiteral type from template dsl string */
|
|
778
800
|
TemplateLiteral<T extends string>(templateDsl: T, options?: SchemaOptions): TTemplateLiteralDslParser<T>;
|
|
779
|
-
/** `[Standard]` Creates a
|
|
801
|
+
/** `[Standard]` Creates a TemplateLiteral type */
|
|
780
802
|
TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T], options?: SchemaOptions): TTemplateLiteral<T>;
|
|
781
803
|
/** `[Standard]` Creates a Tuple type */
|
|
782
804
|
Tuple<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TTuple<T>;
|
|
805
|
+
/** `[Standard]` Uncapitalize a LiteralString type */
|
|
806
|
+
Uncapitalize<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Uncapitalize<T['const']>>;
|
|
783
807
|
/** `[Standard]` Creates a Union type */
|
|
784
808
|
Union(anyOf: [], options?: SchemaOptions): TNever;
|
|
785
809
|
/** `[Standard]` Creates a Union type */
|
|
786
810
|
Union<T extends [TSchema]>(anyOf: [...T], options?: SchemaOptions): T[0];
|
|
787
811
|
/** `[Standard]` Creates a Union type */
|
|
788
812
|
Union<T extends TSchema[]>(anyOf: [...T], options?: SchemaOptions): TUnion<T>;
|
|
789
|
-
/** `[Experimental]`
|
|
813
|
+
/** `[Experimental]` Converts a TemplateLiteral into a Union */
|
|
790
814
|
Union<T extends TTemplateLiteral>(template: T): TUnionTemplateLiteral<T>;
|
|
791
815
|
/** `[Standard]` Creates an Unknown type */
|
|
792
816
|
Unknown(options?: SchemaOptions): TUnknown;
|
|
793
|
-
/** `[Standard]` Creates a Unsafe type that infers
|
|
817
|
+
/** `[Standard]` Creates a Unsafe type that will infers as the generic argument T */
|
|
794
818
|
Unsafe<T>(options?: UnsafeOptions): TUnsafe<T>;
|
|
819
|
+
/** `[Standard]` Uppercase a LiteralString type */
|
|
820
|
+
Uppercase<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Uppercase<T['const']>>;
|
|
795
821
|
}
|
|
796
822
|
export declare class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
797
823
|
/** `[Extended]` Creates a AsyncIterator type */
|
|
798
824
|
AsyncIterator<T extends TSchema>(items: T, options?: SchemaOptions): TAsyncIterator<T>;
|
|
825
|
+
/** `[Extended]` Constructs a type by recursively unwrapping Promise types */
|
|
826
|
+
Awaited<T extends TSchema>(schema: T, options?: SchemaOptions): TAwaited<T>;
|
|
799
827
|
/** `[Extended]` Creates a BigInt type */
|
|
800
828
|
BigInt(options?: NumericOptions<bigint>): TBigInt;
|
|
801
829
|
/** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
|
|
@@ -806,7 +834,7 @@ export declare class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
806
834
|
Date(options?: DateOptions): TDate;
|
|
807
835
|
/** `[Extended]` Creates a Function type */
|
|
808
836
|
Function<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TFunction<T, U>;
|
|
809
|
-
/** `[Extended]` Extracts the InstanceType from the given Constructor */
|
|
837
|
+
/** `[Extended]` Extracts the InstanceType from the given Constructor type */
|
|
810
838
|
InstanceType<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TInstanceType<T>;
|
|
811
839
|
/** `[Extended]` Creates an Iterator type */
|
|
812
840
|
Iterator<T extends TSchema>(items: T, options?: SchemaOptions): TIterator<T>;
|
|
@@ -814,15 +842,15 @@ export declare class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
814
842
|
Parameters<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TParameters<T>;
|
|
815
843
|
/** `[Extended]` Creates a Promise type */
|
|
816
844
|
Promise<T extends TSchema>(item: T, options?: SchemaOptions): TPromise<T>;
|
|
817
|
-
/** `[Extended]` Creates a String
|
|
845
|
+
/** `[Extended]` Creates a String type from a Regular Expression pattern */
|
|
818
846
|
RegExp(pattern: string, options?: SchemaOptions): TString;
|
|
819
|
-
/** `[Extended]` Creates a String
|
|
847
|
+
/** `[Extended]` Creates a String type from a Regular Expression */
|
|
820
848
|
RegExp(regex: RegExp, options?: SchemaOptions): TString;
|
|
821
849
|
/**
|
|
822
850
|
* @deprecated Use `Type.RegExp`
|
|
823
851
|
*/
|
|
824
852
|
RegEx(regex: RegExp, options?: SchemaOptions): TString;
|
|
825
|
-
/** `[Extended]` Extracts the ReturnType from the given Function */
|
|
853
|
+
/** `[Extended]` Extracts the ReturnType from the given Function type */
|
|
826
854
|
ReturnType<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TReturnType<T>;
|
|
827
855
|
/** `[Extended]` Creates a Symbol type */
|
|
828
856
|
Symbol(options?: SchemaOptions): TSymbol;
|
package/typebox.js
CHANGED
|
@@ -222,12 +222,12 @@ var TypeGuard;
|
|
|
222
222
|
schema.type === 'array' &&
|
|
223
223
|
IsOptionalString(schema.$id) &&
|
|
224
224
|
TSchema(schema.items) &&
|
|
225
|
-
IsOptionalSchema(schema.contains) &&
|
|
226
|
-
IsOptionalNumber(schema.minContains) &&
|
|
227
|
-
IsOptionalNumber(schema.maxContains) &&
|
|
228
225
|
IsOptionalNumber(schema.minItems) &&
|
|
229
226
|
IsOptionalNumber(schema.maxItems) &&
|
|
230
|
-
IsOptionalBoolean(schema.uniqueItems)
|
|
227
|
+
IsOptionalBoolean(schema.uniqueItems) &&
|
|
228
|
+
IsOptionalSchema(schema.contains) &&
|
|
229
|
+
IsOptionalNumber(schema.minContains) &&
|
|
230
|
+
IsOptionalNumber(schema.maxContains));
|
|
231
231
|
}
|
|
232
232
|
TypeGuard.TArray = TArray;
|
|
233
233
|
/** Returns true if the given schema is TAsyncIterator */
|
|
@@ -1945,14 +1945,19 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1945
1945
|
return this.Create({ ...options, [exports.Kind]: 'Any' });
|
|
1946
1946
|
}
|
|
1947
1947
|
/** `[Standard]` Creates an Array type */
|
|
1948
|
-
Array(
|
|
1949
|
-
return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items: TypeClone.Clone(
|
|
1948
|
+
Array(schema, options = {}) {
|
|
1949
|
+
return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items: TypeClone.Clone(schema) });
|
|
1950
1950
|
}
|
|
1951
1951
|
/** `[Standard]` Creates a Boolean type */
|
|
1952
1952
|
Boolean(options = {}) {
|
|
1953
1953
|
return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
|
|
1954
1954
|
}
|
|
1955
|
-
/** `[Standard]`
|
|
1955
|
+
/** `[Standard]` Capitalize a LiteralString type */
|
|
1956
|
+
Capitalize(schema, options = {}) {
|
|
1957
|
+
const [first, rest] = [schema.const.slice(0, 1), schema.const.slice(1)];
|
|
1958
|
+
return exports.Type.Literal(`${first.toUpperCase()}${rest}`, options);
|
|
1959
|
+
}
|
|
1960
|
+
/** `[Standard]` Creates a Composite object type */
|
|
1956
1961
|
Composite(objects, options) {
|
|
1957
1962
|
const intersect = exports.Type.Intersect(objects, {});
|
|
1958
1963
|
const keys = KeyResolver.ResolveKeys(intersect, { includePatterns: false });
|
|
@@ -1966,7 +1971,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1966
1971
|
const anyOf = values.map((value) => (ValueGuard.IsString(value) ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
|
|
1967
1972
|
return this.Create({ ...options, [exports.Kind]: 'Union', anyOf });
|
|
1968
1973
|
}
|
|
1969
|
-
/** `[Standard]`
|
|
1974
|
+
/** `[Standard]` Creates a Conditional type */
|
|
1970
1975
|
Extends(left, right, trueType, falseType, options = {}) {
|
|
1971
1976
|
switch (TypeExtends.Extends(left, right)) {
|
|
1972
1977
|
case TypeExtendsResult.Union:
|
|
@@ -1977,35 +1982,35 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1977
1982
|
return TypeClone.Clone(falseType, options);
|
|
1978
1983
|
}
|
|
1979
1984
|
}
|
|
1980
|
-
/** `[Standard]`
|
|
1981
|
-
Exclude(
|
|
1982
|
-
if (TypeGuard.TTemplateLiteral(
|
|
1983
|
-
return this.Exclude(TemplateLiteralResolver.Resolve(
|
|
1984
|
-
if (TypeGuard.TTemplateLiteral(
|
|
1985
|
-
return this.Exclude(
|
|
1986
|
-
if (TypeGuard.TUnion(
|
|
1987
|
-
const narrowed =
|
|
1985
|
+
/** `[Standard]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
|
|
1986
|
+
Exclude(unionType, excludedMembers, options = {}) {
|
|
1987
|
+
if (TypeGuard.TTemplateLiteral(unionType))
|
|
1988
|
+
return this.Exclude(TemplateLiteralResolver.Resolve(unionType), excludedMembers, options);
|
|
1989
|
+
if (TypeGuard.TTemplateLiteral(excludedMembers))
|
|
1990
|
+
return this.Exclude(unionType, TemplateLiteralResolver.Resolve(excludedMembers), options);
|
|
1991
|
+
if (TypeGuard.TUnion(unionType)) {
|
|
1992
|
+
const narrowed = unionType.anyOf.filter((inner) => TypeExtends.Extends(inner, excludedMembers) === TypeExtendsResult.False);
|
|
1988
1993
|
return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
|
|
1989
1994
|
}
|
|
1990
1995
|
else {
|
|
1991
|
-
return (TypeExtends.Extends(
|
|
1996
|
+
return (TypeExtends.Extends(unionType, excludedMembers) !== TypeExtendsResult.False ? this.Never(options) : TypeClone.Clone(unionType, options));
|
|
1992
1997
|
}
|
|
1993
1998
|
}
|
|
1994
|
-
/** `[Standard]`
|
|
1995
|
-
Extract(
|
|
1996
|
-
if (TypeGuard.TTemplateLiteral(
|
|
1997
|
-
return this.Extract(TemplateLiteralResolver.Resolve(
|
|
1998
|
-
if (TypeGuard.TTemplateLiteral(
|
|
1999
|
-
return this.Extract(
|
|
2000
|
-
if (TypeGuard.TUnion(
|
|
2001
|
-
const narrowed =
|
|
1999
|
+
/** `[Standard]` Constructs a type by extracting from type all union members that are assignable to union */
|
|
2000
|
+
Extract(type, union, options = {}) {
|
|
2001
|
+
if (TypeGuard.TTemplateLiteral(type))
|
|
2002
|
+
return this.Extract(TemplateLiteralResolver.Resolve(type), union, options);
|
|
2003
|
+
if (TypeGuard.TTemplateLiteral(union))
|
|
2004
|
+
return this.Extract(type, TemplateLiteralResolver.Resolve(union), options);
|
|
2005
|
+
if (TypeGuard.TUnion(type)) {
|
|
2006
|
+
const narrowed = type.anyOf.filter((inner) => TypeExtends.Extends(inner, union) !== TypeExtendsResult.False);
|
|
2002
2007
|
return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
|
|
2003
2008
|
}
|
|
2004
2009
|
else {
|
|
2005
|
-
return (TypeExtends.Extends(
|
|
2010
|
+
return (TypeExtends.Extends(type, union) !== TypeExtendsResult.False ? TypeClone.Clone(type, options) : this.Never(options));
|
|
2006
2011
|
}
|
|
2007
2012
|
}
|
|
2008
|
-
/** `[Standard]` Returns
|
|
2013
|
+
/** `[Standard]` Returns an Indexed property type for the given keys */
|
|
2009
2014
|
Index(schema, unresolved, options = {}) {
|
|
2010
2015
|
if (TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved)) {
|
|
2011
2016
|
return TypeClone.Clone(schema.items, options);
|
|
@@ -2025,6 +2030,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2025
2030
|
Integer(options = {}) {
|
|
2026
2031
|
return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
|
|
2027
2032
|
}
|
|
2033
|
+
/** `[Standard]` Creates an Intersect type */
|
|
2028
2034
|
Intersect(allOf, options = {}) {
|
|
2029
2035
|
if (allOf.length === 0)
|
|
2030
2036
|
return exports.Type.Never();
|
|
@@ -2070,13 +2076,17 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2070
2076
|
Literal(value, options = {}) {
|
|
2071
2077
|
return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
|
|
2072
2078
|
}
|
|
2079
|
+
/** `[Standard]` Lowercase a LiteralString type */
|
|
2080
|
+
Lowercase(schema, options = {}) {
|
|
2081
|
+
return exports.Type.Literal(schema.const.toLowerCase(), options);
|
|
2082
|
+
}
|
|
2073
2083
|
/** `[Standard]` Creates a Never type */
|
|
2074
2084
|
Never(options = {}) {
|
|
2075
2085
|
return this.Create({ ...options, [exports.Kind]: 'Never', not: {} });
|
|
2076
2086
|
}
|
|
2077
2087
|
/** `[Standard]` Creates a Not type */
|
|
2078
|
-
Not(
|
|
2079
|
-
return this.Create({ ...options, [exports.Kind]: 'Not', not });
|
|
2088
|
+
Not(schema, options) {
|
|
2089
|
+
return this.Create({ ...options, [exports.Kind]: 'Not', not: TypeClone.Clone(schema) });
|
|
2080
2090
|
}
|
|
2081
2091
|
/** `[Standard]` Creates a Null type */
|
|
2082
2092
|
Null(options = {}) {
|
|
@@ -2100,6 +2110,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2100
2110
|
return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties });
|
|
2101
2111
|
}
|
|
2102
2112
|
}
|
|
2113
|
+
/** `[Standard]` Constructs a type whose keys are omitted from the given type */
|
|
2103
2114
|
Omit(schema, unresolved, options = {}) {
|
|
2104
2115
|
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
2105
2116
|
// prettier-ignore
|
|
@@ -2116,7 +2127,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2116
2127
|
return this.Create(object);
|
|
2117
2128
|
}, options);
|
|
2118
2129
|
}
|
|
2119
|
-
/** `[Standard]`
|
|
2130
|
+
/** `[Standard]` Constructs a type where all properties are optional */
|
|
2120
2131
|
Partial(schema, options = {}) {
|
|
2121
2132
|
// prettier-ignore
|
|
2122
2133
|
return ObjectMap.Map(schema, (object) => {
|
|
@@ -2126,6 +2137,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2126
2137
|
return this.Object(properties, this.Discard(object, 'required') /* object used as options to retain other constraints */);
|
|
2127
2138
|
}, options);
|
|
2128
2139
|
}
|
|
2140
|
+
/** `[Standard]` Constructs a type whose keys are picked from the given type */
|
|
2129
2141
|
Pick(schema, unresolved, options = {}) {
|
|
2130
2142
|
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
2131
2143
|
// prettier-ignore
|
|
@@ -2194,7 +2206,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2194
2206
|
throw Error('StandardTypeBuilder.Ref: Target type must specify an $id');
|
|
2195
2207
|
return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: unresolved.$id });
|
|
2196
2208
|
}
|
|
2197
|
-
/** `[Standard]`
|
|
2209
|
+
/** `[Standard]` Constructs a type where all properties are required */
|
|
2198
2210
|
Required(schema, options = {}) {
|
|
2199
2211
|
// prettier-ignore
|
|
2200
2212
|
return ObjectMap.Map(schema, (object) => {
|
|
@@ -2204,7 +2216,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2204
2216
|
return this.Object(properties, object /* object used as options to retain other constraints */);
|
|
2205
2217
|
}, options);
|
|
2206
2218
|
}
|
|
2207
|
-
/** `[Standard]`
|
|
2219
|
+
/** `[Standard]` Extracts the rest array from a Tuple */
|
|
2208
2220
|
Rest(schema) {
|
|
2209
2221
|
if (TypeGuard.TTuple(schema)) {
|
|
2210
2222
|
if (ValueGuard.IsUndefined(schema.items))
|
|
@@ -2219,7 +2231,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2219
2231
|
String(options = {}) {
|
|
2220
2232
|
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
|
|
2221
2233
|
}
|
|
2222
|
-
/** `[Standard]` Creates a
|
|
2234
|
+
/** `[Standard]` Creates a TemplateLiteral type */
|
|
2223
2235
|
TemplateLiteral(unresolved, options = {}) {
|
|
2224
2236
|
// prettier-ignore
|
|
2225
2237
|
const pattern = (ValueGuard.IsString(unresolved))
|
|
@@ -2237,6 +2249,12 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2237
2249
|
{ ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
|
|
2238
2250
|
return this.Create(schema);
|
|
2239
2251
|
}
|
|
2252
|
+
/** `[Standard]` Uncapitalize a LiteralString type */
|
|
2253
|
+
Uncapitalize(schema, options = {}) {
|
|
2254
|
+
const [first, rest] = [schema.const.slice(0, 1), schema.const.slice(1)];
|
|
2255
|
+
return exports.Type.Literal(`${first.toLocaleLowerCase()}${rest}`, options);
|
|
2256
|
+
}
|
|
2257
|
+
/** `[Standard]` Creates a Union type */
|
|
2240
2258
|
Union(union, options = {}) {
|
|
2241
2259
|
if (TypeGuard.TTemplateLiteral(union)) {
|
|
2242
2260
|
return TemplateLiteralResolver.Resolve(union);
|
|
@@ -2255,10 +2273,14 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2255
2273
|
Unknown(options = {}) {
|
|
2256
2274
|
return this.Create({ ...options, [exports.Kind]: 'Unknown' });
|
|
2257
2275
|
}
|
|
2258
|
-
/** `[Standard]` Creates a Unsafe type that infers
|
|
2276
|
+
/** `[Standard]` Creates a Unsafe type that will infers as the generic argument T */
|
|
2259
2277
|
Unsafe(options = {}) {
|
|
2260
2278
|
return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
|
|
2261
2279
|
}
|
|
2280
|
+
/** `[Standard]` Uppercase a LiteralString type */
|
|
2281
|
+
Uppercase(schema, options = {}) {
|
|
2282
|
+
return exports.Type.Literal(schema.const.toUpperCase(), options);
|
|
2283
|
+
}
|
|
2262
2284
|
}
|
|
2263
2285
|
exports.StandardTypeBuilder = StandardTypeBuilder;
|
|
2264
2286
|
// --------------------------------------------------------------------------
|
|
@@ -2269,6 +2291,20 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
2269
2291
|
AsyncIterator(items, options = {}) {
|
|
2270
2292
|
return this.Create({ ...options, [exports.Kind]: 'AsyncIterator', type: 'AsyncIterator', items: TypeClone.Clone(items) });
|
|
2271
2293
|
}
|
|
2294
|
+
/** `[Extended]` Constructs a type by recursively unwrapping Promise types */
|
|
2295
|
+
Awaited(schema, options = {}) {
|
|
2296
|
+
const AwaitedRest = (rest) => {
|
|
2297
|
+
if (rest.length === 0)
|
|
2298
|
+
return rest;
|
|
2299
|
+
const [L, ...R] = rest;
|
|
2300
|
+
return [this.Awaited(L), ...AwaitedRest(R)];
|
|
2301
|
+
};
|
|
2302
|
+
// prettier-ignore
|
|
2303
|
+
return (TypeGuard.TIntersect(schema) ? exports.Type.Intersect(AwaitedRest(schema.allOf)) :
|
|
2304
|
+
TypeGuard.TUnion(schema) ? exports.Type.Union(AwaitedRest(schema.anyOf)) :
|
|
2305
|
+
TypeGuard.TPromise(schema) ? this.Awaited(schema.item) :
|
|
2306
|
+
TypeClone.Clone(schema, options));
|
|
2307
|
+
}
|
|
2272
2308
|
/** `[Extended]` Creates a BigInt type */
|
|
2273
2309
|
BigInt(options = {}) {
|
|
2274
2310
|
return this.Create({ ...options, [exports.Kind]: 'BigInt', type: 'bigint' });
|
|
@@ -2293,7 +2329,7 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
2293
2329
|
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter));
|
|
2294
2330
|
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'function', parameters: clonedParameters, returns: clonedReturns });
|
|
2295
2331
|
}
|
|
2296
|
-
/** `[Extended]` Extracts the InstanceType from the given Constructor */
|
|
2332
|
+
/** `[Extended]` Extracts the InstanceType from the given Constructor type */
|
|
2297
2333
|
InstanceType(schema, options = {}) {
|
|
2298
2334
|
return TypeClone.Clone(schema.returns, options);
|
|
2299
2335
|
}
|
|
@@ -2309,7 +2345,7 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
2309
2345
|
Promise(item, options = {}) {
|
|
2310
2346
|
return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'Promise', item: TypeClone.Clone(item) });
|
|
2311
2347
|
}
|
|
2312
|
-
/** `[Extended]` Creates a String
|
|
2348
|
+
/** `[Extended]` Creates a String type */
|
|
2313
2349
|
RegExp(unresolved, options = {}) {
|
|
2314
2350
|
const pattern = ValueGuard.IsString(unresolved) ? unresolved : unresolved.source;
|
|
2315
2351
|
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern });
|
|
@@ -2320,7 +2356,7 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
2320
2356
|
RegEx(regex, options = {}) {
|
|
2321
2357
|
return this.RegExp(regex, options);
|
|
2322
2358
|
}
|
|
2323
|
-
/** `[Extended]` Extracts the ReturnType from the given Function */
|
|
2359
|
+
/** `[Extended]` Extracts the ReturnType from the given Function type */
|
|
2324
2360
|
ReturnType(schema, options = {}) {
|
|
2325
2361
|
return TypeClone.Clone(schema.returns, options);
|
|
2326
2362
|
}
|
package/value/convert.js
CHANGED
|
@@ -280,7 +280,7 @@ function TUnknown(schema, references, value) {
|
|
|
280
280
|
function TVoid(schema, references, value) {
|
|
281
281
|
return value;
|
|
282
282
|
}
|
|
283
|
-
function
|
|
283
|
+
function TKind(schema, references, value) {
|
|
284
284
|
return value;
|
|
285
285
|
}
|
|
286
286
|
function Visit(schema, references, value) {
|
|
@@ -348,7 +348,7 @@ function Visit(schema, references, value) {
|
|
|
348
348
|
default:
|
|
349
349
|
if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
|
|
350
350
|
throw new ValueConvertUnknownTypeError(schema_);
|
|
351
|
-
return
|
|
351
|
+
return TKind(schema_, references_, value);
|
|
352
352
|
}
|
|
353
353
|
}
|
|
354
354
|
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|