@sinclair/typebox 0.29.0 → 0.29.1
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/package.json +1 -1
- package/readme.md +2 -1
- package/typebox.js +30 -12
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -888,7 +888,7 @@ const C = Type.Index(T, Type.KeyOf(T)) // const C = {
|
|
|
888
888
|
|
|
889
889
|
### Not Types
|
|
890
890
|
|
|
891
|
-
Not types are supported with `Type.Not`. This type represents the JSON Schema `not` keyword and will statically infer as `unknown`. Note that
|
|
891
|
+
Not types are supported with `Type.Not`. This type represents the JSON Schema `not` keyword and will statically infer as `unknown`. Note that Not types are not supported in TypeScript, but can still be partially expressed by interpreting `not` as the broad type `unknown`. When used in intersections, the Not type can be used to create refined assertion rules for specific types, with the inference derived from TypeScript's ability to narrow from `unknown` to `T` via intersection.
|
|
892
892
|
|
|
893
893
|
For example, consider a type which is `number` but not `1 | 2 | 3` and where the static type would still technically be a `number`. The following shows a pseudo TypeScript example using `not` followed by the TypeBox implementation.
|
|
894
894
|
|
|
@@ -1473,6 +1473,7 @@ The following is a list of community packages that provide general tooling and f
|
|
|
1473
1473
|
| [fetch-typebox](https://github.com/erfanium/fetch-typebox) | Drop-in replacement for fetch that brings easy integration with TypeBox |
|
|
1474
1474
|
| [schema2typebox](https://github.com/xddq/schema2typebox) | Creating TypeBox code from JSON schemas |
|
|
1475
1475
|
| [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types |
|
|
1476
|
+
| [typebox-client](https://github.com/flodlc/typebox-client) | Type safe http client library for Fastify |
|
|
1476
1477
|
| [typebox-validators](https://github.com/jtlapp/typebox-validators) | Advanced validators supporting discriminated and heterogeneous unions |
|
|
1477
1478
|
|
|
1478
1479
|
<a name='benchmark'></a>
|
package/typebox.js
CHANGED
|
@@ -887,7 +887,7 @@ var TypeExtends;
|
|
|
887
887
|
// --------------------------------------------------------------------------
|
|
888
888
|
// Not
|
|
889
889
|
// --------------------------------------------------------------------------
|
|
890
|
-
function
|
|
890
|
+
function UnwrapNot(schema) {
|
|
891
891
|
let [current, depth] = [schema, 0];
|
|
892
892
|
while (true) {
|
|
893
893
|
if (!TypeGuard.TNot(current))
|
|
@@ -897,6 +897,16 @@ var TypeExtends;
|
|
|
897
897
|
}
|
|
898
898
|
return depth % 2 === 0 ? current : exports.Type.Unknown();
|
|
899
899
|
}
|
|
900
|
+
function Not(left, right) {
|
|
901
|
+
// TypeScript has no concept of negated types, and attempts to correctly check the negated
|
|
902
|
+
// type at runtime would put TypeBox at odds with TypeScripts ability to statically infer
|
|
903
|
+
// the type. Instead we unwrap to either unknown or T and continue evaluating.
|
|
904
|
+
if (TypeGuard.TNot(left))
|
|
905
|
+
return Visit(UnwrapNot(left), right);
|
|
906
|
+
if (TypeGuard.TNot(right))
|
|
907
|
+
return Visit(left, UnwrapNot(right));
|
|
908
|
+
throw new Error(`TypeExtends: Invalid fallthrough for Not`);
|
|
909
|
+
}
|
|
900
910
|
// --------------------------------------------------------------------------
|
|
901
911
|
// Null
|
|
902
912
|
// --------------------------------------------------------------------------
|
|
@@ -1182,6 +1192,19 @@ var TypeExtends;
|
|
|
1182
1192
|
return TypeGuard.TSymbol(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1183
1193
|
}
|
|
1184
1194
|
// --------------------------------------------------------------------------
|
|
1195
|
+
// TemplateLiteral
|
|
1196
|
+
// --------------------------------------------------------------------------
|
|
1197
|
+
function TemplateLiteral(left, right) {
|
|
1198
|
+
// TemplateLiteral types are resolved to either unions for finite expressions or string
|
|
1199
|
+
// for infinite expressions. Here we call to TemplateLiteralResolver to resolve for
|
|
1200
|
+
// either type and continue evaluating.
|
|
1201
|
+
if (TypeGuard.TTemplateLiteral(left))
|
|
1202
|
+
return Visit(TemplateLiteralResolver.Resolve(left), right);
|
|
1203
|
+
if (TypeGuard.TTemplateLiteral(right))
|
|
1204
|
+
return Visit(left, TemplateLiteralResolver.Resolve(right));
|
|
1205
|
+
throw new Error(`TypeExtends: Invalid fallthrough for TemplateLiteral`);
|
|
1206
|
+
}
|
|
1207
|
+
// --------------------------------------------------------------------------
|
|
1185
1208
|
// Tuple
|
|
1186
1209
|
// --------------------------------------------------------------------------
|
|
1187
1210
|
function TupleRight(left, right) {
|
|
@@ -1317,17 +1340,12 @@ var TypeExtends;
|
|
|
1317
1340
|
return TypeGuard.TVoid(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1318
1341
|
}
|
|
1319
1342
|
function Visit(left, right) {
|
|
1320
|
-
//
|
|
1321
|
-
if (TypeGuard.
|
|
1322
|
-
return
|
|
1323
|
-
if (TypeGuard.TNot(right))
|
|
1324
|
-
return
|
|
1325
|
-
//
|
|
1326
|
-
if (TypeGuard.TTemplateLiteral(left))
|
|
1327
|
-
return Visit(TemplateLiteralResolver.Resolve(left), right);
|
|
1328
|
-
if (TypeGuard.TTemplateLiteral(right))
|
|
1329
|
-
return Visit(left, TemplateLiteralResolver.Resolve(right));
|
|
1330
|
-
// Standard Extends
|
|
1343
|
+
// Resolvable Types
|
|
1344
|
+
if (TypeGuard.TTemplateLiteral(left) || TypeGuard.TTemplateLiteral(right))
|
|
1345
|
+
return TemplateLiteral(left, right);
|
|
1346
|
+
if (TypeGuard.TNot(left) || TypeGuard.TNot(right))
|
|
1347
|
+
return Not(left, right);
|
|
1348
|
+
// Standard Types
|
|
1331
1349
|
if (TypeGuard.TAny(left))
|
|
1332
1350
|
return Any(left, right);
|
|
1333
1351
|
if (TypeGuard.TArray(left))
|