@sinclair/typebox 0.28.3 → 0.28.4

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.28.3",
3
+ "version": "0.28.4",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -1476,11 +1476,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
1476
1476
  ┌──────────────────────┬────────────┬────────────┬─────────────┐
1477
1477
  │ (index) │ Compiled │ Minified │ Compression │
1478
1478
  ├──────────────────────┼────────────┼────────────┼─────────────┤
1479
- │ typebox/compiler │ '126.7 kb' │ ' 56.6 kb' │ '2.24 x' │
1480
- │ typebox/errors │ '110.4 kb' │ ' 48.8 kb' │ '2.26 x' │
1481
- │ typebox/system │ ' 75.9 kb' │ ' 31.1 kb' │ '2.44 x' │
1482
- │ typebox/value │ '176.4 kb' │ ' 76.4 kb' │ '2.31 x' │
1483
- │ typebox │ ' 74.8 kb' │ ' 30.7 kb' │ '2.44 x' │
1479
+ │ typebox/compiler │ '127.1 kb' │ ' 56.7 kb' │ '2.24 x' │
1480
+ │ typebox/errors │ '110.9 kb' │ ' 48.9 kb' │ '2.27 x' │
1481
+ │ typebox/system │ ' 76.3 kb' │ ' 31.2 kb' │ '2.44 x' │
1482
+ │ typebox/value │ '176.8 kb' │ ' 76.5 kb' │ '2.31 x' │
1483
+ │ typebox │ ' 75.2 kb' │ ' 30.8 kb' │ '2.44 x' │
1484
1484
  └──────────────────────┴────────────┴────────────┴─────────────┘
1485
1485
  ```
1486
1486
 
package/typebox.d.ts CHANGED
@@ -34,7 +34,7 @@ export type TOptional<T extends TSchema> = T & {
34
34
  export type TReadonlyOptional<T extends TSchema> = T & {
35
35
  [Modifier]: 'ReadonlyOptional';
36
36
  };
37
- export type Key = keyof any;
37
+ export type Key = string | number;
38
38
  export interface SchemaOptions {
39
39
  $schema?: string;
40
40
  /** Id for this schema */
@@ -168,18 +168,20 @@ export interface TFunction<T extends readonly TSchema[] = TSchema[], U extends T
168
168
  parameters: T;
169
169
  returns: U;
170
170
  }
171
- export type IndexKey = keyof any;
172
- export type TIndexProperty<T extends TProperties, K extends IndexKey> = K extends keyof T ? [T[K]] : [
171
+ export type TIndexProperty<T extends TProperties, K extends Key> = T[K] extends infer R ? [R] : [
173
172
  ];
174
- export type TIndexTuple<T extends TSchema[], K extends IndexKey> = K extends keyof T ? [T[K]] : [
173
+ export type TIndexTuple<T extends TSchema[], K extends Key> = K extends keyof T ? [T[K]] : [
175
174
  ];
176
- export type TIndexComposite<T extends TSchema[], K extends IndexKey> = T extends [infer L, ...infer R] ? [...TIndexKey<AssertType<L>, K>, ...TIndexComposite<AssertRest<R>, K>] : [
175
+ export type TIndexComposite<T extends TSchema[], K extends Key> = T extends [infer L, ...infer R] ? [...TIndexKey<AssertType<L>, K>, ...TIndexComposite<AssertRest<R>, K>] : [
177
176
  ];
178
- export type TIndexKey<T extends TSchema, K extends IndexKey> = T extends TRecursive<infer S> ? TIndexKey<S, K> : T extends TIntersect<infer S> ? TIndexComposite<S, K> : T extends TUnion<infer S> ? TIndexComposite<S, K> : T extends TObject<infer S> ? TIndexProperty<S, K> : T extends TTuple<infer S> ? TIndexTuple<S, K> : T extends TArray<infer S> ? S : [
177
+ export type TIndexKey<T extends TSchema, K extends Key> = T extends TRecursive<infer S> ? TIndexKey<S, K> : T extends TIntersect<infer S> ? TIndexComposite<S, K> : T extends TUnion<infer S> ? TIndexComposite<S, K> : T extends TObject<infer S> ? TIndexProperty<S, K> : T extends TTuple<infer S> ? TIndexTuple<S, K> : T extends TArray<infer S> ? S : [
179
178
  ];
180
- export type TIndexKeys<T extends TSchema, K extends IndexKey[]> = K extends [infer L, ...infer R] ? [...TIndexKey<T, Assert<L, IndexKey>>, ...TIndexKeys<T, Assert<R, IndexKey[]>>] : [
179
+ export type TIndexKeys<T extends TSchema, K extends Key[]> = K extends [infer L, ...infer R] ? [
180
+ ...TIndexKey<T, Assert<L, Key>>,
181
+ ...TIndexKeys<T, Assert<R, Key[]>>
182
+ ] : [
181
183
  ];
182
- export type TIndex<T extends TSchema, K extends IndexKey[]> = TIndexKeys<T, K> extends infer R ? T extends TRecursive<infer S> ? TIndex<S, K> : T extends TTuple ? UnionType<AssertRest<R>> : T extends TIntersect ? UnionType<AssertRest<R>> : T extends TUnion ? UnionType<AssertRest<R>> : T extends TObject ? UnionType<AssertRest<R>> : T extends TArray ? UnionType<AssertRest<R>> : TNever : TNever;
184
+ export type TIndex<T extends TSchema, K extends Key[]> = TIndexKeys<T, K> extends infer R ? T extends TRecursive<infer S> ? TIndex<S, K> : T extends TTuple ? UnionType<AssertRest<R>> : T extends TIntersect ? UnionType<AssertRest<R>> : T extends TUnion ? UnionType<AssertRest<R>> : T extends TObject ? UnionType<AssertRest<R>> : T extends TArray ? UnionType<AssertRest<R>> : TNever : TNever;
183
185
  export interface TInteger extends TSchema, NumericOptions<number> {
184
186
  [Kind]: 'Integer';
185
187
  static: number;
@@ -299,7 +301,7 @@ export type RecordTemplateLiteralObjectType<K extends TTemplateLiteral, T extend
299
301
  [_ in Static<K>]: T;
300
302
  }>>>;
301
303
  export type RecordTemplateLiteralType<K extends TTemplateLiteral, T extends TSchema> = IsTemplateLiteralFinite<K> extends true ? RecordTemplateLiteralObjectType<K, T> : TRecord<K, T>;
302
- export type RecordUnionLiteralType<K extends TUnion<TLiteral<string | number>[]>, T extends TSchema> = Static<K> extends string ? Ensure<TObject<{
304
+ export type RecordUnionLiteralType<K extends TUnion, T extends TSchema> = Static<K> extends string ? Ensure<TObject<{
303
305
  [X in Static<K>]: T;
304
306
  }>> : never;
305
307
  export type RecordLiteralType<K extends TLiteral<string | number>, T extends TSchema> = Ensure<TObject<{
@@ -499,8 +501,8 @@ export declare namespace TypeGuard {
499
501
  function TLiteralNumber(schema: unknown): schema is TLiteral<number>;
500
502
  /** Returns true if the given schema is TLiteral<boolean> */
501
503
  function TLiteralBoolean(schema: unknown): schema is TLiteral<boolean>;
502
- /** Returns true if the given schema is TUnion<Literal<string>[]> */
503
- function TLiteralUnion(schema: unknown): schema is TUnion<TLiteral<string>[]>;
504
+ /** Returns true if the given schema is TUnion<Literal<string | number>[]> */
505
+ function TLiteralUnion(schema: unknown): schema is TUnion<TLiteral[]>;
504
506
  /** Returns true if the given schema is TLiteral */
505
507
  function TLiteral(schema: unknown): schema is TLiteral;
506
508
  /** Returns true if the given schema is TNever */
@@ -568,7 +570,7 @@ export declare namespace TypeClone {
568
570
  function Clone<T extends TSchema>(schema: T, options: SchemaOptions): T;
569
571
  }
570
572
  export declare namespace IndexedAccessor {
571
- function Resolve(schema: TSchema, keys: string[]): TSchema[];
573
+ function Resolve(schema: TSchema, keys: (string | number)[]): TSchema[];
572
574
  }
573
575
  export declare namespace ObjectMap {
574
576
  function Map<T = TSchema>(schema: TSchema, callback: (object: TObject) => TObject, options: SchemaOptions): T;
@@ -586,6 +588,10 @@ export declare namespace KeyArrayResolver {
586
588
  /** Resolves an array of string[] keys from the given schema or array type. */
587
589
  function Resolve(schema: TSchema | string[]): string[];
588
590
  }
591
+ export declare namespace UnionResolver {
592
+ /** Returns a resolved union with interior unions flattened */
593
+ function Resolve(union: TUnion): TUnion;
594
+ }
589
595
  export declare namespace TemplateLiteralPattern {
590
596
  function Create(kinds: TTemplateLiteralKind[]): string;
591
597
  }
@@ -651,11 +657,11 @@ export declare class StandardTypeBuilder extends TypeBuilder {
651
657
  /** `[Standard]` Extracts from the left type any type that is assignable to the right */
652
658
  Extract<L extends TSchema, R extends TSchema>(left: L, right: R, options?: SchemaOptions): TExtract<L, R>;
653
659
  /** `[Standard]` Returns indexed property types for the given keys */
654
- Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndex<T, K>;
660
+ Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndex<T, Assert<K, Key[]>>;
655
661
  /** `[Standard]` Returns indexed property types for the given keys */
656
- Index<T extends TSchema, K extends TUnion<TLiteral<string | number>[]>>(schema: T, keys: K, options?: SchemaOptions): TIndex<T, TLiteralUnion<K>>;
662
+ Index<T extends TSchema, K extends TUnion<TLiteral<Key>[]>>(schema: T, keys: K, options?: SchemaOptions): TIndex<T, TLiteralUnion<K>>;
657
663
  /** `[Standard]` Returns indexed property types for the given keys */
658
- Index<T extends TSchema, K extends TLiteral<string | number>>(schema: T, key: K, options?: SchemaOptions): TIndex<T, [K['const']]>;
664
+ Index<T extends TSchema, K extends TLiteral<Key>>(schema: T, key: K, options?: SchemaOptions): TIndex<T, [K['const']]>;
659
665
  /** `[Standard]` Returns indexed property types for the given keys */
660
666
  Index<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TIndex<T, TTemplateLiteralKeyArray<K>>;
661
667
  /** `[Standard]` Returns indexed property types for the given keys */
@@ -708,7 +714,7 @@ export declare class StandardTypeBuilder extends TypeBuilder {
708
714
  /** `[Standard]` Creates a mapped type whose keys are picked from the given type */
709
715
  Pick<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TPick<T, never>;
710
716
  /** `[Standard]` Creates a Record type */
711
- Record<K extends TUnion<TLiteral<string | number>[]>, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordUnionLiteralType<K, T>;
717
+ Record<K extends TUnion, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordUnionLiteralType<K, T>;
712
718
  /** `[Standard]` Creates a Record type */
713
719
  Record<K extends TLiteral<string | number>, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): RecordLiteralType<K, T>;
714
720
  /** `[Standard]` Creates a Record type */
package/typebox.js CHANGED
@@ -27,7 +27,7 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Modifier = void 0;
30
+ exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.UnionResolver = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Modifier = void 0;
31
31
  // --------------------------------------------------------------------------
32
32
  // Symbols
33
33
  // --------------------------------------------------------------------------
@@ -324,9 +324,9 @@ var TypeGuard;
324
324
  return TKind(schema) && schema[exports.Kind] === 'Literal' && IsOptionalString(schema.$id) && typeof schema.const === 'boolean';
325
325
  }
326
326
  TypeGuard.TLiteralBoolean = TLiteralBoolean;
327
- /** Returns true if the given schema is TUnion<Literal<string>[]> */
327
+ /** Returns true if the given schema is TUnion<Literal<string | number>[]> */
328
328
  function TLiteralUnion(schema) {
329
- return TUnion(schema) && schema.anyOf.every((schema) => TLiteral(schema));
329
+ return TUnion(schema) && schema.anyOf.every((schema) => TLiteralString(schema) || TLiteralNumber(schema));
330
330
  }
331
331
  TypeGuard.TLiteralUnion = TLiteralUnion;
332
332
  /** Returns true if the given schema is TLiteral */
@@ -1411,12 +1411,12 @@ var IndexedAccessor;
1411
1411
  return schema.anyOf.reduce((acc, schema) => [...acc, ...Visit(schema, key)], []);
1412
1412
  }
1413
1413
  function Object(schema, key) {
1414
- const keys = globalThis.Object.getOwnPropertyNames(schema.properties).filter((k) => k === key);
1414
+ const keys = globalThis.Object.getOwnPropertyNames(schema.properties).filter((key_) => key_ === key);
1415
1415
  return keys.map((key) => schema.properties[key]);
1416
1416
  }
1417
1417
  function Tuple(schema, key) {
1418
1418
  const items = schema.items === undefined ? [] : schema.items;
1419
- return items.filter((_, index) => index.toString() === key.toString());
1419
+ return items.filter((_, index) => index.toString() === key);
1420
1420
  }
1421
1421
  function Visit(schema, key) {
1422
1422
  if (schema[exports.Kind] === 'Intersect')
@@ -1430,7 +1430,7 @@ var IndexedAccessor;
1430
1430
  return [];
1431
1431
  }
1432
1432
  function Resolve(schema, keys) {
1433
- return keys.reduce((acc, key) => [...acc, ...Visit(schema, key)], []);
1433
+ return keys.reduce((acc, key) => [...acc, ...Visit(schema, key.toString())], []);
1434
1434
  }
1435
1435
  IndexedAccessor.Resolve = Resolve;
1436
1436
  })(IndexedAccessor = exports.IndexedAccessor || (exports.IndexedAccessor = {}));
@@ -1520,7 +1520,7 @@ var KeyArrayResolver;
1520
1520
  if (globalThis.Array.isArray(schema))
1521
1521
  return schema;
1522
1522
  if (TypeGuard.TLiteralUnion(schema))
1523
- return schema.anyOf.map((schema) => schema.const);
1523
+ return schema.anyOf.map((schema) => schema.const.toString());
1524
1524
  if (TypeGuard.TLiteral(schema))
1525
1525
  return [schema.const];
1526
1526
  if (TypeGuard.TTemplateLiteral(schema)) {
@@ -1534,6 +1534,27 @@ var KeyArrayResolver;
1534
1534
  KeyArrayResolver.Resolve = Resolve;
1535
1535
  })(KeyArrayResolver = exports.KeyArrayResolver || (exports.KeyArrayResolver = {}));
1536
1536
  // --------------------------------------------------------------------------
1537
+ // UnionResolver
1538
+ // --------------------------------------------------------------------------
1539
+ var UnionResolver;
1540
+ (function (UnionResolver) {
1541
+ function* Union(union) {
1542
+ for (const schema of union.anyOf) {
1543
+ if (schema[exports.Kind] === 'Union') {
1544
+ yield* Union(schema);
1545
+ }
1546
+ else {
1547
+ yield schema;
1548
+ }
1549
+ }
1550
+ }
1551
+ /** Returns a resolved union with interior unions flattened */
1552
+ function Resolve(union) {
1553
+ return exports.Type.Union([...Union(union)], { ...union });
1554
+ }
1555
+ UnionResolver.Resolve = Resolve;
1556
+ })(UnionResolver = exports.UnionResolver || (exports.UnionResolver = {}));
1557
+ // --------------------------------------------------------------------------
1537
1558
  // TemplateLiteralPattern
1538
1559
  // --------------------------------------------------------------------------
1539
1560
  var TemplateLiteralPattern;
@@ -2115,20 +2136,21 @@ class StandardTypeBuilder extends TypeBuilder {
2115
2136
  ? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(schema, {}) }), {}), options))
2116
2137
  : this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Clone(schema, {}) } });
2117
2138
  }
2118
- else if (TypeGuard.TLiteralUnion(key)) {
2119
- if (key.anyOf.every((schema) => TypeGuard.TLiteral(schema) && (typeof schema.const === 'string' || typeof schema.const === 'number'))) {
2120
- const properties = key.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
2139
+ else if (TypeGuard.TUnion(key)) {
2140
+ const union = UnionResolver.Resolve(key);
2141
+ if (TypeGuard.TLiteralUnion(union)) {
2142
+ const properties = union.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
2121
2143
  return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
2122
2144
  }
2123
2145
  else
2124
- throw Error('TypeBuilder: Record key can only be derived from union literal of number or string');
2146
+ throw Error('TypeBuilder: Record key of type union contains non-literal types');
2125
2147
  }
2126
2148
  else if (TypeGuard.TLiteral(key)) {
2127
2149
  if (typeof key.const === 'string' || typeof key.const === 'number') {
2128
2150
  return this.Object({ [key.const]: TypeClone.Clone(schema, {}) }, options);
2129
2151
  }
2130
2152
  else
2131
- throw Error('TypeBuilder: Record key can only be derived from literals of number or string');
2153
+ throw Error('TypeBuilder: Record key of type literal is not of type string or number');
2132
2154
  }
2133
2155
  else if (TypeGuard.TInteger(key) || TypeGuard.TNumber(key)) {
2134
2156
  const pattern = exports.PatternNumberExact;
@@ -2139,7 +2161,7 @@ class StandardTypeBuilder extends TypeBuilder {
2139
2161
  return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) } });
2140
2162
  }
2141
2163
  else {
2142
- throw Error(`StandardTypeBuilder: Invalid Record Key`);
2164
+ throw Error(`StandardTypeBuilder: Record key is an invalid type`);
2143
2165
  }
2144
2166
  }
2145
2167
  /** `[Standard]` Creates a Recursive type */