@sinclair/typebox 0.32.0-dev-20 → 0.32.0-dev-21
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/import/compiler/compiler.mjs +62 -62
- package/build/import/errors/errors.mjs +62 -62
- package/build/import/index.d.mts +4 -4
- package/build/import/index.mjs +1 -1
- package/build/import/type/indexed/indexed-property-keys.d.mts +2 -2
- package/build/import/type/indexed/indexed-property-keys.mjs +6 -11
- package/build/import/type/intrinsic/intrinsic.mjs +3 -3
- package/build/import/type/record/record.d.mts +2 -2
- package/build/import/type/record/record.mjs +2 -3
- package/build/import/type/schema/schema.d.mts +1 -1
- package/build/import/type/symbol/symbol.d.mts +1 -1
- package/build/import/type/template-literal/finite.d.mts +7 -5
- package/build/import/type/template-literal/finite.mjs +14 -5
- package/build/import/type/template-literal/generate.d.mts +14 -8
- package/build/import/type/template-literal/generate.mjs +19 -9
- package/build/import/type/template-literal/index.d.mts +1 -1
- package/build/import/type/template-literal/index.mjs +1 -1
- package/build/import/type/template-literal/{parser.mjs → parse.mjs} +7 -2
- package/build/import/type/template-literal/syntax.d.mts +1 -1
- package/build/import/type/template-literal/syntax.mjs +0 -1
- package/build/import/type/template-literal/template-literal.d.mts +2 -2
- package/build/import/type/template-literal/template-literal.mjs +1 -1
- package/build/import/type/template-literal/union.d.mts +2 -2
- package/build/import/type/template-literal/union.mjs +5 -10
- package/build/import/type/type/json.d.mts +2 -2
- package/build/import/value/cast/cast.mjs +20 -20
- package/build/import/value/check/check.mjs +62 -62
- package/build/import/value/clean/clean.mjs +16 -16
- package/build/import/value/convert/convert.mjs +36 -36
- package/build/import/value/create/create.mjs +66 -67
- package/build/import/value/default/default.mjs +16 -16
- package/build/import/value/transform/decode.mjs +18 -18
- package/build/import/value/transform/encode.mjs +18 -18
- package/build/import/value/transform/has.mjs +28 -28
- package/build/require/compiler/compiler.js +62 -62
- package/build/require/errors/errors.js +62 -62
- package/build/require/index.d.ts +4 -4
- package/build/require/index.js +4 -2
- package/build/require/type/indexed/indexed-property-keys.d.ts +2 -2
- package/build/require/type/indexed/indexed-property-keys.js +4 -9
- package/build/require/type/intrinsic/intrinsic.js +2 -2
- package/build/require/type/record/record.d.ts +2 -2
- package/build/require/type/record/record.js +1 -2
- package/build/require/type/schema/schema.d.ts +1 -1
- package/build/require/type/symbol/symbol.d.ts +1 -1
- package/build/require/type/template-literal/finite.d.ts +7 -5
- package/build/require/type/template-literal/finite.js +16 -6
- package/build/require/type/template-literal/generate.d.ts +14 -8
- package/build/require/type/template-literal/generate.js +22 -11
- package/build/require/type/template-literal/index.d.ts +1 -1
- package/build/require/type/template-literal/index.js +1 -1
- package/build/require/type/template-literal/{parser.js → parse.js} +7 -2
- package/build/require/type/template-literal/syntax.d.ts +1 -1
- package/build/require/type/template-literal/syntax.js +0 -1
- package/build/require/type/template-literal/template-literal.d.ts +2 -2
- package/build/require/type/template-literal/template-literal.js +1 -1
- package/build/require/type/template-literal/union.d.ts +2 -2
- package/build/require/type/template-literal/union.js +5 -10
- package/build/require/type/type/json.d.ts +2 -2
- package/build/require/value/cast/cast.js +20 -20
- package/build/require/value/check/check.js +62 -62
- package/build/require/value/clean/clean.js +16 -16
- package/build/require/value/convert/convert.js +36 -36
- package/build/require/value/create/create.js +65 -66
- package/build/require/value/default/default.js +16 -16
- package/build/require/value/transform/decode.js +18 -18
- package/build/require/value/transform/encode.js +18 -18
- package/build/require/value/transform/has.js +28 -28
- package/package.json +1 -1
- package/readme.md +47 -47
- /package/build/import/type/template-literal/{parser.d.mts → parse.d.mts} +0 -0
- /package/build/require/type/template-literal/{parser.d.ts → parse.d.ts} +0 -0
|
@@ -49,13 +49,13 @@ function Default(schema, value) {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
// prettier-ignore
|
|
52
|
-
function
|
|
52
|
+
function FromArray(schema, references, value) {
|
|
53
53
|
return ((0, index_6.IsArray)(value))
|
|
54
54
|
? Default(schema, value.map((value) => Visit(schema.items, references, value)))
|
|
55
55
|
: Default(schema, value);
|
|
56
56
|
}
|
|
57
57
|
// prettier-ignore
|
|
58
|
-
function
|
|
58
|
+
function FromIntersect(schema, references, value) {
|
|
59
59
|
if (!(0, index_6.IsPlainObject)(value) || (0, index_6.IsValueType)(value))
|
|
60
60
|
return Default(schema, value);
|
|
61
61
|
const knownKeys = (0, index_2.KeyOfPropertyKeys)(schema);
|
|
@@ -76,11 +76,11 @@ function TIntersect(schema, references, value) {
|
|
|
76
76
|
}, knownProperties);
|
|
77
77
|
return Default(schema, unknownProperties);
|
|
78
78
|
}
|
|
79
|
-
function
|
|
79
|
+
function FromNot(schema, references, value) {
|
|
80
80
|
return Default(schema, Visit(schema.not, references, value));
|
|
81
81
|
}
|
|
82
82
|
// prettier-ignore
|
|
83
|
-
function
|
|
83
|
+
function FromObject(schema, references, value) {
|
|
84
84
|
if (!(0, index_6.IsPlainObject)(value))
|
|
85
85
|
return Default(schema, value);
|
|
86
86
|
const knownKeys = (0, index_2.KeyOfPropertyKeys)(schema);
|
|
@@ -102,7 +102,7 @@ function TObject(schema, references, value) {
|
|
|
102
102
|
return Default(schema, unknownProperties);
|
|
103
103
|
}
|
|
104
104
|
// prettier-ignore
|
|
105
|
-
function
|
|
105
|
+
function FromRecord(schema, references, value) {
|
|
106
106
|
if (!(0, index_6.IsPlainObject)(value))
|
|
107
107
|
return Default(schema, value);
|
|
108
108
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
@@ -125,23 +125,23 @@ function TRecord(schema, references, value) {
|
|
|
125
125
|
return Default(schema, unknownProperties);
|
|
126
126
|
}
|
|
127
127
|
// prettier-ignore
|
|
128
|
-
function
|
|
128
|
+
function FromRef(schema, references, value) {
|
|
129
129
|
const target = (0, index_4.Deref)(schema, references);
|
|
130
130
|
return Default(schema, Visit(target, references, value));
|
|
131
131
|
}
|
|
132
132
|
// prettier-ignore
|
|
133
|
-
function
|
|
133
|
+
function FromThis(schema, references, value) {
|
|
134
134
|
const target = (0, index_4.Deref)(schema, references);
|
|
135
135
|
return Default(schema, Visit(target, references, value));
|
|
136
136
|
}
|
|
137
137
|
// prettier-ignore
|
|
138
|
-
function
|
|
138
|
+
function FromTuple(schema, references, value) {
|
|
139
139
|
return ((0, index_6.IsArray)(value) && (0, index_6.IsArray)(schema.items))
|
|
140
140
|
? Default(schema, schema.items.map((schema, index) => Visit(schema, references, value[index])))
|
|
141
141
|
: Default(schema, value);
|
|
142
142
|
}
|
|
143
143
|
// prettier-ignore
|
|
144
|
-
function
|
|
144
|
+
function FromUnion(schema, references, value) {
|
|
145
145
|
for (const subschema of schema.anyOf) {
|
|
146
146
|
if (!(0, index_5.Check)(subschema, references, value))
|
|
147
147
|
continue;
|
|
@@ -157,25 +157,25 @@ function Visit(schema, references, value) {
|
|
|
157
157
|
const schema_ = schema;
|
|
158
158
|
switch (schema[index_1.Kind]) {
|
|
159
159
|
case 'Array':
|
|
160
|
-
return
|
|
160
|
+
return FromArray(schema_, references_, value);
|
|
161
161
|
case 'Intersect':
|
|
162
|
-
return
|
|
162
|
+
return FromIntersect(schema_, references_, value);
|
|
163
163
|
case 'Not':
|
|
164
|
-
return
|
|
164
|
+
return FromNot(schema_, references_, value);
|
|
165
165
|
case 'Object':
|
|
166
|
-
return
|
|
166
|
+
return FromObject(schema_, references_, value);
|
|
167
167
|
case 'Record':
|
|
168
|
-
return
|
|
168
|
+
return FromRecord(schema_, references_, value);
|
|
169
169
|
case 'Ref':
|
|
170
|
-
return
|
|
170
|
+
return FromRef(schema_, references_, value);
|
|
171
171
|
case 'Symbol':
|
|
172
172
|
return Default(schema_, value);
|
|
173
173
|
case 'This':
|
|
174
|
-
return
|
|
174
|
+
return FromThis(schema_, references_, value);
|
|
175
175
|
case 'Tuple':
|
|
176
|
-
return
|
|
176
|
+
return FromTuple(schema_, references_, value);
|
|
177
177
|
case 'Union':
|
|
178
|
-
return
|
|
178
|
+
return FromUnion(schema_, references_, value);
|
|
179
179
|
default:
|
|
180
180
|
return Default(schema_, value);
|
|
181
181
|
}
|
|
@@ -48,14 +48,14 @@ function Default(schema, value) {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
// prettier-ignore
|
|
51
|
-
function
|
|
51
|
+
function FromArray(schema, references, value) {
|
|
52
52
|
const defaulted = Default(schema, value);
|
|
53
53
|
return (0, index_6.IsArray)(defaulted)
|
|
54
54
|
? defaulted.map((value) => Visit(schema.items, references, value))
|
|
55
55
|
: defaulted;
|
|
56
56
|
}
|
|
57
57
|
// prettier-ignore
|
|
58
|
-
function
|
|
58
|
+
function FromIntersect(schema, references, value) {
|
|
59
59
|
const defaulted = Default(schema, value);
|
|
60
60
|
if (!(0, index_6.IsPlainObject)(value) || (0, index_6.IsValueType)(value))
|
|
61
61
|
return defaulted;
|
|
@@ -77,11 +77,11 @@ function TIntersect(schema, references, value) {
|
|
|
77
77
|
}, knownProperties);
|
|
78
78
|
}
|
|
79
79
|
// prettier-ignore
|
|
80
|
-
function
|
|
80
|
+
function FromNot(schema, references, value) {
|
|
81
81
|
return Default(schema.not, Default(schema, value));
|
|
82
82
|
}
|
|
83
83
|
// prettier-ignore
|
|
84
|
-
function
|
|
84
|
+
function FromObject(schema, references, value) {
|
|
85
85
|
const defaulted = Default(schema, value);
|
|
86
86
|
if (!(0, index_6.IsPlainObject)(value))
|
|
87
87
|
return defaulted;
|
|
@@ -103,7 +103,7 @@ function TObject(schema, references, value) {
|
|
|
103
103
|
}, knownProperties);
|
|
104
104
|
}
|
|
105
105
|
// prettier-ignore
|
|
106
|
-
function
|
|
106
|
+
function FromRecord(schema, references, value) {
|
|
107
107
|
const defaulted = Default(schema, value);
|
|
108
108
|
if (!(0, index_6.IsPlainObject)(value))
|
|
109
109
|
return defaulted;
|
|
@@ -126,24 +126,24 @@ function TRecord(schema, references, value) {
|
|
|
126
126
|
}, knownProperties);
|
|
127
127
|
}
|
|
128
128
|
// prettier-ignore
|
|
129
|
-
function
|
|
129
|
+
function FromRef(schema, references, value) {
|
|
130
130
|
const target = (0, index_4.Deref)(schema, references);
|
|
131
131
|
const resolved = Visit(target, references, value);
|
|
132
132
|
return Default(schema, resolved);
|
|
133
133
|
}
|
|
134
134
|
// prettier-ignore
|
|
135
|
-
function
|
|
135
|
+
function FromThis(schema, references, value) {
|
|
136
136
|
const target = (0, index_4.Deref)(schema, references);
|
|
137
137
|
const resolved = Visit(target, references, value);
|
|
138
138
|
return Default(schema, resolved);
|
|
139
139
|
}
|
|
140
140
|
// prettier-ignore
|
|
141
|
-
function
|
|
141
|
+
function FromTuple(schema, references, value) {
|
|
142
142
|
const value1 = Default(schema, value);
|
|
143
143
|
return (0, index_6.IsArray)(schema.items) ? schema.items.map((schema, index) => Visit(schema, references, value1[index])) : [];
|
|
144
144
|
}
|
|
145
145
|
// prettier-ignore
|
|
146
|
-
function
|
|
146
|
+
function FromUnion(schema, references, value) {
|
|
147
147
|
// test value against union variants
|
|
148
148
|
for (const subschema of schema.anyOf) {
|
|
149
149
|
if (!(0, index_5.Check)(subschema, references, value))
|
|
@@ -166,23 +166,23 @@ function Visit(schema, references, value) {
|
|
|
166
166
|
const schema_ = schema;
|
|
167
167
|
switch (schema[index_1.Kind]) {
|
|
168
168
|
case 'Array':
|
|
169
|
-
return
|
|
169
|
+
return FromArray(schema_, references_, value);
|
|
170
170
|
case 'Intersect':
|
|
171
|
-
return
|
|
171
|
+
return FromIntersect(schema_, references_, value);
|
|
172
172
|
case 'Not':
|
|
173
|
-
return
|
|
173
|
+
return FromNot(schema_, references_, value);
|
|
174
174
|
case 'Object':
|
|
175
|
-
return
|
|
175
|
+
return FromObject(schema_, references_, value);
|
|
176
176
|
case 'Record':
|
|
177
|
-
return
|
|
177
|
+
return FromRecord(schema_, references_, value);
|
|
178
178
|
case 'Ref':
|
|
179
|
-
return
|
|
179
|
+
return FromRef(schema_, references_, value);
|
|
180
180
|
case 'This':
|
|
181
|
-
return
|
|
181
|
+
return FromThis(schema_, references_, value);
|
|
182
182
|
case 'Tuple':
|
|
183
|
-
return
|
|
183
|
+
return FromTuple(schema_, references_, value);
|
|
184
184
|
case 'Union':
|
|
185
|
-
return
|
|
185
|
+
return FromUnion(schema_, references_, value);
|
|
186
186
|
default:
|
|
187
187
|
return Default(schema_, value);
|
|
188
188
|
}
|
|
@@ -13,67 +13,67 @@ const type_1 = require("../../type/guard/type");
|
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
const index_3 = require("../guard/index");
|
|
15
15
|
// prettier-ignore
|
|
16
|
-
function
|
|
16
|
+
function FromArray(schema, references) {
|
|
17
17
|
return (0, type_1.IsTransform)(schema) || Visit(schema.items, references);
|
|
18
18
|
}
|
|
19
19
|
// prettier-ignore
|
|
20
|
-
function
|
|
20
|
+
function FromAsyncIterator(schema, references) {
|
|
21
21
|
return (0, type_1.IsTransform)(schema) || Visit(schema.items, references);
|
|
22
22
|
}
|
|
23
23
|
// prettier-ignore
|
|
24
|
-
function
|
|
24
|
+
function FromConstructor(schema, references) {
|
|
25
25
|
return (0, type_1.IsTransform)(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references));
|
|
26
26
|
}
|
|
27
27
|
// prettier-ignore
|
|
28
|
-
function
|
|
28
|
+
function FromFunction(schema, references) {
|
|
29
29
|
return (0, type_1.IsTransform)(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references));
|
|
30
30
|
}
|
|
31
31
|
// prettier-ignore
|
|
32
|
-
function
|
|
32
|
+
function FromIntersect(schema, references) {
|
|
33
33
|
return (0, type_1.IsTransform)(schema) || (0, type_1.IsTransform)(schema.unevaluatedProperties) || schema.allOf.some((schema) => Visit(schema, references));
|
|
34
34
|
}
|
|
35
35
|
// prettier-ignore
|
|
36
|
-
function
|
|
36
|
+
function FromIterator(schema, references) {
|
|
37
37
|
return (0, type_1.IsTransform)(schema) || Visit(schema.items, references);
|
|
38
38
|
}
|
|
39
39
|
// prettier-ignore
|
|
40
|
-
function
|
|
40
|
+
function FromNot(schema, references) {
|
|
41
41
|
return (0, type_1.IsTransform)(schema) || Visit(schema.not, references);
|
|
42
42
|
}
|
|
43
43
|
// prettier-ignore
|
|
44
|
-
function
|
|
44
|
+
function FromObject(schema, references) {
|
|
45
45
|
return ((0, type_1.IsTransform)(schema) ||
|
|
46
46
|
Object.values(schema.properties).some((schema) => Visit(schema, references)) ||
|
|
47
47
|
((0, type_1.IsSchema)(schema.additionalProperties) && Visit(schema.additionalProperties, references)));
|
|
48
48
|
}
|
|
49
49
|
// prettier-ignore
|
|
50
|
-
function
|
|
50
|
+
function FromPromise(schema, references) {
|
|
51
51
|
return (0, type_1.IsTransform)(schema) || Visit(schema.item, references);
|
|
52
52
|
}
|
|
53
53
|
// prettier-ignore
|
|
54
|
-
function
|
|
54
|
+
function FromRecord(schema, references) {
|
|
55
55
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
56
56
|
const property = schema.patternProperties[pattern];
|
|
57
57
|
return (0, type_1.IsTransform)(schema) || Visit(property, references) || ((0, type_1.IsSchema)(schema.additionalProperties) && (0, type_1.IsTransform)(schema.additionalProperties));
|
|
58
58
|
}
|
|
59
59
|
// prettier-ignore
|
|
60
|
-
function
|
|
60
|
+
function FromRef(schema, references) {
|
|
61
61
|
if ((0, type_1.IsTransform)(schema))
|
|
62
62
|
return true;
|
|
63
63
|
return Visit((0, index_1.Deref)(schema, references), references);
|
|
64
64
|
}
|
|
65
65
|
// prettier-ignore
|
|
66
|
-
function
|
|
66
|
+
function FromThis(schema, references) {
|
|
67
67
|
if ((0, type_1.IsTransform)(schema))
|
|
68
68
|
return true;
|
|
69
69
|
return Visit((0, index_1.Deref)(schema, references), references);
|
|
70
70
|
}
|
|
71
71
|
// prettier-ignore
|
|
72
|
-
function
|
|
72
|
+
function FromTuple(schema, references) {
|
|
73
73
|
return (0, type_1.IsTransform)(schema) || (!(0, index_3.IsUndefined)(schema.items) && schema.items.some((schema) => Visit(schema, references)));
|
|
74
74
|
}
|
|
75
75
|
// prettier-ignore
|
|
76
|
-
function
|
|
76
|
+
function FromUnion(schema, references) {
|
|
77
77
|
return (0, type_1.IsTransform)(schema) || schema.anyOf.some((schema) => Visit(schema, references));
|
|
78
78
|
}
|
|
79
79
|
// prettier-ignore
|
|
@@ -86,33 +86,33 @@ function Visit(schema, references) {
|
|
|
86
86
|
visited.add(schema.$id);
|
|
87
87
|
switch (schema[index_2.Kind]) {
|
|
88
88
|
case 'Array':
|
|
89
|
-
return
|
|
89
|
+
return FromArray(schema_, references_);
|
|
90
90
|
case 'AsyncIterator':
|
|
91
|
-
return
|
|
91
|
+
return FromAsyncIterator(schema_, references_);
|
|
92
92
|
case 'Constructor':
|
|
93
|
-
return
|
|
93
|
+
return FromConstructor(schema_, references_);
|
|
94
94
|
case 'Function':
|
|
95
|
-
return
|
|
95
|
+
return FromFunction(schema_, references_);
|
|
96
96
|
case 'Intersect':
|
|
97
|
-
return
|
|
97
|
+
return FromIntersect(schema_, references_);
|
|
98
98
|
case 'Iterator':
|
|
99
|
-
return
|
|
99
|
+
return FromIterator(schema_, references_);
|
|
100
100
|
case 'Not':
|
|
101
|
-
return
|
|
101
|
+
return FromNot(schema_, references_);
|
|
102
102
|
case 'Object':
|
|
103
|
-
return
|
|
103
|
+
return FromObject(schema_, references_);
|
|
104
104
|
case 'Promise':
|
|
105
|
-
return
|
|
105
|
+
return FromPromise(schema_, references_);
|
|
106
106
|
case 'Record':
|
|
107
|
-
return
|
|
107
|
+
return FromRecord(schema_, references_);
|
|
108
108
|
case 'Ref':
|
|
109
|
-
return
|
|
109
|
+
return FromRef(schema_, references_);
|
|
110
110
|
case 'This':
|
|
111
|
-
return
|
|
111
|
+
return FromThis(schema_, references_);
|
|
112
112
|
case 'Tuple':
|
|
113
|
-
return
|
|
113
|
+
return FromTuple(schema_, references_);
|
|
114
114
|
case 'Union':
|
|
115
|
-
return
|
|
115
|
+
return FromUnion(schema_, references_);
|
|
116
116
|
default:
|
|
117
117
|
return (0, type_1.IsTransform)(schema);
|
|
118
118
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -51,9 +51,9 @@ type T = Static<typeof T> // type T = {
|
|
|
51
51
|
|
|
52
52
|
## Overview
|
|
53
53
|
|
|
54
|
-
TypeBox is a runtime type builder that creates in-memory Json Schema objects that
|
|
54
|
+
TypeBox is a runtime type builder that creates in-memory Json Schema objects that infer as TypeScript types. The Json Schema produced by this library is designed to match the static type checking rules of the TypeScript compiler. TypeBox provides a unified type that can be statically checked by the TypeScript compiler and runtime asserted using standard Json Schema validation.
|
|
55
55
|
|
|
56
|
-
This library enables Json Schema to
|
|
56
|
+
This library enables Json Schema to be created with the same expressiveness as TypeScript's type system. It can be used either as a simple tool to build up complex schematics or integrated into REST or RPC services to help validate data received over the wire.
|
|
57
57
|
|
|
58
58
|
License MIT
|
|
59
59
|
|
|
@@ -70,9 +70,9 @@ License MIT
|
|
|
70
70
|
- [Generics](#types-generics)
|
|
71
71
|
- [References](#types-references)
|
|
72
72
|
- [Recursive](#types-recursive)
|
|
73
|
+
- [Template Literal](#types-template-literal)
|
|
73
74
|
- [Conditional](#types-conditional)
|
|
74
75
|
- [Mapped](#types-mapped)
|
|
75
|
-
- [Template Literal](#types-templateliteral)
|
|
76
76
|
- [Indexed](#types-indexed)
|
|
77
77
|
- [Intrinsic](#types-intrinsic)
|
|
78
78
|
- [Transform](#types-transform)
|
|
@@ -647,7 +647,7 @@ TypeBox provides an extended type set that can be used to create schematics for
|
|
|
647
647
|
|
|
648
648
|
### Modules
|
|
649
649
|
|
|
650
|
-
TypeBox supports a modular type system that allows for selective type imports. Using selective imports can be helpful in resource constrained environments as it enables modern bundlers to tree shake unused types
|
|
650
|
+
TypeBox supports a modular type system that allows for selective type imports. Using selective imports can be helpful in resource constrained environments as it enables modern bundlers to tree shake unused types. This can lead to reduced bundle sizes.
|
|
651
651
|
|
|
652
652
|
```typescript
|
|
653
653
|
import { Object, Number, String, Boolean, type Static } from '@sinclair/typebox'
|
|
@@ -882,6 +882,41 @@ function test(node: Node) {
|
|
|
882
882
|
}
|
|
883
883
|
```
|
|
884
884
|
|
|
885
|
+
<a name='types-template-literal'></a>
|
|
886
|
+
|
|
887
|
+
### Template Literal Types
|
|
888
|
+
|
|
889
|
+
TypeBox supports template literals with Type.TemplateLiteral. This type can be created using a syntax similar to the TypeScript template literal syntax or composed from exterior types. TypeBox encodes template literals as regular expression patterns which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing, enabling template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
|
|
890
|
+
|
|
891
|
+
```typescript
|
|
892
|
+
// TypeScript
|
|
893
|
+
|
|
894
|
+
type K = `prop${'A'|'B'|'C'}` // type T = 'propA' | 'propB' | 'propC'
|
|
895
|
+
|
|
896
|
+
type R = Record<K, string> // type R = {
|
|
897
|
+
// propA: string
|
|
898
|
+
// propB: string
|
|
899
|
+
// propC: string
|
|
900
|
+
// }
|
|
901
|
+
|
|
902
|
+
// TypeBox
|
|
903
|
+
|
|
904
|
+
const K = Type.TemplateLiteral('prop${A|B|C}') // const K: TTemplateLiteral<[
|
|
905
|
+
// TLiteral<'prop'>,
|
|
906
|
+
// TUnion<[
|
|
907
|
+
// TLiteral<'A'>,
|
|
908
|
+
// TLiteral<'B'>,
|
|
909
|
+
// TLiteral<'C'>,
|
|
910
|
+
// ]>
|
|
911
|
+
// ]>
|
|
912
|
+
|
|
913
|
+
const R = Type.Record(K, Type.String()) // const R: TObject<{
|
|
914
|
+
// hello1: TString,
|
|
915
|
+
// hello2: TString,
|
|
916
|
+
// hello3: TString,
|
|
917
|
+
// }>
|
|
918
|
+
```
|
|
919
|
+
|
|
885
920
|
<a name='types-conditional'></a>
|
|
886
921
|
|
|
887
922
|
### Conditional Types
|
|
@@ -931,7 +966,7 @@ const T = Type.Exclude( // const T: TUnion<[
|
|
|
931
966
|
|
|
932
967
|
### Mapped Types
|
|
933
968
|
|
|
934
|
-
TypeBox supports Mapped types with Type.Mapped. This type accepts a union of
|
|
969
|
+
TypeBox supports Mapped types with Type.Mapped. This type accepts two arguments, the first a union of literal keys and the second a type mapping function which receives a mapping key `K`. The following remaps the type `T` to be `Partial<T>` using type mapping.
|
|
935
970
|
|
|
936
971
|
```typescript
|
|
937
972
|
const T = Type.Object({ // const T: TObject<{
|
|
@@ -940,10 +975,10 @@ const T = Type.Object({ // const T: TObject<{
|
|
|
940
975
|
z: Type.Boolean() // z: TBoolean
|
|
941
976
|
}) // }>
|
|
942
977
|
|
|
943
|
-
const M = Type.Mapped(Type.KeyOf(T), K => { // type M = {
|
|
944
|
-
return Type.Optional(Type.Index(T, K)) //
|
|
945
|
-
}) //
|
|
946
|
-
|
|
978
|
+
const M = Type.Mapped(Type.KeyOf(T), K => { // type M = { [K in keyof T]?: T[K] }
|
|
979
|
+
return Type.Optional(Type.Index(T, K)) //
|
|
980
|
+
}) //
|
|
981
|
+
|
|
947
982
|
// ... runtime mapped as
|
|
948
983
|
//
|
|
949
984
|
// const M: TObject<{
|
|
@@ -953,41 +988,6 @@ const M = Type.Mapped(Type.KeyOf(T), K => { // type M = {
|
|
|
953
988
|
// }>
|
|
954
989
|
```
|
|
955
990
|
|
|
956
|
-
<a name='types-templateliteral'></a>
|
|
957
|
-
|
|
958
|
-
### Template Literal Types
|
|
959
|
-
|
|
960
|
-
TypeBox supports template literals with Type.TemplateLiteral. This type can be created using a syntax similar to the TypeScript template literal syntax or composed from exterior types. TypeBox encodes template literals as regular expression patterns which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing, enabling template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
|
|
961
|
-
|
|
962
|
-
```typescript
|
|
963
|
-
// TypeScript
|
|
964
|
-
|
|
965
|
-
type K = `prop${'A'|'B'|'C'}` // type T = 'propA' | 'propB' | 'propC'
|
|
966
|
-
|
|
967
|
-
type R = Record<K, string> // type R = {
|
|
968
|
-
// propA: string
|
|
969
|
-
// propB: string
|
|
970
|
-
// propC: string
|
|
971
|
-
// }
|
|
972
|
-
|
|
973
|
-
// TypeBox
|
|
974
|
-
|
|
975
|
-
const K = Type.TemplateLiteral('prop${A|B|C}') // const K: TTemplateLiteral<[
|
|
976
|
-
// TLiteral<'prop'>,
|
|
977
|
-
// TUnion<[
|
|
978
|
-
// TLiteral<'A'>,
|
|
979
|
-
// TLiteral<'B'>,
|
|
980
|
-
// TLiteral<'C'>,
|
|
981
|
-
// ]>
|
|
982
|
-
// ]>
|
|
983
|
-
|
|
984
|
-
const R = Type.Record(K, Type.String()) // const R: TObject<{
|
|
985
|
-
// hello1: TString,
|
|
986
|
-
// hello2: TString,
|
|
987
|
-
// hello3: TString,
|
|
988
|
-
// }>
|
|
989
|
-
```
|
|
990
|
-
|
|
991
991
|
<a name='types-indexed'></a>
|
|
992
992
|
|
|
993
993
|
### Indexed Access Types
|
|
@@ -1157,13 +1157,13 @@ TypeBox types contain various symbol properties that are used for reflection, co
|
|
|
1157
1157
|
|
|
1158
1158
|
```typescript
|
|
1159
1159
|
const T = Type.Object({ // const T = {
|
|
1160
|
-
name: Type.Optional(Type.String()) // [Kind]: 'Object',
|
|
1160
|
+
name: Type.Optional(Type.String()) // [Symbol(TypeBox.Kind)]: 'Object',
|
|
1161
1161
|
}) // type: 'object',
|
|
1162
1162
|
// properties: {
|
|
1163
1163
|
// name: {
|
|
1164
1164
|
// type: 'string',
|
|
1165
|
-
// [Kind]: 'String',
|
|
1166
|
-
// [Optional]: 'Optional'
|
|
1165
|
+
// [Symbol(TypeBox.Kind)]: 'String',
|
|
1166
|
+
// [Symbol(TypeBox.Optional)]: 'Optional'
|
|
1167
1167
|
// }
|
|
1168
1168
|
// }
|
|
1169
1169
|
// }
|
|
File without changes
|
|
File without changes
|