@sinclair/typebox 0.31.12 → 0.31.14

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.31.12",
3
+ "version": "0.31.14",
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 | 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 | TEnum | 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;
@@ -160,10 +160,12 @@ export interface TDate extends TSchema, DateOptions {
160
160
  export type TEnumRecord = Record<TEnumKey, TEnumValue>;
161
161
  export type TEnumValue = string | number;
162
162
  export type TEnumKey = string;
163
- export type TEnumToLiteralUnion<T extends TEnumValue> = T extends TEnumValue ? (string extends T ? TNever : TLiteral<T>) : never;
164
- export type TEnumToLiteralTuple<T extends TEnumValue> = UnionToTuple<TEnumToLiteralUnion<T>>;
165
- export type TEnumToUnion<T extends TEnumValue> = UnionType<AssertRest<TEnumToLiteralTuple<T>>>;
166
- export type TEnum<T extends TEnumRecord> = TEnumToUnion<T[keyof T]>;
163
+ export interface TEnum<T extends Record<string, string | number> = Record<string, string | number>> extends TSchema {
164
+ [Kind]: 'Union';
165
+ [Hint]: 'Enum';
166
+ static: T[keyof T];
167
+ anyOf: TLiteral<T[keyof T]>[];
168
+ }
167
169
  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;
168
170
  export type TExcludeTemplateLiteralResult<T extends string> = UnionType<AssertRest<UnionToTuple<{
169
171
  [K in T]: TLiteral<K>;
@@ -438,7 +440,7 @@ export type DecodeProperties<T extends TProperties> = {
438
440
  [K in keyof T]: DecodeType<T[K]>;
439
441
  };
440
442
  export type DecodeRest<T extends TSchema[]> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? [DecodeType<L>, ...DecodeRest<R>] : [];
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);
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 TEnum<infer S> ? TEnum<S> : 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);
442
444
  export type TransformFunction<T = any, U = any> = (value: T) => U;
443
445
  export interface TransformOptions<I extends TSchema = TSchema, O extends unknown = unknown> {
444
446
  Decode: TransformFunction<StaticDecode<I>, O>;
package/typebox.js CHANGED
@@ -1934,13 +1934,11 @@ 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.Never(options);
1939
1937
  // prettier-ignore
1940
1938
  const values1 = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
1941
1939
  const values2 = [...new Set(values1)];
1942
1940
  const anyOf = values2.map((value) => exports.Type.Literal(value));
1943
- return this.Union(anyOf, options);
1941
+ return this.Union(anyOf, { ...options, [exports.Hint]: 'Enum' });
1944
1942
  }
1945
1943
  /** `[Json]` Creates a Conditional type */
1946
1944
  Extends(left, right, trueType, falseType, options = {}) {
package/value/convert.js CHANGED
@@ -180,6 +180,12 @@ function TDate(schema, references, value) {
180
180
  function TInteger(schema, references, value) {
181
181
  return TryConvertInteger(value);
182
182
  }
183
+ function TIntersect(schema, references, value) {
184
+ // prettier-ignore
185
+ return (schema.allOf.every(schema => Types.TypeGuard.TObject(schema)))
186
+ ? Visit(Types.Type.Composite(schema.allOf), references, value)
187
+ : Visit(schema.allOf[0], references, value);
188
+ }
183
189
  function TLiteral(schema, references, value) {
184
190
  return TryConvertLiteral(schema, value);
185
191
  }
@@ -254,6 +260,8 @@ function Visit(schema, references, value) {
254
260
  return TDate(schema_, references_, value);
255
261
  case 'Integer':
256
262
  return TInteger(schema_, references_, value);
263
+ case 'Intersect':
264
+ return TIntersect(schema_, references_, value);
257
265
  case 'Literal':
258
266
  return TLiteral(schema_, references_, value);
259
267
  case 'Null':
@@ -285,7 +293,6 @@ function Visit(schema, references, value) {
285
293
  case 'AsyncIterator':
286
294
  case 'Constructor':
287
295
  case 'Function':
288
- case 'Intersect':
289
296
  case 'Iterator':
290
297
  case 'Never':
291
298
  case 'Promise':