@sinclair/typebox 0.34.17 → 0.34.19

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.
@@ -1,19 +1,30 @@
1
- import * as t from '@sinclair/typebox';
2
- export type TCallback = (schema: t.TSchema) => t.TSchema;
1
+ import { TSchema } from '../schema/index';
2
+ import { TObject, TProperties } from '../object/index';
3
+ import { TConstructor } from '../constructor/index';
4
+ import { TFunction } from '../function/index';
5
+ import { TIntersect } from '../intersect/index';
6
+ import { TUnion } from '../union/index';
7
+ import { TTuple } from '../tuple/index';
8
+ import { TArray } from '../array/index';
9
+ import { TAsyncIterator } from '../async-iterator/index';
10
+ import { TIterator } from '../iterator/index';
11
+ import { TPromise } from '../promise/index';
12
+ import { TRecord, TRecordOrObject } from '../record/index';
13
+ export type TCallback = (schema: TSchema) => TSchema;
3
14
  export interface TMapping {
4
15
  input: unknown;
5
16
  output: unknown;
6
17
  }
7
- type TApply<Type extends t.TSchema, Mapping extends TMapping, Mapped = (Mapping & {
18
+ type TApply<Type extends TSchema, Mapping extends TMapping, Mapped = (Mapping & {
8
19
  input: Type;
9
- })['output'], Result = Mapped extends t.TSchema ? Mapped : never> = Result;
10
- type TFromProperties<Properties extends t.TProperties, Mapping extends TMapping, Result extends t.TProperties = {
20
+ })['output'], Result = Mapped extends TSchema ? Mapped : never> = Result;
21
+ type TFromProperties<Properties extends TProperties, Mapping extends TMapping, Result extends TProperties = {
11
22
  [Key in keyof Properties]: TRemap<Properties[Key], Mapping>;
12
23
  }> = Result;
13
- type TFromTypes<Types extends t.TSchema[], Mapping extends TMapping, Result extends t.TSchema[] = []> = (Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]] ? TFromTypes<Right, Mapping, [...Result, TRemap<Left, Mapping>]> : Result);
14
- type TFromType<Type extends t.TSchema, Mapping extends TMapping, Result extends t.TSchema = (TApply<Type, Mapping>)> = Result;
24
+ type TFromTypes<Types extends TSchema[], Mapping extends TMapping, Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromTypes<Right, Mapping, [...Result, TRemap<Left, Mapping>]> : Result);
25
+ type TFromType<Type extends TSchema, Mapping extends TMapping, Result extends TSchema = (TApply<Type, Mapping>)> = Result;
15
26
  /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
16
- export type TRemap<Type extends t.TSchema, Mapping extends TMapping, Mapped extends t.TSchema = TFromType<Type, Mapping>, Result extends t.TSchema = (Mapped extends t.TConstructor<infer Parameters extends t.TSchema[], infer ReturnType extends t.TSchema> ? t.TConstructor<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends t.TFunction<infer Parameters extends t.TSchema[], infer ReturnType extends t.TSchema> ? t.TFunction<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends t.TIntersect<infer Types extends t.TSchema[]> ? t.TIntersect<TFromTypes<Types, Mapping>> : Mapped extends t.TUnion<infer Types extends t.TSchema[]> ? t.TUnion<TFromTypes<Types, Mapping>> : Mapped extends t.TTuple<infer Types extends t.TSchema[]> ? t.TTuple<TFromTypes<Types, Mapping>> : Mapped extends t.TArray<infer Type extends t.TSchema> ? t.TArray<TFromType<Type, Mapping>> : Mapped extends t.TAsyncIterator<infer Type extends t.TSchema> ? t.TAsyncIterator<TFromType<Type, Mapping>> : Mapped extends t.TIterator<infer Type extends t.TSchema> ? t.TIterator<TFromType<Type, Mapping>> : Mapped extends t.TPromise<infer Type extends t.TSchema> ? t.TPromise<TFromType<Type, Mapping>> : Mapped extends t.TObject<infer Properties extends t.TProperties> ? t.TObject<TFromProperties<Properties, Mapping>> : Mapped extends t.TRecord<infer Key extends t.TSchema, infer Value extends t.TSchema> ? t.TRecordOrObject<TFromType<Key, Mapping>, TFromType<Value, Mapping>> : Mapped)> = Result;
27
+ export type TRemap<Type extends TSchema, Mapping extends TMapping, Mapped extends TSchema = TFromType<Type, Mapping>, Result extends TSchema = (Mapped extends TConstructor<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TConstructor<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromTypes<Types, Mapping>> : Mapped extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromTypes<Types, Mapping>> : Mapped extends TTuple<infer Types extends TSchema[]> ? TTuple<TFromTypes<Types, Mapping>> : Mapped extends TArray<infer Type extends TSchema> ? TArray<TFromType<Type, Mapping>> : Mapped extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TFromType<Type, Mapping>> : Mapped extends TIterator<infer Type extends TSchema> ? TIterator<TFromType<Type, Mapping>> : Mapped extends TPromise<infer Type extends TSchema> ? TPromise<TFromType<Type, Mapping>> : Mapped extends TObject<infer Properties extends TProperties> ? TObject<TFromProperties<Properties, Mapping>> : Mapped extends TRecord<infer Key extends TSchema, infer Value extends TSchema> ? TRecordOrObject<TFromType<Key, Mapping>, TFromType<Value, Mapping>> : Mapped)> = Result;
17
28
  /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
18
- export declare function Remap(type: t.TSchema, callback: TCallback): t.TSchema;
29
+ export declare function Remap(type: TSchema, callback: TCallback): TSchema;
19
30
  export {};
@@ -2,7 +2,19 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.Remap = Remap;
5
- const t = require("@sinclair/typebox");
5
+ const index_1 = require("../object/index");
6
+ const index_2 = require("../constructor/index");
7
+ const index_3 = require("../function/index");
8
+ const index_4 = require("../intersect/index");
9
+ const index_5 = require("../union/index");
10
+ const index_6 = require("../tuple/index");
11
+ const index_7 = require("../array/index");
12
+ const index_8 = require("../async-iterator/index");
13
+ const index_9 = require("../iterator/index");
14
+ const index_10 = require("../promise/index");
15
+ const index_11 = require("../record/index");
16
+ const KindGuard = require("../guard/kind");
17
+ const type_1 = require("../clone/type");
6
18
  function FromProperties(properties, func) {
7
19
  return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
8
20
  return { ...result, [key]: Remap(properties[key], func) };
@@ -18,18 +30,18 @@ function FromType(type, callback) {
18
30
  // prettier-ignore
19
31
  function Remap(type, callback) {
20
32
  // Map incoming type
21
- const mapped = t.CloneType(FromType(type, callback));
33
+ const mapped = (0, type_1.CloneType)(FromType(type, callback));
22
34
  // Return remapped interior
23
- return (t.KindGuard.IsConstructor(type) ? t.Constructor(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
24
- t.KindGuard.IsFunction(type) ? t.Function(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
25
- t.KindGuard.IsIntersect(type) ? t.Intersect(FromTypes(type.allOf, callback), mapped) :
26
- t.KindGuard.IsUnion(type) ? t.Union(FromTypes(type.anyOf, callback), mapped) :
27
- t.KindGuard.IsTuple(type) ? t.Tuple(FromTypes(type.items || [], callback), mapped) :
28
- t.KindGuard.IsArray(type) ? t.Array(FromType(type.items, callback), mapped) :
29
- t.KindGuard.IsAsyncIterator(type) ? t.AsyncIterator(FromType(type.items, callback), mapped) :
30
- t.KindGuard.IsIterator(type) ? t.Iterator(FromType(type.items, callback), mapped) :
31
- t.KindGuard.IsPromise(type) ? t.Promise(FromType(type.items, callback), mapped) :
32
- t.KindGuard.IsObject(type) ? t.Object(FromProperties(type.properties, callback), mapped) :
33
- t.KindGuard.IsRecord(type) ? t.Record(FromType(t.RecordKey(type), callback), FromType(t.RecordValue(type), callback), mapped) :
34
- t.CloneType(mapped));
35
+ return (KindGuard.IsConstructor(type) ? (0, index_2.Constructor)(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
36
+ KindGuard.IsFunction(type) ? (0, index_3.Function)(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
37
+ KindGuard.IsIntersect(type) ? (0, index_4.Intersect)(FromTypes(type.allOf, callback), mapped) :
38
+ KindGuard.IsUnion(type) ? (0, index_5.Union)(FromTypes(type.anyOf, callback), mapped) :
39
+ KindGuard.IsTuple(type) ? (0, index_6.Tuple)(FromTypes(type.items || [], callback), mapped) :
40
+ KindGuard.IsArray(type) ? (0, index_7.Array)(FromType(type.items, callback), mapped) :
41
+ KindGuard.IsAsyncIterator(type) ? (0, index_8.AsyncIterator)(FromType(type.items, callback), mapped) :
42
+ KindGuard.IsIterator(type) ? (0, index_9.Iterator)(FromType(type.items, callback), mapped) :
43
+ KindGuard.IsPromise(type) ? (0, index_10.Promise)(FromType(type.items, callback), mapped) :
44
+ KindGuard.IsObject(type) ? (0, index_1.Object)(FromProperties(type.properties, callback), mapped) :
45
+ KindGuard.IsRecord(type) ? (0, index_11.Record)(FromType((0, index_11.RecordKey)(type), callback), FromType((0, index_11.RecordValue)(type), callback), mapped) :
46
+ (0, type_1.CloneType)(mapped));
35
47
  }
@@ -1,19 +1,30 @@
1
- import * as t from '@sinclair/typebox.mjs';
2
- export type TCallback = (schema: t.TSchema) => t.TSchema;
1
+ import { TSchema } from '../schema/index.mjs';
2
+ import { TObject, TProperties } from '../object/index.mjs';
3
+ import { TConstructor } from '../constructor/index.mjs';
4
+ import { TFunction } from '../function/index.mjs';
5
+ import { TIntersect } from '../intersect/index.mjs';
6
+ import { TUnion } from '../union/index.mjs';
7
+ import { TTuple } from '../tuple/index.mjs';
8
+ import { TArray } from '../array/index.mjs';
9
+ import { TAsyncIterator } from '../async-iterator/index.mjs';
10
+ import { TIterator } from '../iterator/index.mjs';
11
+ import { TPromise } from '../promise/index.mjs';
12
+ import { TRecord, TRecordOrObject } from '../record/index.mjs';
13
+ export type TCallback = (schema: TSchema) => TSchema;
3
14
  export interface TMapping {
4
15
  input: unknown;
5
16
  output: unknown;
6
17
  }
7
- type TApply<Type extends t.TSchema, Mapping extends TMapping, Mapped = (Mapping & {
18
+ type TApply<Type extends TSchema, Mapping extends TMapping, Mapped = (Mapping & {
8
19
  input: Type;
9
- })['output'], Result = Mapped extends t.TSchema ? Mapped : never> = Result;
10
- type TFromProperties<Properties extends t.TProperties, Mapping extends TMapping, Result extends t.TProperties = {
20
+ })['output'], Result = Mapped extends TSchema ? Mapped : never> = Result;
21
+ type TFromProperties<Properties extends TProperties, Mapping extends TMapping, Result extends TProperties = {
11
22
  [Key in keyof Properties]: TRemap<Properties[Key], Mapping>;
12
23
  }> = Result;
13
- type TFromTypes<Types extends t.TSchema[], Mapping extends TMapping, Result extends t.TSchema[] = []> = (Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]] ? TFromTypes<Right, Mapping, [...Result, TRemap<Left, Mapping>]> : Result);
14
- type TFromType<Type extends t.TSchema, Mapping extends TMapping, Result extends t.TSchema = (TApply<Type, Mapping>)> = Result;
24
+ type TFromTypes<Types extends TSchema[], Mapping extends TMapping, Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromTypes<Right, Mapping, [...Result, TRemap<Left, Mapping>]> : Result);
25
+ type TFromType<Type extends TSchema, Mapping extends TMapping, Result extends TSchema = (TApply<Type, Mapping>)> = Result;
15
26
  /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
16
- export type TRemap<Type extends t.TSchema, Mapping extends TMapping, Mapped extends t.TSchema = TFromType<Type, Mapping>, Result extends t.TSchema = (Mapped extends t.TConstructor<infer Parameters extends t.TSchema[], infer ReturnType extends t.TSchema> ? t.TConstructor<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends t.TFunction<infer Parameters extends t.TSchema[], infer ReturnType extends t.TSchema> ? t.TFunction<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends t.TIntersect<infer Types extends t.TSchema[]> ? t.TIntersect<TFromTypes<Types, Mapping>> : Mapped extends t.TUnion<infer Types extends t.TSchema[]> ? t.TUnion<TFromTypes<Types, Mapping>> : Mapped extends t.TTuple<infer Types extends t.TSchema[]> ? t.TTuple<TFromTypes<Types, Mapping>> : Mapped extends t.TArray<infer Type extends t.TSchema> ? t.TArray<TFromType<Type, Mapping>> : Mapped extends t.TAsyncIterator<infer Type extends t.TSchema> ? t.TAsyncIterator<TFromType<Type, Mapping>> : Mapped extends t.TIterator<infer Type extends t.TSchema> ? t.TIterator<TFromType<Type, Mapping>> : Mapped extends t.TPromise<infer Type extends t.TSchema> ? t.TPromise<TFromType<Type, Mapping>> : Mapped extends t.TObject<infer Properties extends t.TProperties> ? t.TObject<TFromProperties<Properties, Mapping>> : Mapped extends t.TRecord<infer Key extends t.TSchema, infer Value extends t.TSchema> ? t.TRecordOrObject<TFromType<Key, Mapping>, TFromType<Value, Mapping>> : Mapped)> = Result;
27
+ export type TRemap<Type extends TSchema, Mapping extends TMapping, Mapped extends TSchema = TFromType<Type, Mapping>, Result extends TSchema = (Mapped extends TConstructor<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TConstructor<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromTypes<Types, Mapping>> : Mapped extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromTypes<Types, Mapping>> : Mapped extends TTuple<infer Types extends TSchema[]> ? TTuple<TFromTypes<Types, Mapping>> : Mapped extends TArray<infer Type extends TSchema> ? TArray<TFromType<Type, Mapping>> : Mapped extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TFromType<Type, Mapping>> : Mapped extends TIterator<infer Type extends TSchema> ? TIterator<TFromType<Type, Mapping>> : Mapped extends TPromise<infer Type extends TSchema> ? TPromise<TFromType<Type, Mapping>> : Mapped extends TObject<infer Properties extends TProperties> ? TObject<TFromProperties<Properties, Mapping>> : Mapped extends TRecord<infer Key extends TSchema, infer Value extends TSchema> ? TRecordOrObject<TFromType<Key, Mapping>, TFromType<Value, Mapping>> : Mapped)> = Result;
17
28
  /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
18
- export declare function Remap(type: t.TSchema, callback: TCallback): t.TSchema;
29
+ export declare function Remap(type: TSchema, callback: TCallback): TSchema;
19
30
  export {};
@@ -1,4 +1,16 @@
1
- import * as t from '@sinclair/typebox.mjs';
1
+ import { Object } from '../object/index.mjs';
2
+ import { Constructor } from '../constructor/index.mjs';
3
+ import { Function } from '../function/index.mjs';
4
+ import { Intersect } from '../intersect/index.mjs';
5
+ import { Union } from '../union/index.mjs';
6
+ import { Tuple } from '../tuple/index.mjs';
7
+ import { Array } from '../array/index.mjs';
8
+ import { AsyncIterator } from '../async-iterator/index.mjs';
9
+ import { Iterator } from '../iterator/index.mjs';
10
+ import { Promise } from '../promise/index.mjs';
11
+ import { Record, RecordKey, RecordValue } from '../record/index.mjs';
12
+ import * as KindGuard from '../guard/kind.mjs';
13
+ import { CloneType } from '../clone/type.mjs';
2
14
  function FromProperties(properties, func) {
3
15
  return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
4
16
  return { ...result, [key]: Remap(properties[key], func) };
@@ -14,18 +26,18 @@ function FromType(type, callback) {
14
26
  // prettier-ignore
15
27
  export function Remap(type, callback) {
16
28
  // Map incoming type
17
- const mapped = t.CloneType(FromType(type, callback));
29
+ const mapped = CloneType(FromType(type, callback));
18
30
  // Return remapped interior
19
- return (t.KindGuard.IsConstructor(type) ? t.Constructor(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
20
- t.KindGuard.IsFunction(type) ? t.Function(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
21
- t.KindGuard.IsIntersect(type) ? t.Intersect(FromTypes(type.allOf, callback), mapped) :
22
- t.KindGuard.IsUnion(type) ? t.Union(FromTypes(type.anyOf, callback), mapped) :
23
- t.KindGuard.IsTuple(type) ? t.Tuple(FromTypes(type.items || [], callback), mapped) :
24
- t.KindGuard.IsArray(type) ? t.Array(FromType(type.items, callback), mapped) :
25
- t.KindGuard.IsAsyncIterator(type) ? t.AsyncIterator(FromType(type.items, callback), mapped) :
26
- t.KindGuard.IsIterator(type) ? t.Iterator(FromType(type.items, callback), mapped) :
27
- t.KindGuard.IsPromise(type) ? t.Promise(FromType(type.items, callback), mapped) :
28
- t.KindGuard.IsObject(type) ? t.Object(FromProperties(type.properties, callback), mapped) :
29
- t.KindGuard.IsRecord(type) ? t.Record(FromType(t.RecordKey(type), callback), FromType(t.RecordValue(type), callback), mapped) :
30
- t.CloneType(mapped));
31
+ return (KindGuard.IsConstructor(type) ? Constructor(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
32
+ KindGuard.IsFunction(type) ? Function(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
33
+ KindGuard.IsIntersect(type) ? Intersect(FromTypes(type.allOf, callback), mapped) :
34
+ KindGuard.IsUnion(type) ? Union(FromTypes(type.anyOf, callback), mapped) :
35
+ KindGuard.IsTuple(type) ? Tuple(FromTypes(type.items || [], callback), mapped) :
36
+ KindGuard.IsArray(type) ? Array(FromType(type.items, callback), mapped) :
37
+ KindGuard.IsAsyncIterator(type) ? AsyncIterator(FromType(type.items, callback), mapped) :
38
+ KindGuard.IsIterator(type) ? Iterator(FromType(type.items, callback), mapped) :
39
+ KindGuard.IsPromise(type) ? Promise(FromType(type.items, callback), mapped) :
40
+ KindGuard.IsObject(type) ? Object(FromProperties(type.properties, callback), mapped) :
41
+ KindGuard.IsRecord(type) ? Record(FromType(RecordKey(type), callback), FromType(RecordValue(type), callback), mapped) :
42
+ CloneType(mapped));
31
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.34.17",
3
+ "version": "0.34.19",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -1390,25 +1390,26 @@ const S = Syntax({ T }, `{ x: T, y: T, z: T }`) // const S: TObject<{
1390
1390
 
1391
1391
  ### Generics
1392
1392
 
1393
- Generic types can be created by passing Argument types as parameters.
1393
+ Generic types can be created using Argument types.
1394
1394
 
1395
1395
  ```typescript
1396
- // Generic Vector Type
1397
-
1398
- const Vector = Syntax({ // type Vector<X, Y, Z> = {
1399
- X: Type.Argument(0), // x: X
1400
- Y: Type.Argument(1), // y: Y,
1401
- Z: Type.Argument(2) // z: Z
1402
- }, // }
1403
- `{
1404
- x: X,
1405
- y: Y,
1406
- z: Z
1396
+ const Vector = Syntax(`{
1397
+ x: Argument<0>,
1398
+ y: Argument<1>,
1399
+ z: Argument<2>
1407
1400
  }`)
1408
1401
 
1409
- // Instanced Vector Type
1402
+ const Basis = Syntax({ Vector }, `{
1403
+ x: Vector<1, 0, 0>,
1404
+ y: Vector<0, 1, 0>,
1405
+ z: Vector<0, 0, 1>,
1406
+ }`)
1410
1407
 
1411
- const Up = Syntax({ Vector }, `Vector<0, 1, 0>`) // type Up = Vector<0, 1, 0>
1408
+ type Basis = Static<typeof Basis> // type Basis = {
1409
+ // x: { x: 1, y: 0, z: 0 },
1410
+ // y: { x: 0, y: 1, z: 0 },
1411
+ // z: { x: 0, y: 0, z: 1 }
1412
+ // }
1412
1413
  ```
1413
1414
 
1414
1415
  <a name='typeregistry'></a>