@sinclair/typebox 0.34.22 → 0.34.24

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/readme.md CHANGED
@@ -64,7 +64,6 @@ License MIT
64
64
  - [Types](#types)
65
65
  - [Json](#types-json)
66
66
  - [JavaScript](#types-javascript)
67
- - [Import](#types-import)
68
67
  - [Options](#types-options)
69
68
  - [Properties](#types-properties)
70
69
  - [Generics](#types-generics)
@@ -97,16 +96,18 @@ License MIT
97
96
  - [Mutate](#values-mutate)
98
97
  - [Pointer](#values-pointer)
99
98
  - [Syntax](#syntax)
100
- - [Type](#syntax-type)
101
- - [Options](#syntax-options)
99
+ - [Create](#syntax-create)
102
100
  - [Parameters](#syntax-parameters)
103
101
  - [Generics](#syntax-generics)
102
+ - [Options](#syntax-options)
104
103
  - [TypeRegistry](#typeregistry)
105
104
  - [Type](#typeregistry-type)
106
105
  - [Format](#typeregistry-format)
107
106
  - [TypeCheck](#typecheck)
108
107
  - [Ajv](#typecheck-ajv)
109
108
  - [TypeCompiler](#typecheck-typecompiler)
109
+ - [TypeMap](#typemap)
110
+ - [Usage](#typemap-usage)
110
111
  - [TypeSystem](#typesystem)
111
112
  - [Policies](#typesystem-policies)
112
113
  - [Error Function](#error-function)
@@ -638,22 +639,6 @@ TypeBox provides an extended type set that can be used to create schematics for
638
639
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
639
640
  ```
640
641
 
641
- <a name='types-import'></a>
642
-
643
- ### Import
644
-
645
- Import the Type namespace to bring in the full TypeBox type system. This is recommended for most users.
646
-
647
- ```typescript
648
- import { Type, type Static } from '@sinclair/typebox'
649
- ```
650
-
651
- You can also selectively import types. This enables modern bundlers to tree shake for unused types.
652
-
653
- ```typescript
654
- import { Object, Number, String, Boolean, type Static } from '@sinclair/typebox'
655
- ```
656
-
657
642
  <a name='types-options'></a>
658
643
 
659
644
  ### Options
@@ -741,22 +726,6 @@ const Nullable = <T extends TSchema>(T: T) => { // type Nullable<T> = T | nu
741
726
  const T = Nullable(Type.String()) // type T = Nullable<string>
742
727
  ```
743
728
 
744
- Generic types can also be created with Argument types
745
-
746
- ```typescript
747
- const Vector = Type.Object({ // type Vector<A_0, A_1, A_2> = {
748
- x: Type.Argument(0), // x: A_0,
749
- y: Type.Argument(1), // y: A_1,
750
- z: Type.Argument(2), // z: A_2
751
- }) // }
752
-
753
- const T = Type.Instantiate(Vector, [ // type T = Vector<
754
- Type.Boolean(), // boolean,
755
- Type.Number(), // number,
756
- Type.String() // string
757
- ]) // >
758
- ```
759
-
760
729
  <a name='types-recursive'></a>
761
730
 
762
731
  ### Recursive Types
@@ -1337,79 +1306,89 @@ ValuePointer.Set(A, '/z', 1) // A' = { x: 1, y: 1, z: 1
1337
1306
 
1338
1307
  ## Syntax Types
1339
1308
 
1340
- TypeBox provides optional support for runtime and type level parsing from TypeScript syntax.
1309
+ TypeBox includes support for parsing TypeScript annotation syntax into TypeBox schematics.
1310
+
1311
+ This feature is provided via optional import.
1341
1312
 
1342
1313
  ```typescript
1343
1314
  import { Syntax } from '@sinclair/typebox/syntax'
1344
1315
  ```
1345
1316
 
1346
- <a name='syntax-type'></a>
1317
+ <a name='syntax-create'></a>
1347
1318
 
1348
- ### Type
1319
+ ### Create
1349
1320
 
1350
- Use the Syntax function to create TypeBox type from TypeScript syntax.
1321
+ Use the Syntax function to create TypeBox types from TypeScript syntax ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgChQkWIhTYYwBgWKly1OoxZtO3foMkSGEdDXgAVOAF4Uo3AAoABkhwAuOOgCuIPjygAaOFR3Lx8-AkcASjgY2Jj2djhjUwt3cwB5PgArHgYYAB4ECTiS0rLyisrYhNi3OHMAOW9fAOKq9o7OuBqY4PqmsKg2rpHR+MT8AD4JCS5eeut5LEUGfLmeCCJ6ybHKmvWFmyLdk86euDrQlv9h07uy876rv1v7t-GCIA))
1351
1322
 
1352
1323
  ```typescript
1353
1324
  const T = Syntax(`{ x: number, y: number }`) // const T: TObject<{
1354
- // x: TNumber
1325
+ // x: TNumber,
1355
1326
  // y: TNumber
1356
1327
  // }>
1357
- ```
1358
-
1359
- <a name='syntax-options'></a>
1360
-
1361
- ### Options
1362
-
1363
- Options can be passed to types on the last parameter
1364
1328
 
1365
- ```typescript
1366
- const T = Syntax(`number`, { // const T = {
1367
- minimum: 0, // type: 'number',
1368
- maximum: 10 // minimum: 0,
1369
- }) // maximum: 10
1370
- // }
1329
+ type T = Static<typeof T> // type T = {
1330
+ // x: number,
1331
+ // y: number
1332
+ // }
1371
1333
  ```
1372
1334
 
1373
1335
  <a name="syntax-parameters"></a>
1374
1336
 
1375
1337
  ### Parameters
1376
1338
 
1377
- Syntax types can be parameterized to accept exterior types.
1339
+ Syntax types can be parameterized to receive exterior types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgCgJDCOhrwAKnAC8KUbgAUAAyQ4AXHHQBXEHx5QANHFQHjp8wS0BKOK7ev27ODLmKDCgHk+ACseBhgAHgQJd1i4+ITEpLdPN304BQA5EzNLGOSCwqK4VNcbDOz7KHzi2rqPL3wAPikfeRQVNUxNJCV8Ky0ABSxYYCwmCIUm52LUtvhkfyDQ8Kia+o2C0rh0wLAYYFlxycrcpot1zav47fK9g6OJrJzzFuv3m8amoA))
1378
1340
 
1379
1341
  ```typescript
1380
- const T = Syntax('number')
1381
-
1382
- const S = Syntax({ T }, `{ x: T, y: T, z: T }`) // const S: TObject<{
1342
+ const T = Syntax(`{ x: number, y: number }`) // const T: TObject<{
1383
1343
  // x: TNumber,
1384
- // y: TNumber,
1385
- // z: TNumber
1344
+ // y: TNumber
1345
+ // }>
1346
+
1347
+ const S = Syntax({ T }, `Partial<T>`) // const S: TObject<{
1348
+ // x: TOptional<TNumber>,
1349
+ // y: TOptional<TNumber>
1386
1350
  // }>
1387
1351
  ```
1388
1352
 
1353
+
1354
+
1389
1355
  <a name="syntax-generics"></a>
1390
1356
 
1391
1357
  ### Generics
1392
1358
 
1393
- Generic types can be created using Argument types.
1359
+ Syntax types support generic parameters in the following way ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgChQkWIhTYYwBgWKly1OoxZtO3foMkSGEdDXgA1HgxjQ4AXhSjcACgAGAHgAaAGjgBNXwAtAD45CTg4HAAuOB84cLhUGID4iIAvGMD4-FcASgkjEzM4ACEsOhpLa2gae0dMFyQqmygCX1cEBOi4Zuh3AEZfAAZh4O8EpJ6rFvcRuEG4IbGEjKnqqFnh337lnPyJLl5S8uBK6Zq65AUld0OeCCJjit6oGlCIiPZ2ODun05fag5Oh8QaCweCIZCoV8Pt0kN0FpM5qshm0ElCMZisSCYRFJvCYnNJgsUWjseSKeDcXBVgTFr4kb5Vv0COjKezsTD8EA))
1394
1360
 
1395
1361
  ```typescript
1396
- const Vector = Syntax(`{
1397
- x: Argument<0>,
1398
- y: Argument<1>,
1399
- z: Argument<2>
1362
+ const Vector = Syntax(`<X, Y, Z> {
1363
+ x: X,
1364
+ y: Y,
1365
+ z: Z
1400
1366
  }`)
1401
1367
 
1402
- const Basis = Syntax({ Vector }, `{
1368
+ const BasisVectors = Syntax({ Vector }, `{
1403
1369
  x: Vector<1, 0, 0>,
1404
1370
  y: Vector<0, 1, 0>,
1405
1371
  z: Vector<0, 0, 1>,
1406
1372
  }`)
1407
1373
 
1408
- type Basis = Static<typeof Basis> // type Basis = {
1374
+ type BasisVectors = Static<typeof BasisVectors> // type BasisVectors = {
1409
1375
  // x: { x: 1, y: 0, z: 0 },
1410
1376
  // y: { x: 0, y: 1, z: 0 },
1411
1377
  // z: { x: 0, y: 0, z: 1 }
1412
- // }
1378
+ // }
1379
+ ```
1380
+
1381
+ <a name='syntax-options'></a>
1382
+
1383
+ ### Options
1384
+
1385
+ Options can be passed via the last parameter
1386
+
1387
+ ```typescript
1388
+ const T = Syntax(`number`, { minimum: 42 }) // const T = {
1389
+ // type: 'number',
1390
+ // minimum: 42
1391
+ // }
1413
1392
  ```
1414
1393
 
1415
1394
  <a name='typeregistry'></a>
@@ -1573,6 +1552,44 @@ const C = TypeCompiler.Code(Type.String()) // const C = `return functi
1573
1552
  // }`
1574
1553
  ```
1575
1554
 
1555
+ <a name='typemap'></a>
1556
+
1557
+ ## TypeMap
1558
+
1559
+ TypeBox offers an external package for bi-directional mapping between TypeBox, Valibot, and Zod type libraries. It also includes syntax parsing support for Valibot and Zod and supports the Standard Schema specification. For more details on TypeMap, refer to the project repository.
1560
+
1561
+ [TypeMap Repository](https://github.com/sinclairzx81/typemap)
1562
+
1563
+ <a name='typemap-usage'></a>
1564
+
1565
+ ### Usage
1566
+
1567
+ TypeMap needs to be installed seperately
1568
+
1569
+ ```bash
1570
+ $ npm install @sinclair/typemap
1571
+ ```
1572
+
1573
+ Once installed it offers advanced structural remapping between various runtime type libraries ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAyqgHYwCGBcAWhACZwC+cAZlBCHAOQACAzsFIBjADYVgUAPQx0GEBTDcAUMuERS-eMjgBeFHJy4AFAAMkuAFxxSAVxAAjDFEKprdx88IAvd-adQzKYAlHBwUlJw6pra1sgA8g4AVhjCMAA8CMphObl5+QWFRcW5ETlWKABy-s4A3NkljU3NBWVhblU1UPUtvX3FbXC+nZ7dDf0TE2VMAHyq0VrEesRklCbIoS1lC-BE1twWfqOuRwE+p87MKmoaiwBKy3T0xkTBAHRgFFD8GMZ2oqJNnltrd4HdrFlJltImEKh4Aj0oU1Bh14XVxkiBjChhcxpjGtMwkA))
1574
+
1575
+ ```typescript
1576
+ import { TypeBox, Syntax, Zod } from '@sinclair/typemap'
1577
+
1578
+ const T = TypeBox(`{ x: number, y: number, z: number }`) // const T: TObject<{
1579
+ // x: TNumber;
1580
+ // y: TNumber;
1581
+ // z: TNumber;
1582
+ // }>
1583
+
1584
+ const S = Syntax(T) // const S: '{ x: number, y: number, z: number }'
1585
+
1586
+ const R = Zod(S).parse(null) // const R: {
1587
+ // x: number;
1588
+ // y: number;
1589
+ // z: number;
1590
+ // }
1591
+ ```
1592
+
1576
1593
  <a name='typesystem'></a>
1577
1594
 
1578
1595
  ## TypeSystem
@@ -1 +0,0 @@
1
- export * from './remap';
@@ -1,18 +0,0 @@
1
- "use strict";
2
-
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
- };
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- __exportStar(require("./remap"), exports);
@@ -1,30 +0,0 @@
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;
14
- export interface TMapping {
15
- input: unknown;
16
- output: unknown;
17
- }
18
- type TApply<Type extends TSchema, Mapping extends TMapping, Mapped = (Mapping & {
19
- input: Type;
20
- })['output'], Result = Mapped extends TSchema ? Mapped : never> = Result;
21
- type TFromProperties<Properties extends TProperties, Mapping extends TMapping, Result extends TProperties = {
22
- [Key in keyof Properties]: TRemap<Properties[Key], Mapping>;
23
- }> = 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;
26
- /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
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;
28
- /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
29
- export declare function Remap(type: TSchema, callback: TCallback): TSchema;
30
- export {};
@@ -1,47 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.Remap = Remap;
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");
18
- function FromProperties(properties, func) {
19
- return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
20
- return { ...result, [key]: Remap(properties[key], func) };
21
- }, {});
22
- }
23
- function FromTypes(types, callback) {
24
- return types.map((type) => Remap(type, callback));
25
- }
26
- function FromType(type, callback) {
27
- return callback(type);
28
- }
29
- /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
30
- // prettier-ignore
31
- function Remap(type, callback) {
32
- // Map incoming type
33
- const mapped = (0, type_1.CloneType)(FromType(type, callback));
34
- // Return remapped interior
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));
47
- }
@@ -1 +0,0 @@
1
- export * from './remap.mjs';
@@ -1 +0,0 @@
1
- export * from './remap.mjs';
@@ -1,30 +0,0 @@
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;
14
- export interface TMapping {
15
- input: unknown;
16
- output: unknown;
17
- }
18
- type TApply<Type extends TSchema, Mapping extends TMapping, Mapped = (Mapping & {
19
- input: Type;
20
- })['output'], Result = Mapped extends TSchema ? Mapped : never> = Result;
21
- type TFromProperties<Properties extends TProperties, Mapping extends TMapping, Result extends TProperties = {
22
- [Key in keyof Properties]: TRemap<Properties[Key], Mapping>;
23
- }> = 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;
26
- /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
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;
28
- /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
29
- export declare function Remap(type: TSchema, callback: TCallback): TSchema;
30
- export {};
@@ -1,43 +0,0 @@
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';
14
- function FromProperties(properties, func) {
15
- return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
16
- return { ...result, [key]: Remap(properties[key], func) };
17
- }, {});
18
- }
19
- function FromTypes(types, callback) {
20
- return types.map((type) => Remap(type, callback));
21
- }
22
- function FromType(type, callback) {
23
- return callback(type);
24
- }
25
- /** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
26
- // prettier-ignore
27
- export function Remap(type, callback) {
28
- // Map incoming type
29
- const mapped = CloneType(FromType(type, callback));
30
- // Return remapped interior
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));
43
- }