@sinclair/typebox 0.26.8 → 0.27.0

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/typebox.d.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  export declare const Modifier: unique symbol;
2
2
  export declare const Hint: unique symbol;
3
3
  export declare const Kind: unique symbol;
4
+ export declare const PatternBoolean = "(true|false)";
5
+ export declare const PatternNumber = "(0|[1-9][0-9]*)";
6
+ export declare const PatternString = ".*";
7
+ export declare const PatternBooleanExact: string;
8
+ export declare const PatternNumberExact: string;
9
+ export declare const PatternStringExact: string;
4
10
  export type TupleToIntersect<T extends any[]> = T extends [infer I] ? I : T extends [infer I, ...infer R] ? I & TupleToIntersect<R> : never;
5
11
  export type TupleToUnion<T extends any[]> = {
6
12
  [K in keyof T]: T[K];
@@ -46,7 +52,7 @@ export interface TSchema extends SchemaOptions, TKind {
46
52
  params: unknown[];
47
53
  static: unknown;
48
54
  }
49
- export type TAnySchema = TSchema | TAny | TArray | TBigInt | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TIntersect | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TSelf | TString | TSymbol | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
55
+ export type TAnySchema = TSchema | TAny | TArray | TBigInt | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TIntersect | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TString | TSymbol | TTemplateLiteral | TThis | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
50
56
  export type TNumeric = TInteger | TNumber;
51
57
  export interface NumericOptions<N extends number | bigint> extends SchemaOptions {
52
58
  exclusiveMaximum?: N;
@@ -122,21 +128,28 @@ export interface TEnumOption<T> {
122
128
  type: 'number' | 'string';
123
129
  const: T;
124
130
  }
125
- export type TEnumStatic<T extends Record<string, string | number>> = T[keyof T];
126
131
  export interface TEnum<T extends Record<string, string | number> = Record<string, string | number>> extends TSchema {
127
132
  [Kind]: 'Union';
128
- static: TEnumStatic<T>;
133
+ static: T[keyof T];
129
134
  anyOf: TLiteral<string | number>[];
130
135
  }
131
136
  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<[Assert<X, TSchema>, Assert<Y, TSchema>]> : Assert<O, TSchema> : never;
137
+ export type TExcludeTemplateLiteralResult<T extends string> = TUnionResult<Assert<UnionToTuple<{
138
+ [K in T]: TLiteral<K>;
139
+ }[T]>, TSchema[]>>;
140
+ export type TExcludeTemplateLiteral<T extends TTemplateLiteral, U extends TSchema> = Exclude<Static<T>, Static<U>> extends infer S ? TExcludeTemplateLiteralResult<Assert<S, string>> : never;
132
141
  export type TExcludeArray<T extends TSchema[], U extends TSchema> = Assert<UnionToTuple<{
133
142
  [K in keyof T]: Static<Assert<T[K], TSchema>> extends Static<U> ? never : T[K];
134
143
  }[number]>, TSchema[]> extends infer R ? TUnionResult<Assert<R, TSchema[]>> : never;
135
- export type TExclude<T extends TSchema, U extends TSchema> = T extends TUnion<infer S> ? TExcludeArray<S, U> : T extends U ? TNever : T;
144
+ export type TExclude<T extends TSchema, U extends TSchema> = T extends TTemplateLiteral ? TExcludeTemplateLiteral<T, U> : T extends TUnion<infer S> ? TExcludeArray<S, U> : T extends U ? TNever : T;
145
+ export type TExtractTemplateLiteralResult<T extends string> = TUnionResult<Assert<UnionToTuple<{
146
+ [K in T]: TLiteral<K>;
147
+ }[T]>, TSchema[]>>;
148
+ export type TExtractTemplateLiteral<T extends TTemplateLiteral, U extends TSchema> = Extract<Static<T>, Static<U>> extends infer S ? TExtractTemplateLiteralResult<Assert<S, string>> : never;
136
149
  export type TExtractArray<T extends TSchema[], U extends TSchema> = Assert<UnionToTuple<{
137
150
  [K in keyof T]: Static<Assert<T[K], TSchema>> extends Static<U> ? T[K] : never;
138
151
  }[number]>, TSchema[]> extends infer R ? TUnionResult<Assert<R, TSchema[]>> : never;
139
- export type TExtract<T extends TSchema, U extends TSchema> = T extends TUnion<infer S> ? TExtractArray<S, U> : T extends U ? T : TNever;
152
+ export type TExtract<T extends TSchema, U extends TSchema> = T extends TTemplateLiteral ? TExtractTemplateLiteral<T, U> : T extends TUnion<infer S> ? TExtractArray<S, U> : T extends U ? T : T;
140
153
  export type TFunctionParameters<T extends readonly TSchema[], P extends unknown[]> = [...{
141
154
  [K in keyof T]: Static<Assert<T[K], TSchema>, P>;
142
155
  }];
@@ -157,13 +170,12 @@ export type TUnevaluatedProperties = undefined | TSchema | boolean;
157
170
  export interface IntersectOptions extends SchemaOptions {
158
171
  unevaluatedProperties?: TUnevaluatedProperties;
159
172
  }
160
- export type TIntersectStatic<T extends TSchema[], P extends unknown[]> = TupleToIntersect<{
161
- [K in keyof T]: Static<Assert<T[K], TSchema>, P>;
162
- }>;
163
173
  export interface TIntersect<T extends TSchema[] = TSchema[]> extends TSchema, IntersectOptions {
164
174
  [Kind]: 'Intersect';
175
+ static: TupleToIntersect<{
176
+ [K in keyof T]: Static<Assert<T[K], TSchema>, this['params']>;
177
+ }>;
165
178
  type?: 'object';
166
- static: TIntersectStatic<T, this['params']>;
167
179
  allOf: [...T];
168
180
  }
169
181
  export type TKeyOfTuple<T extends TSchema> = {
@@ -171,7 +183,7 @@ export type TKeyOfTuple<T extends TSchema> = {
171
183
  } extends infer U ? UnionToTuple<Exclude<{
172
184
  [K in keyof U]: U[K];
173
185
  }[keyof U], undefined>> : never;
174
- export type TKeyOf<T extends TSchema = TSchema> = (T extends TComposite ? TKeyOfTuple<T> : T extends TIntersect ? TKeyOfTuple<T> : T extends TUnion ? TKeyOfTuple<T> : T extends TObject ? TKeyOfTuple<T> : [
186
+ export type TKeyOf<T extends TSchema = TSchema> = (T extends TComposite ? TKeyOfTuple<T> : T extends TIntersect ? TKeyOfTuple<T> : T extends TUnion ? TKeyOfTuple<T> : T extends TObject ? TKeyOfTuple<T> : T extends TRecord<infer K> ? [K] : [
175
187
  ]) extends infer R ? TUnionResult<Assert<R, TSchema[]>> : never;
176
188
  export type TLiteralValue = string | number | boolean;
177
189
  export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
@@ -184,10 +196,9 @@ export interface TNever extends TSchema {
184
196
  static: never;
185
197
  not: {};
186
198
  }
187
- export type TNotStatic<_ extends TSchema = TSchema, T extends TSchema = TSchema> = Static<T>;
188
199
  export interface TNot<Not extends TSchema = TSchema, T extends TSchema = TSchema> extends TSchema {
189
200
  [Kind]: 'Not';
190
- static: TNotStatic<Not, T>;
201
+ static: Static<T>;
191
202
  allOf: [{
192
203
  not: Not;
193
204
  }, T];
@@ -256,33 +267,37 @@ export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T
256
267
  [K in keyof R]: Assert<R[K], TSchema> extends TSchema ? R[K] : never;
257
268
  }) : never;
258
269
  export type TPick<T extends TSchema, K extends keyof any> = T extends TComposite<infer S> ? TComposite<TPickArray<S, K>> : T extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : T extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T;
259
- export type TPromiseStatic<T extends TSchema, P extends unknown[]> = Promise<Static<T, P>>;
260
270
  export interface TPromise<T extends TSchema = TSchema> extends TSchema {
261
271
  [Kind]: 'Promise';
262
- static: TPromiseStatic<T, this['params']>;
272
+ static: Promise<Static<T, this['params']>>;
263
273
  type: 'object';
264
274
  instanceOf: 'Promise';
265
275
  item: TSchema;
266
276
  }
267
- export type TRecordKey = TString | TNumeric | TUnion<TLiteral<any>[]>;
268
- export type TRecordPropertiesFromUnionLiteral<K extends TUnion<TLiteral<string | number>[]>, T extends TSchema> = Static<K> extends string ? {
277
+ export type RecordTemplateLiteralObjectType<K extends TTemplateLiteral, T extends TSchema> = Ensure<TObject<Evaluate<{
278
+ [_ in Static<K>]: T;
279
+ }>>>;
280
+ export type RecordTemplateLiteralType<K extends TTemplateLiteral, T extends TSchema> = IsTemplateLiteralFinite<K> extends true ? RecordTemplateLiteralObjectType<K, T> : TRecord<TString, T>;
281
+ export type RecordUnionLiteralType<K extends TUnion<TLiteral<string | number>[]>, T extends TSchema> = Static<K> extends string ? Ensure<TObject<{
269
282
  [X in Static<K>]: T;
270
- } : never;
271
- export type TRecordPropertiesFromLiteral<K extends TLiteral<string | number>, T extends TSchema> = Evaluate<{
283
+ }>> : never;
284
+ export type RecordLiteralType<K extends TLiteral<string | number>, T extends TSchema> = Ensure<TObject<{
272
285
  [K2 in K['const']]: T;
273
- }>;
274
- export type TRecordStatic<K extends TRecordKey, T extends TSchema, P extends unknown[]> = Record<Static<K>, Static<T, P>>;
275
- export interface TRecord<K extends TRecordKey = TRecordKey, T extends TSchema = TSchema> extends TSchema {
286
+ }>>;
287
+ export type RecordNumberType<K extends TInteger | TNumber, T extends TSchema> = Ensure<TRecord<K, T>>;
288
+ export type RecordStringType<K extends TString, T extends TSchema> = Ensure<TRecord<K, T>>;
289
+ export type RecordKey = TUnion<TLiteral<string | number>[]> | TLiteral<string | number> | TTemplateLiteral | TInteger | TNumber | TString;
290
+ export interface TRecord<K extends RecordKey = RecordKey, T extends TSchema = TSchema> extends TSchema {
276
291
  [Kind]: 'Record';
277
- static: TRecordStatic<K, T, this['params']>;
292
+ static: Record<Static<K>, Static<T, this['params']>>;
278
293
  type: 'object';
279
294
  patternProperties: {
280
295
  [pattern: string]: T;
281
296
  };
282
297
  additionalProperties: false;
283
298
  }
284
- export interface TSelf extends TSchema {
285
- [Kind]: 'Self';
299
+ export interface TThis extends TSchema {
300
+ [Kind]: 'This';
286
301
  static: this['params'][0];
287
302
  $ref: string;
288
303
  }
@@ -290,10 +305,9 @@ export type TRecursiveReduce<T extends TSchema> = Static<T, [TRecursiveReduce<T>
290
305
  export interface TRecursive<T extends TSchema> extends TSchema {
291
306
  static: TRecursiveReduce<T>;
292
307
  }
293
- export type TRefStatic<T extends TSchema, P extends unknown[]> = Static<T, P>;
294
308
  export interface TRef<T extends TSchema = TSchema> extends TSchema {
295
309
  [Kind]: 'Ref';
296
- static: TRefStatic<T, this['params']>;
310
+ static: Static<T, this['params']>;
297
311
  $ref: string;
298
312
  }
299
313
  export type TReturnType<T extends TFunction> = T['returns'];
@@ -325,13 +339,26 @@ export interface TSymbol extends TSchema, SchemaOptions {
325
339
  type: 'null';
326
340
  typeOf: 'Symbol';
327
341
  }
342
+ export type IsTemplateLiteralFiniteCheck<T> = T extends TTemplateLiteral<infer U> ? IsTemplateLiteralFiniteArray<Assert<U, TTemplateLiteralKind[]>> : T extends TUnion<infer U> ? IsTemplateLiteralFiniteArray<Assert<U, TTemplateLiteralKind[]>> : T extends TString ? false : T extends TBoolean ? false : T extends TNumber ? false : T extends TInteger ? false : T extends TBigInt ? false : T extends TLiteral ? true : false;
343
+ export type IsTemplateLiteralFiniteArray<T extends TTemplateLiteralKind[]> = T extends [infer L, ...infer R] ? IsTemplateLiteralFiniteCheck<L> extends false ? false : IsTemplateLiteralFiniteArray<Assert<R, TTemplateLiteralKind[]>> : T extends [infer L] ? IsTemplateLiteralFiniteCheck<L> extends false ? false : true : true;
344
+ export type IsTemplateLiteralFinite<T> = T extends TTemplateLiteral<infer U> ? IsTemplateLiteralFiniteArray<U> : false;
345
+ export type TTemplateLiteralKind = TUnion | TLiteral | TInteger | TTemplateLiteral | TNumber | TBigInt | TString | TBoolean | TNever;
346
+ export type TTemplateLiteralConst<T, Acc extends string> = T extends TUnion<infer U> ? {
347
+ [K in keyof U]: TTemplateLiteralUnion<Assert<[U[K]], TTemplateLiteralKind[]>, Acc>;
348
+ }[number] : T extends TTemplateLiteral ? `${Static<T>}` : T extends TLiteral<infer U> ? `${U}` : T extends TString ? `${string}` : T extends TNumber ? `${number}` : T extends TBigInt ? `${bigint}` : T extends TBoolean ? `${boolean}` : never;
349
+ export type TTemplateLiteralUnion<T extends TTemplateLiteralKind[], Acc extends string = ''> = T extends [infer L, ...infer R] ? `${TTemplateLiteralConst<L, Acc>}${TTemplateLiteralUnion<Assert<R, TTemplateLiteralKind[]>, Acc>}` : T extends [infer L] ? `${TTemplateLiteralConst<L, Acc>}${Acc}` : Acc;
350
+ export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLiteralKind[]> extends TSchema {
351
+ [Kind]: 'TemplateLiteral';
352
+ static: TTemplateLiteralUnion<T>;
353
+ type: 'string';
354
+ pattern: string;
355
+ }
328
356
  export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? Assert<R, TSchema[]> : never;
329
- export type TTupleStatic<T extends TSchema[], P extends unknown[]> = {
330
- [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : T[K];
331
- };
332
357
  export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema {
333
358
  [Kind]: 'Tuple';
334
- static: TTupleStatic<T, this['params']>;
359
+ static: {
360
+ [K in keyof T]: T[K] extends TSchema ? Static<T[K], this['params']> : T[K];
361
+ };
335
362
  type: 'array';
336
363
  items?: T;
337
364
  additionalItems?: false;
@@ -349,6 +376,9 @@ export type TUnionOfLiteralArray<T extends TLiteral<string>[]> = {
349
376
  }[number];
350
377
  export type TUnionOfLiteral<T extends TUnion<TLiteral<string>[]>> = TUnionOfLiteralArray<T['anyOf']>;
351
378
  export type TUnionResult<T extends TSchema[]> = T extends [] ? TNever : T extends [infer S] ? S : TUnion<T>;
379
+ export type TUnionTemplateLiteral<T extends TTemplateLiteral, S extends string = Static<T>> = Ensure<TUnionResult<Assert<UnionToTuple<{
380
+ [K in S]: TLiteral<K>;
381
+ }[S]>, TLiteral[]>>>;
352
382
  export interface TUnion<T extends TSchema[] = TSchema[]> extends TSchema {
353
383
  [Kind]: 'Union';
354
384
  static: {
@@ -425,7 +455,7 @@ export declare namespace TypeGuard {
425
455
  function TAny(schema: unknown): schema is TAny;
426
456
  /** Returns true if the given schema is TArray */
427
457
  function TArray(schema: unknown): schema is TArray;
428
- /** Returns true if the given schema is TSymbol */
458
+ /** Returns true if the given schema is TBigInt */
429
459
  function TBigInt(schema: unknown): schema is TBigInt;
430
460
  /** Returns true if the given schema is TBoolean */
431
461
  function TBoolean(schema: unknown): schema is TBoolean;
@@ -457,14 +487,16 @@ export declare namespace TypeGuard {
457
487
  function TPromise(schema: unknown): schema is TPromise;
458
488
  /** Returns true if the given schema is TRecord */
459
489
  function TRecord(schema: unknown): schema is TRecord;
460
- /** Returns true if the given schema is TSelf */
461
- function TSelf(schema: unknown): schema is TSelf;
462
490
  /** Returns true if the given schema is TRef */
463
491
  function TRef(schema: unknown): schema is TRef;
464
492
  /** Returns true if the given schema is TString */
465
493
  function TString(schema: unknown): schema is TString;
466
494
  /** Returns true if the given schema is TSymbol */
467
495
  function TSymbol(schema: unknown): schema is TSymbol;
496
+ /** Returns true if the given schema is TTemplateLiteral */
497
+ function TTemplateLiteral(schema: unknown): schema is TTemplateLiteral;
498
+ /** Returns true if the given schema is TThis */
499
+ function TThis(schema: unknown): schema is TThis;
468
500
  /** Returns true if the given schema is TTuple */
469
501
  function TTuple(schema: unknown): schema is TTuple;
470
502
  /** Returns true if the given schema is TUndefined */
@@ -513,6 +545,40 @@ export declare namespace ObjectMap {
513
545
  export declare namespace KeyResolver {
514
546
  function Resolve<T extends TSchema>(schema: T): string[];
515
547
  }
548
+ export declare namespace TemplateLiteralPattern {
549
+ function Create(kinds: TTemplateLiteralKind[]): string;
550
+ }
551
+ export declare namespace TemplateLiteralResolver {
552
+ function Resolve(template: TTemplateLiteral): TString | TUnion | TLiteral;
553
+ }
554
+ export declare class TemplateLiteralParserError extends Error {
555
+ constructor(message: string);
556
+ }
557
+ export declare namespace TemplateLiteralParser {
558
+ type Expression = And | Or | Const;
559
+ type Const = {
560
+ type: 'const';
561
+ const: string;
562
+ };
563
+ type And = {
564
+ type: 'and';
565
+ expr: Expression[];
566
+ };
567
+ type Or = {
568
+ type: 'or';
569
+ expr: Expression[];
570
+ };
571
+ /** Parses a pattern and returns an expression tree */
572
+ function Parse(pattern: string): Expression;
573
+ /** Parses a pattern and strips forward and trailing ^ and $ */
574
+ function ParseExact(pattern: string): Expression;
575
+ }
576
+ export declare namespace TemplateLiteralFinite {
577
+ function Check(expression: TemplateLiteralParser.Expression): boolean;
578
+ }
579
+ export declare namespace TemplateLiteralGenerator {
580
+ function Generate(expression: TemplateLiteralParser.Expression): IterableIterator<string>;
581
+ }
516
582
  export declare class TypeBuilder {
517
583
  /** `[Utility]` Creates a schema without `static` and `params` types */
518
584
  protected Create<T>(schema: Omit<T, 'static' | 'params'>): T;
@@ -581,20 +647,26 @@ export declare class StandardTypeBuilder extends TypeBuilder {
581
647
  Pick<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TPick<T, K['const']>;
582
648
  /** `[Standard]` Creates a mapped type whose keys are picked from the given type */
583
649
  Pick<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TPick<T, never>;
584
- /** `[Standard]` Creates an Object type from the given Literal Union */
585
- Record<K extends TUnion<TLiteral<string | number>[]>, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): TObject<TRecordPropertiesFromUnionLiteral<K, T>>;
586
- /** `[Standard]` Creates an Object type from the given Literal Union */
587
- Record<K extends TLiteral<string | number>, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): TObject<TRecordPropertiesFromLiteral<K, T>>;
588
650
  /** `[Standard]` Creates a Record type */
589
- Record<K extends TString | TNumeric, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): TRecord<K, T>;
651
+ Record<K extends TUnion<TLiteral<string | number>[]>, T extends TSchema>(key: K, schema: T): RecordUnionLiteralType<K, T>;
652
+ /** `[Standard]` Creates a Record type */
653
+ Record<K extends TLiteral<string | number>, T extends TSchema>(key: K, schema: T): RecordLiteralType<K, T>;
654
+ /** `[Standard]` Creates a Record type */
655
+ Record<K extends TTemplateLiteral, T extends TSchema>(key: K, schema: T): RecordTemplateLiteralType<K, T>;
656
+ /** `[Standard]` Creates a Record type */
657
+ Record<K extends TInteger | TNumber, T extends TSchema>(key: K, schema: T): RecordNumberType<K, T>;
658
+ /** `[Standard]` Creates a Record type */
659
+ Record<K extends TString, T extends TSchema>(key: K, schema: T): RecordStringType<K, T>;
590
660
  /** `[Standard]` Creates a Recursive type */
591
- Recursive<T extends TSchema>(callback: (self: TSelf) => T, options?: SchemaOptions): TRecursive<T>;
661
+ Recursive<T extends TSchema>(callback: (thisType: TThis) => T, options?: SchemaOptions): TRecursive<T>;
592
662
  /** `[Standard]` Creates a Ref type. The referenced type must contain a $id */
593
663
  Ref<T extends TSchema>(schema: T, options?: SchemaOptions): TRef<T>;
594
664
  /** `[Standard]` Creates a mapped type where all properties are Required */
595
665
  Required<T extends TSchema>(schema: T, options?: SchemaOptions): TRequired<T>;
596
666
  /** `[Standard]` Creates a String type */
597
667
  String<Format extends string>(options?: StringOptions<StringFormatOption | Format>): TString<Format>;
668
+ /** `[Standard]` Creates a template literal type */
669
+ TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T], options?: SchemaOptions): TTemplateLiteral<T>;
598
670
  /** `[Standard]` Creates a Tuple type */
599
671
  Tuple<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TTuple<T>;
600
672
  /** `[Standard]` Creates a Union type */
@@ -603,6 +675,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
603
675
  Union<T extends [TSchema]>(anyOf: [...T], options?: SchemaOptions): T[0];
604
676
  /** `[Standard]` Creates a Union type */
605
677
  Union<T extends TSchema[]>(anyOf: [...T], options?: SchemaOptions): TUnion<T>;
678
+ /** `[Experimental]` Remaps a TemplateLiteral into a Union representation. This function is known to cause TS compiler crashes for finite templates with large generation counts. Use with caution. */
679
+ Union<T extends TTemplateLiteral>(template: T): TUnionTemplateLiteral<T>;
606
680
  /** `[Standard]` Creates an Unknown type */
607
681
  Unknown(options?: SchemaOptions): TUnknown;
608
682
  /** `[Standard]` Creates a Unsafe type that infers for the generic argument */