@sinclair/typebox 0.31.20 → 0.31.22
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/typebox.d.ts +4 -2
- package/typebox.js +43 -34
package/package.json
CHANGED
package/typebox.d.ts
CHANGED
|
@@ -337,7 +337,9 @@ export type TRecordFromUnionLiteralString<K extends TLiteralString, T extends TS
|
|
|
337
337
|
export type TRecordFromUnionLiteralNumber<K extends TLiteralNumber, T extends TSchema> = {
|
|
338
338
|
[_ in K['const']]: T;
|
|
339
339
|
};
|
|
340
|
-
export type TRecordFromEnumKey<K extends
|
|
340
|
+
export type TRecordFromEnumKey<K extends Record<string, string | number>, T extends TSchema> = Ensure<TObject<{
|
|
341
|
+
[_ in K[keyof K]]: T;
|
|
342
|
+
}>>;
|
|
341
343
|
export type TRecordFromUnionRest<K extends TSchema[], T extends TSchema> = K extends [infer L, ...infer R] ? (L extends TUnion<infer S> ? TRecordFromUnionRest<S, T> & TRecordFromUnionRest<AssertRest<R>, T> : L extends TLiteralString ? TRecordFromUnionLiteralString<L, T> & TRecordFromUnionRest<AssertRest<R>, T> : L extends TLiteralNumber ? TRecordFromUnionLiteralNumber<L, T> & TRecordFromUnionRest<AssertRest<R>, T> : {}) : {};
|
|
342
344
|
export type TRecordFromUnion<K extends TSchema[], T extends TSchema> = Ensure<TObject<AssertProperties<Evaluate<TRecordFromUnionRest<K, T>>>>>;
|
|
343
345
|
export type TRecordFromTemplateLiteralKeyInfinite<K extends TTemplateLiteral, T extends TSchema> = Ensure<TRecord<K, T>>;
|
|
@@ -354,7 +356,7 @@ export type TRecordFromLiteralNumberKey<K extends TLiteralNumber, T extends TSch
|
|
|
354
356
|
export type TRecordFromStringKey<K extends TString, T extends TSchema> = Ensure<TRecord<K, T>>;
|
|
355
357
|
export type TRecordFromNumberKey<K extends TNumber, T extends TSchema> = Ensure<TRecord<K, T>>;
|
|
356
358
|
export type TRecordFromIntegerKey<K extends TInteger, T extends TSchema> = Ensure<TRecord<K, T>>;
|
|
357
|
-
export type TRecordResolve<K extends TSchema, T extends TSchema> = K extends TEnum<infer
|
|
359
|
+
export type TRecordResolve<K extends TSchema, T extends TSchema> = K extends TEnum<infer S> ? TRecordFromEnumKey<S, T> : K extends TUnion<infer S> ? TRecordFromUnion<S, T> : K extends TTemplateLiteral ? TRecordFromTemplateLiteralKey<K, T> : K extends TLiteralString ? TRecordFromLiteralStringKey<K, T> : K extends TLiteralNumber ? TRecordFromLiteralNumberKey<K, T> : K extends TString ? TRecordFromStringKey<K, T> : K extends TNumber ? TRecordFromNumberKey<K, T> : K extends TInteger ? TRecordFromIntegerKey<K, T> : TNever;
|
|
358
360
|
export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchema> extends TSchema {
|
|
359
361
|
[Kind]: 'Record';
|
|
360
362
|
static: Record<Assert<Static<K>, string | number>, Static<T, this['params']>>;
|
package/typebox.js
CHANGED
|
@@ -224,6 +224,9 @@ var TypeGuard;
|
|
|
224
224
|
function IsOptionalSchema(value) {
|
|
225
225
|
return ValueGuard.IsUndefined(value) || TSchema(value);
|
|
226
226
|
}
|
|
227
|
+
// ----------------------------------------------------------------
|
|
228
|
+
// Types
|
|
229
|
+
// ----------------------------------------------------------------
|
|
227
230
|
/** Returns true if the given value is TAny */
|
|
228
231
|
function TAny(schema) {
|
|
229
232
|
// prettier-ignore
|
|
@@ -587,39 +590,39 @@ var TypeGuard;
|
|
|
587
590
|
TypeGuard.TOptional = TOptional;
|
|
588
591
|
/** Returns true if the given value is TSchema */
|
|
589
592
|
function TSchema(schema) {
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
593
|
+
// prettier-ignore
|
|
594
|
+
return (ValueGuard.IsObject(schema)) && (TAny(schema) ||
|
|
595
|
+
TArray(schema) ||
|
|
596
|
+
TBoolean(schema) ||
|
|
597
|
+
TBigInt(schema) ||
|
|
598
|
+
TAsyncIterator(schema) ||
|
|
599
|
+
TConstructor(schema) ||
|
|
600
|
+
TDate(schema) ||
|
|
601
|
+
TFunction(schema) ||
|
|
602
|
+
TInteger(schema) ||
|
|
603
|
+
TIntersect(schema) ||
|
|
604
|
+
TIterator(schema) ||
|
|
605
|
+
TLiteral(schema) ||
|
|
606
|
+
TNever(schema) ||
|
|
607
|
+
TNot(schema) ||
|
|
608
|
+
TNull(schema) ||
|
|
609
|
+
TNumber(schema) ||
|
|
610
|
+
TObject(schema) ||
|
|
611
|
+
TPromise(schema) ||
|
|
612
|
+
TRecord(schema) ||
|
|
613
|
+
TRef(schema) ||
|
|
614
|
+
TString(schema) ||
|
|
615
|
+
TSymbol(schema) ||
|
|
616
|
+
TTemplateLiteral(schema) ||
|
|
617
|
+
TThis(schema) ||
|
|
618
|
+
TTuple(schema) ||
|
|
619
|
+
TUndefined(schema) ||
|
|
620
|
+
TUnion(schema) ||
|
|
621
|
+
TUint8Array(schema) ||
|
|
622
|
+
TUnknown(schema) ||
|
|
623
|
+
TUnsafe(schema) ||
|
|
624
|
+
TVoid(schema) ||
|
|
625
|
+
(TKind(schema) && TypeRegistry.Has(schema[exports.Kind])));
|
|
623
626
|
}
|
|
624
627
|
TypeGuard.TSchema = TSchema;
|
|
625
628
|
})(TypeGuard || (exports.TypeGuard = TypeGuard = {}));
|
|
@@ -995,8 +998,12 @@ var TypeExtends;
|
|
|
995
998
|
!TypeGuard.TObject(right) ? TypeExtendsResult.False :
|
|
996
999
|
(() => {
|
|
997
1000
|
for (const key of Object.getOwnPropertyNames(right.properties)) {
|
|
998
|
-
if (!(key in left.properties))
|
|
1001
|
+
if (!(key in left.properties) && !TypeGuard.TOptional(right.properties[key])) {
|
|
999
1002
|
return TypeExtendsResult.False;
|
|
1003
|
+
}
|
|
1004
|
+
if (TypeGuard.TOptional(right.properties[key])) {
|
|
1005
|
+
return TypeExtendsResult.True;
|
|
1006
|
+
}
|
|
1000
1007
|
if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
|
|
1001
1008
|
return TypeExtendsResult.False;
|
|
1002
1009
|
}
|
|
@@ -1934,6 +1941,8 @@ class JsonTypeBuilder extends TypeBuilder {
|
|
|
1934
1941
|
}
|
|
1935
1942
|
/** `[Json]` Creates a Enum type */
|
|
1936
1943
|
Enum(item, options = {}) {
|
|
1944
|
+
if (ValueGuard.IsUndefined(item))
|
|
1945
|
+
return this.Throw('Enum undefined or empty');
|
|
1937
1946
|
// prettier-ignore
|
|
1938
1947
|
const values1 = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
|
|
1939
1948
|
const values2 = [...new Set(values1)];
|