@sinclair/typebox 0.34.18 → 0.34.20
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.
|
@@ -4,14 +4,12 @@ import type { Static } from '../static/index';
|
|
|
4
4
|
import type { Evaluate, Ensure, Assert } from '../helpers/index';
|
|
5
5
|
import { type TAny } from '../any/index';
|
|
6
6
|
import { type TBoolean } from '../boolean/index';
|
|
7
|
-
import { type TComputed } from '../computed/index';
|
|
8
7
|
import { type TEnumRecord, type TEnum } from '../enum/index';
|
|
9
8
|
import { type TInteger } from '../integer/index';
|
|
10
9
|
import { type TLiteral, type TLiteralValue } from '../literal/index';
|
|
11
10
|
import { type TNever } from '../never/index';
|
|
12
11
|
import { type TNumber } from '../number/index';
|
|
13
12
|
import { type TObject, type TProperties, type TAdditionalProperties, type ObjectOptions } from '../object/index';
|
|
14
|
-
import { type TRef } from '../ref/index';
|
|
15
13
|
import { type TRegExp } from '../regexp/index';
|
|
16
14
|
import { type TString } from '../string/index';
|
|
17
15
|
import { type TUnion } from '../union/index';
|
|
@@ -57,7 +55,7 @@ export interface TRecord<Key extends TSchema = TSchema, Type extends TSchema = T
|
|
|
57
55
|
};
|
|
58
56
|
additionalProperties: TAdditionalProperties;
|
|
59
57
|
}
|
|
60
|
-
export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = (
|
|
58
|
+
export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = (Key extends TTemplateLiteral ? TFromTemplateLiteralKey<Key, Type> : Key extends TEnum<infer Enum extends TEnumRecord> ? TFromEnumKey<Enum, Type> : Key extends TUnion<infer Types extends TSchema[]> ? TFromUnionKey<Types, Type> : Key extends TLiteral<infer Value extends TLiteralValue> ? TFromLiteralKey<Value, Type> : Key extends TBoolean ? TFromBooleanKey<Key, Type> : Key extends TInteger ? TFromIntegerKey<Key, Type> : Key extends TNumber ? TFromNumberKey<Key, Type> : Key extends TRegExp ? TFromRegExpKey<Key, Type> : Key extends TString ? TFromStringKey<Key, Type> : Key extends TAny ? TFromAnyKey<Key, Type> : Key extends TNever ? TFromNeverKey<Key, Type> : TNever);
|
|
61
59
|
/** `[Json]` Creates a Record type */
|
|
62
60
|
export declare function Record<Key extends TSchema, Type extends TSchema>(key: Key, type: Type, options?: ObjectOptions): TRecordOrObject<Key, Type>;
|
|
63
61
|
/** Gets the Records Pattern */
|
|
@@ -7,16 +7,14 @@ exports.RecordKey = RecordKey;
|
|
|
7
7
|
exports.RecordValue = RecordValue;
|
|
8
8
|
const type_1 = require("../create/type");
|
|
9
9
|
const index_1 = require("../symbols/index");
|
|
10
|
-
const index_2 = require("../
|
|
11
|
-
const index_3 = require("../
|
|
12
|
-
const index_4 = require("../
|
|
13
|
-
const index_5 = require("../
|
|
14
|
-
const index_6 = require("../
|
|
15
|
-
const index_7 = require("../
|
|
16
|
-
const index_8 = require("../
|
|
17
|
-
const index_9 = require("../
|
|
18
|
-
const index_10 = require("../patterns/index");
|
|
19
|
-
const index_11 = require("../indexed/index");
|
|
10
|
+
const index_2 = require("../never/index");
|
|
11
|
+
const index_3 = require("../number/index");
|
|
12
|
+
const index_4 = require("../object/index");
|
|
13
|
+
const index_5 = require("../string/index");
|
|
14
|
+
const index_6 = require("../union/index");
|
|
15
|
+
const index_7 = require("../template-literal/index");
|
|
16
|
+
const index_8 = require("../patterns/index");
|
|
17
|
+
const index_9 = require("../indexed/index");
|
|
20
18
|
// ------------------------------------------------------------------
|
|
21
19
|
// ValueGuard
|
|
22
20
|
// ------------------------------------------------------------------
|
|
@@ -40,17 +38,17 @@ function RecordCreateFromKeys(K, T, options) {
|
|
|
40
38
|
const result = {};
|
|
41
39
|
for (const K2 of K)
|
|
42
40
|
result[K2] = T;
|
|
43
|
-
return (0,
|
|
41
|
+
return (0, index_4.Object)(result, { ...options, [index_1.Hint]: 'Record' });
|
|
44
42
|
}
|
|
45
43
|
// prettier-ignore
|
|
46
44
|
function FromTemplateLiteralKey(K, T, options) {
|
|
47
|
-
return ((0,
|
|
48
|
-
? RecordCreateFromKeys((0,
|
|
45
|
+
return ((0, index_7.IsTemplateLiteralFinite)(K)
|
|
46
|
+
? RecordCreateFromKeys((0, index_9.IndexPropertyKeys)(K), T, options)
|
|
49
47
|
: RecordCreateFromPattern(K.pattern, T, options));
|
|
50
48
|
}
|
|
51
49
|
// prettier-ignore
|
|
52
50
|
function FromUnionKey(key, type, options) {
|
|
53
|
-
return RecordCreateFromKeys((0,
|
|
51
|
+
return RecordCreateFromKeys((0, index_9.IndexPropertyKeys)((0, index_6.Union)(key)), type, options);
|
|
54
52
|
}
|
|
55
53
|
// prettier-ignore
|
|
56
54
|
function FromLiteralKey(key, type, options) {
|
|
@@ -62,28 +60,28 @@ function FromRegExpKey(key, type, options) {
|
|
|
62
60
|
}
|
|
63
61
|
// prettier-ignore
|
|
64
62
|
function FromStringKey(key, type, options) {
|
|
65
|
-
const pattern = (0, value_1.IsUndefined)(key.pattern) ?
|
|
63
|
+
const pattern = (0, value_1.IsUndefined)(key.pattern) ? index_8.PatternStringExact : key.pattern;
|
|
66
64
|
return RecordCreateFromPattern(pattern, type, options);
|
|
67
65
|
}
|
|
68
66
|
// prettier-ignore
|
|
69
67
|
function FromAnyKey(_, type, options) {
|
|
70
|
-
return RecordCreateFromPattern(
|
|
68
|
+
return RecordCreateFromPattern(index_8.PatternStringExact, type, options);
|
|
71
69
|
}
|
|
72
70
|
// prettier-ignore
|
|
73
71
|
function FromNeverKey(_key, type, options) {
|
|
74
|
-
return RecordCreateFromPattern(
|
|
72
|
+
return RecordCreateFromPattern(index_8.PatternNeverExact, type, options);
|
|
75
73
|
}
|
|
76
74
|
// prettier-ignore
|
|
77
75
|
function FromBooleanKey(_key, type, options) {
|
|
78
|
-
return (0,
|
|
76
|
+
return (0, index_4.Object)({ true: type, false: type }, options);
|
|
79
77
|
}
|
|
80
78
|
// prettier-ignore
|
|
81
79
|
function FromIntegerKey(_key, type, options) {
|
|
82
|
-
return RecordCreateFromPattern(
|
|
80
|
+
return RecordCreateFromPattern(index_8.PatternNumberExact, type, options);
|
|
83
81
|
}
|
|
84
82
|
// prettier-ignore
|
|
85
83
|
function FromNumberKey(_, type, options) {
|
|
86
|
-
return RecordCreateFromPattern(
|
|
84
|
+
return RecordCreateFromPattern(index_8.PatternNumberExact, type, options);
|
|
87
85
|
}
|
|
88
86
|
// ------------------------------------------------------------------
|
|
89
87
|
// TRecordOrObject
|
|
@@ -91,20 +89,17 @@ function FromNumberKey(_, type, options) {
|
|
|
91
89
|
/** `[Json]` Creates a Record type */
|
|
92
90
|
function Record(key, type, options = {}) {
|
|
93
91
|
// prettier-ignore
|
|
94
|
-
return ((0, kind_1.
|
|
95
|
-
(0, kind_1.
|
|
96
|
-
(0, kind_1.
|
|
97
|
-
(0, kind_1.
|
|
98
|
-
(0, kind_1.
|
|
99
|
-
(0, kind_1.
|
|
100
|
-
(0, kind_1.
|
|
101
|
-
(0, kind_1.
|
|
102
|
-
(0, kind_1.
|
|
103
|
-
(0, kind_1.
|
|
104
|
-
(0,
|
|
105
|
-
(0, kind_1.IsAny)(key) ? FromAnyKey(key, type, options) :
|
|
106
|
-
(0, kind_1.IsNever)(key) ? FromNeverKey(key, type, options) :
|
|
107
|
-
(0, index_3.Never)(options));
|
|
92
|
+
return ((0, kind_1.IsUnion)(key) ? FromUnionKey(key.anyOf, type, options) :
|
|
93
|
+
(0, kind_1.IsTemplateLiteral)(key) ? FromTemplateLiteralKey(key, type, options) :
|
|
94
|
+
(0, kind_1.IsLiteral)(key) ? FromLiteralKey(key.const, type, options) :
|
|
95
|
+
(0, kind_1.IsBoolean)(key) ? FromBooleanKey(key, type, options) :
|
|
96
|
+
(0, kind_1.IsInteger)(key) ? FromIntegerKey(key, type, options) :
|
|
97
|
+
(0, kind_1.IsNumber)(key) ? FromNumberKey(key, type, options) :
|
|
98
|
+
(0, kind_1.IsRegExp)(key) ? FromRegExpKey(key, type, options) :
|
|
99
|
+
(0, kind_1.IsString)(key) ? FromStringKey(key, type, options) :
|
|
100
|
+
(0, kind_1.IsAny)(key) ? FromAnyKey(key, type, options) :
|
|
101
|
+
(0, kind_1.IsNever)(key) ? FromNeverKey(key, type, options) :
|
|
102
|
+
(0, index_2.Never)(options));
|
|
108
103
|
}
|
|
109
104
|
// ------------------------------------------------------------------
|
|
110
105
|
// Record Utilities
|
|
@@ -117,9 +112,9 @@ function RecordPattern(record) {
|
|
|
117
112
|
// prettier-ignore
|
|
118
113
|
function RecordKey(type) {
|
|
119
114
|
const pattern = RecordPattern(type);
|
|
120
|
-
return (pattern ===
|
|
121
|
-
pattern ===
|
|
122
|
-
(0,
|
|
115
|
+
return (pattern === index_8.PatternStringExact ? (0, index_5.String)() :
|
|
116
|
+
pattern === index_8.PatternNumberExact ? (0, index_3.Number)() :
|
|
117
|
+
(0, index_5.String)({ pattern }));
|
|
123
118
|
}
|
|
124
119
|
/** Gets a Record Value Type */
|
|
125
120
|
// prettier-ignore
|
|
@@ -4,14 +4,12 @@ import type { Static } from '../static/index.mjs';
|
|
|
4
4
|
import type { Evaluate, Ensure, Assert } from '../helpers/index.mjs';
|
|
5
5
|
import { type TAny } from '../any/index.mjs';
|
|
6
6
|
import { type TBoolean } from '../boolean/index.mjs';
|
|
7
|
-
import { type TComputed } from '../computed/index.mjs';
|
|
8
7
|
import { type TEnumRecord, type TEnum } from '../enum/index.mjs';
|
|
9
8
|
import { type TInteger } from '../integer/index.mjs';
|
|
10
9
|
import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
|
|
11
10
|
import { type TNever } from '../never/index.mjs';
|
|
12
11
|
import { type TNumber } from '../number/index.mjs';
|
|
13
12
|
import { type TObject, type TProperties, type TAdditionalProperties, type ObjectOptions } from '../object/index.mjs';
|
|
14
|
-
import { type TRef } from '../ref/index.mjs';
|
|
15
13
|
import { type TRegExp } from '../regexp/index.mjs';
|
|
16
14
|
import { type TString } from '../string/index.mjs';
|
|
17
15
|
import { type TUnion } from '../union/index.mjs';
|
|
@@ -57,7 +55,7 @@ export interface TRecord<Key extends TSchema = TSchema, Type extends TSchema = T
|
|
|
57
55
|
};
|
|
58
56
|
additionalProperties: TAdditionalProperties;
|
|
59
57
|
}
|
|
60
|
-
export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = (
|
|
58
|
+
export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = (Key extends TTemplateLiteral ? TFromTemplateLiteralKey<Key, Type> : Key extends TEnum<infer Enum extends TEnumRecord> ? TFromEnumKey<Enum, Type> : Key extends TUnion<infer Types extends TSchema[]> ? TFromUnionKey<Types, Type> : Key extends TLiteral<infer Value extends TLiteralValue> ? TFromLiteralKey<Value, Type> : Key extends TBoolean ? TFromBooleanKey<Key, Type> : Key extends TInteger ? TFromIntegerKey<Key, Type> : Key extends TNumber ? TFromNumberKey<Key, Type> : Key extends TRegExp ? TFromRegExpKey<Key, Type> : Key extends TString ? TFromStringKey<Key, Type> : Key extends TAny ? TFromAnyKey<Key, Type> : Key extends TNever ? TFromNeverKey<Key, Type> : TNever);
|
|
61
59
|
/** `[Json]` Creates a Record type */
|
|
62
60
|
export declare function Record<Key extends TSchema, Type extends TSchema>(key: Key, type: Type, options?: ObjectOptions): TRecordOrObject<Key, Type>;
|
|
63
61
|
/** Gets the Records Pattern */
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { CreateType } from '../create/type.mjs';
|
|
2
2
|
import { Kind, Hint } from '../symbols/index.mjs';
|
|
3
|
-
import { Computed } from '../computed/index.mjs';
|
|
4
3
|
import { Never } from '../never/index.mjs';
|
|
5
4
|
import { Number } from '../number/index.mjs';
|
|
6
5
|
import { Object } from '../object/index.mjs';
|
|
7
|
-
import { Ref } from '../ref/index.mjs';
|
|
8
6
|
import { String } from '../string/index.mjs';
|
|
9
7
|
import { Union } from '../union/index.mjs';
|
|
10
8
|
import { IsTemplateLiteralFinite } from '../template-literal/index.mjs';
|
|
@@ -17,7 +15,7 @@ import { IsUndefined } from '../guard/value.mjs';
|
|
|
17
15
|
// ------------------------------------------------------------------
|
|
18
16
|
// TypeGuard
|
|
19
17
|
// ------------------------------------------------------------------
|
|
20
|
-
import { IsInteger, IsLiteral, IsAny, IsBoolean, IsNever, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion
|
|
18
|
+
import { IsInteger, IsLiteral, IsAny, IsBoolean, IsNever, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion } from '../guard/kind.mjs';
|
|
21
19
|
// ------------------------------------------------------------------
|
|
22
20
|
// RecordCreateFromPattern
|
|
23
21
|
// ------------------------------------------------------------------
|
|
@@ -84,20 +82,17 @@ function FromNumberKey(_, type, options) {
|
|
|
84
82
|
/** `[Json]` Creates a Record type */
|
|
85
83
|
export function Record(key, type, options = {}) {
|
|
86
84
|
// prettier-ignore
|
|
87
|
-
return (
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
IsAny(key) ? FromAnyKey(key, type, options) :
|
|
99
|
-
IsNever(key) ? FromNeverKey(key, type, options) :
|
|
100
|
-
Never(options));
|
|
85
|
+
return (IsUnion(key) ? FromUnionKey(key.anyOf, type, options) :
|
|
86
|
+
IsTemplateLiteral(key) ? FromTemplateLiteralKey(key, type, options) :
|
|
87
|
+
IsLiteral(key) ? FromLiteralKey(key.const, type, options) :
|
|
88
|
+
IsBoolean(key) ? FromBooleanKey(key, type, options) :
|
|
89
|
+
IsInteger(key) ? FromIntegerKey(key, type, options) :
|
|
90
|
+
IsNumber(key) ? FromNumberKey(key, type, options) :
|
|
91
|
+
IsRegExp(key) ? FromRegExpKey(key, type, options) :
|
|
92
|
+
IsString(key) ? FromStringKey(key, type, options) :
|
|
93
|
+
IsAny(key) ? FromAnyKey(key, type, options) :
|
|
94
|
+
IsNever(key) ? FromNeverKey(key, type, options) :
|
|
95
|
+
Never(options));
|
|
101
96
|
}
|
|
102
97
|
// ------------------------------------------------------------------
|
|
103
98
|
// Record Utilities
|
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>
|