@sinclair/typebox 0.31.8 → 0.31.10

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 (3) hide show
  1. package/package.json +1 -1
  2. package/typebox.d.ts +10 -12
  3. package/typebox.js +6 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.31.8",
3
+ "version": "0.31.10",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/typebox.d.ts CHANGED
@@ -72,7 +72,7 @@ export interface TSchema extends SchemaOptions, TKind {
72
72
  params: unknown[];
73
73
  static: unknown;
74
74
  }
75
- export type TAnySchema = TSchema | TAny | TArray | TAsyncIterator | TBigInt | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TIntersect | TIterator | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TString | TSymbol | TTemplateLiteral | TThis | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
75
+ export type TAnySchema = TSchema | TAny | TArray | TAsyncIterator | TBigInt | TBoolean | TConstructor | TDate | TFunction | TInteger | TIntersect | TIterator | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TString | TSymbol | TTemplateLiteral | TThis | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
76
76
  export interface NumericOptions<N extends number | bigint> extends SchemaOptions {
77
77
  exclusiveMaximum?: N;
78
78
  exclusiveMinimum?: N;
@@ -157,15 +157,13 @@ export interface TDate extends TSchema, DateOptions {
157
157
  static: Date;
158
158
  type: 'date';
159
159
  }
160
- export interface TEnumOption<T> {
161
- type: 'number' | 'string';
162
- const: T;
163
- }
164
- export interface TEnum<T extends Record<string, string | number> = Record<string, string | number>> extends TSchema {
165
- [Kind]: 'Union';
166
- static: T[keyof T];
167
- anyOf: TLiteral<T[keyof T]>[];
168
- }
160
+ export type TEnumRecord = Record<TEnumKey, TEnumValue>;
161
+ export type TEnumValue = string | number;
162
+ export type TEnumKey = string;
163
+ export type TEnumToLiteralUnion<T extends TEnumValue> = T extends TEnumValue ? TLiteral<T> : never;
164
+ export type TEnumToLiteralTuple<T extends TEnumValue> = UnionToTuple<TEnumToLiteralUnion<T>>;
165
+ export type TEnumToUnion<T extends TEnumValue, R = UnionType<AssertRest<TEnumToLiteralTuple<T>>>> = R extends TLiteralString ? TNever : R;
166
+ export type TEnum<T extends TEnumValue> = Ensure<TEnumToUnion<T>>;
169
167
  export type TExtends<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema> = (Static<L> extends Static<R> ? T : U) extends infer O ? UnionToTuple<O> extends [infer X, infer Y] ? TUnion<[AssertType<X>, AssertType<Y>]> : AssertType<O> : never;
170
168
  export type TExcludeTemplateLiteralResult<T extends string> = UnionType<AssertRest<UnionToTuple<{
171
169
  [K in T]: TLiteral<K>;
@@ -440,7 +438,7 @@ export type DecodeProperties<T extends TProperties> = {
440
438
  [K in keyof T]: DecodeType<T[K]>;
441
439
  };
442
440
  export type DecodeRest<T extends TSchema[]> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? [DecodeType<L>, ...DecodeRest<R>] : [];
443
- export type DecodeType<T extends TSchema> = (T extends TOptional<infer S extends TSchema> ? TOptional<DecodeType<S>> : T extends TReadonly<infer S extends TSchema> ? TReadonly<DecodeType<S>> : T extends TTransform<infer _, infer R> ? TUnsafe<R> : T extends TArray<infer S extends TSchema> ? TArray<DecodeType<S>> : T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<DecodeType<S>> : T extends TConstructor<infer P extends TSchema[], infer R extends TSchema> ? TConstructor<P, DecodeType<R>> : T extends TFunction<infer P extends TSchema[], infer R extends TSchema> ? TFunction<P, DecodeType<R>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<S> : T extends TIterator<infer S extends TSchema> ? TIterator<DecodeType<S>> : T extends TNot<infer S extends TSchema> ? TNot<DecodeType<S>> : T extends TObject<infer S> ? TObject<Evaluate<DecodeProperties<S>>> : T extends TPromise<infer S extends TSchema> ? TPromise<DecodeType<S>> : T extends TRecord<infer K, infer S> ? TRecord<K, DecodeType<S>> : T extends TRecursive<infer S extends TSchema> ? TRecursive<DecodeType<S>> : T extends TRef<infer S extends TSchema> ? TRef<DecodeType<S>> : T extends TTuple<infer S extends TSchema[]> ? TTuple<S> : T extends TUnion<infer S extends TSchema[]> ? TUnion<S> : T);
441
+ export type DecodeType<T extends TSchema> = (T extends TOptional<infer S extends TSchema> ? TOptional<DecodeType<S>> : T extends TReadonly<infer S extends TSchema> ? TReadonly<DecodeType<S>> : T extends TTransform<infer _, infer R> ? TUnsafe<R> : T extends TArray<infer S extends TSchema> ? TArray<DecodeType<S>> : T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<DecodeType<S>> : T extends TConstructor<infer P extends TSchema[], infer R extends TSchema> ? TConstructor<P, DecodeType<R>> : T extends TFunction<infer P extends TSchema[], infer R extends TSchema> ? TFunction<P, DecodeType<R>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<DecodeRest<S>> : T extends TIterator<infer S extends TSchema> ? TIterator<DecodeType<S>> : T extends TNot<infer S extends TSchema> ? TNot<DecodeType<S>> : T extends TObject<infer S> ? TObject<Evaluate<DecodeProperties<S>>> : T extends TPromise<infer S extends TSchema> ? TPromise<DecodeType<S>> : T extends TRecord<infer K, infer S> ? TRecord<K, DecodeType<S>> : T extends TRecursive<infer S extends TSchema> ? TRecursive<DecodeType<S>> : T extends TRef<infer S extends TSchema> ? TRef<DecodeType<S>> : T extends TTuple<infer S extends TSchema[]> ? TTuple<DecodeRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<DecodeRest<S>> : T);
444
442
  export type TransformFunction<T = any, U = any> = (value: T) => U;
445
443
  export interface TransformOptions<I extends TSchema = TSchema, O extends unknown = unknown> {
446
444
  Decode: TransformFunction<StaticDecode<I>, O>;
@@ -799,7 +797,7 @@ export declare class JsonTypeBuilder extends TypeBuilder {
799
797
  /** `[Json]` Creates a Composite object type */
800
798
  Composite<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TComposite<T>;
801
799
  /** `[Json]` Creates a Enum type */
802
- Enum<V extends string | number, T extends Record<string, V>>(item: T, options?: SchemaOptions): TEnum<T>;
800
+ Enum<V extends TEnumValue, T extends Record<TEnumKey, V>>(item: T, options?: SchemaOptions): TEnum<T[keyof T]>;
803
801
  /** `[Json]` Creates a Conditional type */
804
802
  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>;
805
803
  /** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
package/typebox.js CHANGED
@@ -1934,13 +1934,13 @@ class JsonTypeBuilder extends TypeBuilder {
1934
1934
  }
1935
1935
  /** `[Json]` Creates a Enum type */
1936
1936
  Enum(item, options = {}) {
1937
+ if (ValueGuard.IsUndefined(item))
1938
+ return this.Union([], options);
1937
1939
  // prettier-ignore
1938
- const values = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
1939
- // prettier-ignore
1940
- const anyOf = values.map((value) => ValueGuard.IsString(value)
1941
- ? { [exports.Kind]: 'Literal', type: 'string', const: value }
1942
- : { [exports.Kind]: 'Literal', type: 'number', const: value });
1943
- return this.Create({ ...options, [exports.Kind]: 'Union', anyOf });
1940
+ const values1 = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
1941
+ const values2 = [...new Set(values1)]; // distinct
1942
+ const anyOf = values2.map((value) => exports.Type.Literal(value));
1943
+ return this.Union(anyOf, options);
1944
1944
  }
1945
1945
  /** `[Json]` Creates a Conditional type */
1946
1946
  Extends(left, right, trueType, falseType, options = {}) {