@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.
- package/build/cjs/type/remap/remap.d.ts +20 -9
- package/build/cjs/type/remap/remap.js +26 -14
- package/build/esm/type/remap/remap.d.mts +20 -9
- package/build/esm/type/remap/remap.mjs +26 -14
- package/package.json +1 -1
- package/readme.md +15 -14
|
@@ -1,19 +1,30 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
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
|
|
18
|
+
type TApply<Type extends TSchema, Mapping extends TMapping, Mapped = (Mapping & {
|
|
8
19
|
input: Type;
|
|
9
|
-
})['output'], Result = Mapped extends
|
|
10
|
-
type TFromProperties<Properties extends
|
|
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
|
|
14
|
-
type TFromType<Type extends
|
|
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
|
|
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:
|
|
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
|
|
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 =
|
|
33
|
+
const mapped = (0, type_1.CloneType)(FromType(type, callback));
|
|
22
34
|
// Return remapped interior
|
|
23
|
-
return (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|
2
|
-
|
|
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
|
|
18
|
+
type TApply<Type extends TSchema, Mapping extends TMapping, Mapped = (Mapping & {
|
|
8
19
|
input: Type;
|
|
9
|
-
})['output'], Result = Mapped extends
|
|
10
|
-
type TFromProperties<Properties extends
|
|
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
|
|
14
|
-
type TFromType<Type extends
|
|
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
|
|
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:
|
|
29
|
+
export declare function Remap(type: TSchema, callback: TCallback): TSchema;
|
|
19
30
|
export {};
|
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
import
|
|
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 =
|
|
29
|
+
const mapped = CloneType(FromType(type, callback));
|
|
18
30
|
// Return remapped interior
|
|
19
|
-
return (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
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
|
|
1393
|
+
Generic types can be created using Argument types.
|
|
1394
1394
|
|
|
1395
1395
|
```typescript
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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>
|