@sinclair/typebox 0.28.4 → 0.28.5
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/readme.md +27 -14
- package/typebox.d.ts +21 -24
- package/typebox.js +7 -7
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -83,7 +83,7 @@ License MIT
|
|
|
83
83
|
- [Conditional](#types-conditional)
|
|
84
84
|
- [Template](#types-template-literal)
|
|
85
85
|
- [Indexed](#types-indexed)
|
|
86
|
-
- [
|
|
86
|
+
- [Rest](#types-rest)
|
|
87
87
|
- [Guards](#types-guards)
|
|
88
88
|
- [Unsafe](#types-unsafe)
|
|
89
89
|
- [Strict](#types-strict)
|
|
@@ -872,30 +872,43 @@ const A = Type.Index(T, ['x']) // type A = T['x']
|
|
|
872
872
|
|
|
873
873
|
const B = Type.Index(T, Type.KeyOf(T)) // type B = T[keyof T]
|
|
874
874
|
```
|
|
875
|
-
<a name='types-
|
|
875
|
+
<a name='types-rest'></a>
|
|
876
876
|
|
|
877
|
-
###
|
|
877
|
+
### Rest Types
|
|
878
878
|
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
The following creates variadic functions using the Rest type.
|
|
879
|
+
Rest parameters are supported with `Type.Rest`. This function is used to extract interior arrays from tuples to allow them to compose with the JavaScript spread operator `...`. This type can be used for tuple concatination and variadic function composition.
|
|
882
880
|
|
|
883
881
|
```typescript
|
|
884
882
|
// TypeScript
|
|
885
883
|
|
|
886
|
-
type
|
|
884
|
+
type T = [number, number] // type T = [number, number]
|
|
887
885
|
|
|
888
|
-
type
|
|
886
|
+
type C = [...T, number] // type C = [number, number, number]
|
|
889
887
|
|
|
890
|
-
type
|
|
888
|
+
type F = (...param: C) => void // type F = (
|
|
889
|
+
// param0: number,
|
|
890
|
+
// param1: number,
|
|
891
|
+
// param2: number,
|
|
892
|
+
// ) => void
|
|
891
893
|
|
|
892
894
|
// TypeBox
|
|
893
895
|
|
|
894
|
-
const
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
896
|
+
const T = Type.Tuple([ // const T: TTuple<[
|
|
897
|
+
Type.Number(), // TNumber,
|
|
898
|
+
Type.Number() // TNumber,
|
|
899
|
+
]) // ]>
|
|
900
|
+
|
|
901
|
+
const C = Type.Tuple([ // const C: TTuple<[
|
|
902
|
+
...Type.Rest(T), // TNumber,
|
|
903
|
+
Type.Number() // TNumber,
|
|
904
|
+
]) // TNumber
|
|
905
|
+
// ]>
|
|
906
|
+
|
|
907
|
+
const F = Type.Function(Type.Rest(C), Type.Void()) // const F: TFunction<[
|
|
908
|
+
// TNumber,
|
|
909
|
+
// TNumber,
|
|
910
|
+
// TNumber
|
|
911
|
+
// ], TVoid>
|
|
899
912
|
```
|
|
900
913
|
<a name='types-unsafe'></a>
|
|
901
914
|
|
package/typebox.d.ts
CHANGED
|
@@ -181,7 +181,14 @@ export type TIndexKeys<T extends TSchema, K extends Key[]> = K extends [infer L,
|
|
|
181
181
|
...TIndexKeys<T, Assert<R, Key[]>>
|
|
182
182
|
] : [
|
|
183
183
|
];
|
|
184
|
-
export type
|
|
184
|
+
export type TIndexFromKeyTuple<T extends TSchema, K extends Key[]> = TIndexKeys<T, K> extends infer R ? T extends TRecursive<infer S> ? TIndexFromKeyTuple<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;
|
|
185
|
+
export type TIndex<T extends TSchema, K extends TSchema> = [
|
|
186
|
+
T,
|
|
187
|
+
K
|
|
188
|
+
] extends [TTuple, TNumber] ? UnionType<Assert<T['items'], TSchema[]>> : [
|
|
189
|
+
T,
|
|
190
|
+
K
|
|
191
|
+
] extends [TArray, TNumber] ? AssertType<T['items']> : K extends TTemplateLiteral ? TIndexFromKeyTuple<T, TTemplateLiteralKeyTuple<K>> : K extends TUnion<TLiteral<Key>[]> ? TIndexFromKeyTuple<T, TUnionLiteral<K>> : K extends TLiteral<Key> ? TIndexFromKeyTuple<T, [K['const']]> : TNever;
|
|
185
192
|
export interface TInteger extends TSchema, NumericOptions<number> {
|
|
186
193
|
[Kind]: 'Integer';
|
|
187
194
|
static: number;
|
|
@@ -372,7 +379,7 @@ export type TTemplateLiteralConst<T, Acc extends string> = T extends TUnion<infe
|
|
|
372
379
|
[K in keyof U]: TTemplateLiteralUnion<Assert<[U[K]], TTemplateLiteralKind[]>, Acc>;
|
|
373
380
|
}[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;
|
|
374
381
|
export type TTemplateLiteralUnion<T extends TTemplateLiteralKind[], Acc extends string = ''> = T extends [infer L, ...infer R] ? `${TTemplateLiteralConst<L, Acc>}${TTemplateLiteralUnion<Assert<R, TTemplateLiteralKind[]>, Acc>}` : Acc;
|
|
375
|
-
export type
|
|
382
|
+
export type TTemplateLiteralKeyTuple<T extends TTemplateLiteral> = Assert<UnionToTuple<Static<T>>, Key[]>;
|
|
376
383
|
export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLiteralKind[]> extends TSchema {
|
|
377
384
|
[Kind]: 'TemplateLiteral';
|
|
378
385
|
static: TTemplateLiteralUnion<T>;
|
|
@@ -380,11 +387,10 @@ export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLi
|
|
|
380
387
|
pattern: string;
|
|
381
388
|
}
|
|
382
389
|
export type TTupleIntoArray<T extends TTuple<TSchema[]>> = T extends TTuple<infer R> ? AssertRest<R> : never;
|
|
390
|
+
export type TTupleInfer<T extends TSchema[], P extends unknown[]> = T extends [infer L, ...infer R] ? [Static<AssertType<L>, P>, ...TTupleInfer<AssertRest<R>, P>] : [];
|
|
383
391
|
export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema {
|
|
384
392
|
[Kind]: 'Tuple';
|
|
385
|
-
static:
|
|
386
|
-
[K in keyof T]: T[K] extends TSchema ? Static<T[K], this['params']> : T[K];
|
|
387
|
-
};
|
|
393
|
+
static: TTupleInfer<T, this['params']>;
|
|
388
394
|
type: 'array';
|
|
389
395
|
items?: T;
|
|
390
396
|
additionalItems?: false;
|
|
@@ -399,7 +405,8 @@ export interface TUndefined extends TSchema {
|
|
|
399
405
|
}
|
|
400
406
|
export type TLiteralUnionReduce<T extends TLiteral<string | number>[]> = T extends [infer L, ...infer R] ? [Assert<L, TLiteral<string | number>>['const'], ...TLiteralUnionReduce<Assert<R, TLiteral<string | number>[]>>] : [
|
|
401
407
|
];
|
|
402
|
-
export type
|
|
408
|
+
export type TUnionLiteral<T extends TUnion<TLiteral<string | number>[]>> = T extends TUnion<infer S> ? TLiteralUnionReduce<Assert<S, TLiteral<string | number>[]>> : [
|
|
409
|
+
];
|
|
403
410
|
export type TUnionTemplateLiteral<T extends TTemplateLiteral, S extends string = Static<T>> = Ensure<UnionType<Assert<UnionToTuple<{
|
|
404
411
|
[K in S]: TLiteral<K>;
|
|
405
412
|
}[S]>, TLiteral[]>>>;
|
|
@@ -501,8 +508,6 @@ export declare namespace TypeGuard {
|
|
|
501
508
|
function TLiteralNumber(schema: unknown): schema is TLiteral<number>;
|
|
502
509
|
/** Returns true if the given schema is TLiteral<boolean> */
|
|
503
510
|
function TLiteralBoolean(schema: unknown): schema is TLiteral<boolean>;
|
|
504
|
-
/** Returns true if the given schema is TUnion<Literal<string | number>[]> */
|
|
505
|
-
function TLiteralUnion(schema: unknown): schema is TUnion<TLiteral[]>;
|
|
506
511
|
/** Returns true if the given schema is TLiteral */
|
|
507
512
|
function TLiteral(schema: unknown): schema is TLiteral;
|
|
508
513
|
/** Returns true if the given schema is TNever */
|
|
@@ -533,6 +538,8 @@ export declare namespace TypeGuard {
|
|
|
533
538
|
function TTuple(schema: unknown): schema is TTuple;
|
|
534
539
|
/** Returns true if the given schema is TUndefined */
|
|
535
540
|
function TUndefined(schema: unknown): schema is TUndefined;
|
|
541
|
+
/** Returns true if the given schema is TUnion<Literal<string | number>[]> */
|
|
542
|
+
function TUnionLiteral(schema: unknown): schema is TUnion<TLiteral[]>;
|
|
536
543
|
/** Returns true if the given schema is TUnion */
|
|
537
544
|
function TUnion(schema: unknown): schema is TUnion;
|
|
538
545
|
/** Returns true if the given schema is TUint8Array */
|
|
@@ -657,19 +664,9 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
657
664
|
/** `[Standard]` Extracts from the left type any type that is assignable to the right */
|
|
658
665
|
Extract<L extends TSchema, R extends TSchema>(left: L, right: R, options?: SchemaOptions): TExtract<L, R>;
|
|
659
666
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
660
|
-
Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions):
|
|
661
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
662
|
-
Index<T extends TSchema, K extends TUnion<TLiteral<Key>[]>>(schema: T, keys: K, options?: SchemaOptions): TIndex<T, TLiteralUnion<K>>;
|
|
663
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
664
|
-
Index<T extends TSchema, K extends TLiteral<Key>>(schema: T, key: K, options?: SchemaOptions): TIndex<T, [K['const']]>;
|
|
665
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
666
|
-
Index<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TIndex<T, TTemplateLiteralKeyArray<K>>;
|
|
667
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
668
|
-
Index<T extends TTuple, K extends TNumber>(schema: T, key: K, options?: SchemaOptions): UnionType<Assert<T['items'], TSchema[]>>;
|
|
669
|
-
/** `[Standard]` Returns indexed property types for the given keys */
|
|
670
|
-
Index<T extends TArray, K extends TNumber>(schema: T, key: K, options?: SchemaOptions): AssertType<T['items']>;
|
|
667
|
+
Index<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndexFromKeyTuple<T, Assert<K, Key[]>>;
|
|
671
668
|
/** `[Standard]` Returns indexed property types for the given keys */
|
|
672
|
-
Index<T extends TSchema, K extends
|
|
669
|
+
Index<T extends TSchema, K extends TSchema>(schema: T, key: K, options?: SchemaOptions): TIndex<T, K>;
|
|
673
670
|
/** `[Standard]` Creates an Integer type */
|
|
674
671
|
Integer(options?: NumericOptions<number>): TInteger;
|
|
675
672
|
/** `[Standard]` Creates a Intersect type */
|
|
@@ -694,11 +691,11 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
694
691
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
695
692
|
Omit<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TOmit<T, K[number]>;
|
|
696
693
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
697
|
-
Omit<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T,
|
|
694
|
+
Omit<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T, TUnionLiteral<K>[number]>;
|
|
698
695
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
699
696
|
Omit<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TOmit<T, K['const']>;
|
|
700
697
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
701
|
-
Omit<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TOmit<T,
|
|
698
|
+
Omit<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TOmit<T, TTemplateLiteralKeyTuple<K>[number]>;
|
|
702
699
|
/** `[Standard]` Creates a mapped type whose keys are omitted from the given type */
|
|
703
700
|
Omit<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TOmit<T, never>;
|
|
704
701
|
/** `[Standard]` Creates a mapped type where all properties are Optional */
|
|
@@ -706,11 +703,11 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
706
703
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
707
704
|
Pick<T extends TSchema, K extends (keyof Static<T>)[]>(schema: T, keys: readonly [...K], options?: SchemaOptions): TPick<T, K[number]>;
|
|
708
705
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
709
|
-
Pick<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T,
|
|
706
|
+
Pick<T extends TSchema, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T, TUnionLiteral<K>[number]>;
|
|
710
707
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
711
708
|
Pick<T extends TSchema, K extends TLiteral<string>>(schema: T, key: K, options?: SchemaOptions): TPick<T, K['const']>;
|
|
712
709
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
713
|
-
Pick<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TPick<T,
|
|
710
|
+
Pick<T extends TSchema, K extends TTemplateLiteral>(schema: T, key: K, options?: SchemaOptions): TPick<T, TTemplateLiteralKeyTuple<K>[number]>;
|
|
714
711
|
/** `[Standard]` Creates a mapped type whose keys are picked from the given type */
|
|
715
712
|
Pick<T extends TSchema, K extends TNever>(schema: T, key: K, options?: SchemaOptions): TPick<T, never>;
|
|
716
713
|
/** `[Standard]` Creates a Record type */
|
package/typebox.js
CHANGED
|
@@ -324,11 +324,6 @@ 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 | number>[]> */
|
|
328
|
-
function TLiteralUnion(schema) {
|
|
329
|
-
return TUnion(schema) && schema.anyOf.every((schema) => TLiteralString(schema) || TLiteralNumber(schema));
|
|
330
|
-
}
|
|
331
|
-
TypeGuard.TLiteralUnion = TLiteralUnion;
|
|
332
327
|
/** Returns true if the given schema is TLiteral */
|
|
333
328
|
function TLiteral(schema) {
|
|
334
329
|
return TLiteralString(schema) || TLiteralNumber(schema) || TLiteralBoolean(schema);
|
|
@@ -515,6 +510,11 @@ var TypeGuard;
|
|
|
515
510
|
IsOptionalString(schema.$id));
|
|
516
511
|
}
|
|
517
512
|
TypeGuard.TUndefined = TUndefined;
|
|
513
|
+
/** Returns true if the given schema is TUnion<Literal<string | number>[]> */
|
|
514
|
+
function TUnionLiteral(schema) {
|
|
515
|
+
return TUnion(schema) && schema.anyOf.every((schema) => TLiteralString(schema) || TLiteralNumber(schema));
|
|
516
|
+
}
|
|
517
|
+
TypeGuard.TUnionLiteral = TUnionLiteral;
|
|
518
518
|
/** Returns true if the given schema is TUnion */
|
|
519
519
|
function TUnion(schema) {
|
|
520
520
|
// prettier-ignore
|
|
@@ -1519,7 +1519,7 @@ var KeyArrayResolver;
|
|
|
1519
1519
|
function Resolve(schema) {
|
|
1520
1520
|
if (globalThis.Array.isArray(schema))
|
|
1521
1521
|
return schema;
|
|
1522
|
-
if (TypeGuard.
|
|
1522
|
+
if (TypeGuard.TUnionLiteral(schema))
|
|
1523
1523
|
return schema.anyOf.map((schema) => schema.const.toString());
|
|
1524
1524
|
if (TypeGuard.TLiteral(schema))
|
|
1525
1525
|
return [schema.const];
|
|
@@ -2138,7 +2138,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2138
2138
|
}
|
|
2139
2139
|
else if (TypeGuard.TUnion(key)) {
|
|
2140
2140
|
const union = UnionResolver.Resolve(key);
|
|
2141
|
-
if (TypeGuard.
|
|
2141
|
+
if (TypeGuard.TUnionLiteral(union)) {
|
|
2142
2142
|
const properties = union.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
|
|
2143
2143
|
return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
|
|
2144
2144
|
}
|