@sinclair/typebox 0.34.8 → 0.34.10

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.
Files changed (32) hide show
  1. package/build/cjs/compiler/compiler.d.ts +4 -0
  2. package/build/cjs/compiler/compiler.js +8 -0
  3. package/build/cjs/syntax/runtime.d.ts +1 -1
  4. package/build/cjs/type/indexed/indexed-from-mapped-result.js +1 -2
  5. package/build/cjs/type/indexed/indexed-property-keys.d.ts +3 -3
  6. package/build/cjs/type/indexed/indexed-property-keys.js +7 -7
  7. package/build/cjs/type/indexed/indexed.d.ts +33 -35
  8. package/build/cjs/type/indexed/indexed.js +40 -39
  9. package/build/cjs/type/module/compute.d.ts +3 -3
  10. package/build/cjs/type/module/compute.js +0 -3
  11. package/build/cjs/type/module/infer.d.ts +3 -1
  12. package/build/cjs/type/record/record.d.ts +1 -1
  13. package/build/cjs/type/type/json.d.ts +11 -5
  14. package/build/cjs/value/parse/parse.d.ts +20 -4
  15. package/build/cjs/value/parse/parse.js +73 -17
  16. package/build/esm/compiler/compiler.d.mts +4 -0
  17. package/build/esm/compiler/compiler.mjs +8 -0
  18. package/build/esm/syntax/runtime.d.mts +1 -1
  19. package/build/esm/type/indexed/indexed-from-mapped-result.mjs +1 -2
  20. package/build/esm/type/indexed/indexed-property-keys.d.mts +3 -3
  21. package/build/esm/type/indexed/indexed-property-keys.mjs +7 -7
  22. package/build/esm/type/indexed/indexed.d.mts +33 -35
  23. package/build/esm/type/indexed/indexed.mjs +39 -39
  24. package/build/esm/type/module/compute.d.mts +3 -3
  25. package/build/esm/type/module/compute.mjs +0 -3
  26. package/build/esm/type/module/infer.d.mts +3 -1
  27. package/build/esm/type/record/record.d.mts +1 -1
  28. package/build/esm/type/type/json.d.mts +11 -5
  29. package/build/esm/value/parse/parse.d.mts +20 -4
  30. package/build/esm/value/parse/parse.mjs +70 -16
  31. package/package.json +1 -1
  32. package/readme.md +43 -77
package/readme.md CHANGED
@@ -78,9 +78,6 @@ License MIT
78
78
  - [Unsafe](#types-unsafe)
79
79
  - [Syntax](#syntax)
80
80
  - [Parse](#syntax-parse)
81
- - [Compose](#syntax-compose)
82
- - [Context](#syntax-context)
83
- - [Module](#syntax-module)
84
81
  - [Static](#syntax-static)
85
82
  - [Limits](#syntax-limits)
86
83
  - [Values](#values)
@@ -1025,9 +1022,9 @@ if(TypeGuard.IsString(T)) {
1025
1022
 
1026
1023
  ## Syntax Types
1027
1024
 
1028
- TypeBox provides support for parsing TypeScript syntax directly into TypeBox Json Schema schematics. Syntax Types offer a string based DSL frontend to TypeBox's Type Builder system and can be useful for converting existing TypeScript type definitions into Json Schema schematics without reimplementation via the Type Builder.
1025
+ TypeBox has support for parsing TypeScript type annotations directly into TypeBox types. This feature supports both runtime and static parsing, with TypeBox implementing TypeScript parsers within the TypeScript type system itself. Syntax Types use the TypeBox Json Schema representations as an AST target for TypeScript types, providing a direct mapping between TypeScript syntax and Json Schema. Syntax Types are offered as a syntactical frontend to the Standard Type Builder API.
1029
1026
 
1030
- Syntax Types are provided via optional import.
1027
+ This feature is available via optional import.
1031
1028
 
1032
1029
  ```typescript
1033
1030
  import { Parse } from '@sinclair/typebox/syntax'
@@ -1037,7 +1034,7 @@ import { Parse } from '@sinclair/typebox/syntax'
1037
1034
 
1038
1035
  ### Parse
1039
1036
 
1040
- Use the Parse function to convert a TypeScript string into a TypeBox type. TypeBox will infer the appropriate TSchema type or return undefined if there is a syntax error.
1037
+ Use the Parse function to transform a TypeScript type annotation into a TypeBox type. This function will return the parsed TypeBox type or undefined on error.
1041
1038
 
1042
1039
  ```typescript
1043
1040
  const A = Parse('string') // const A: TString
@@ -1054,45 +1051,46 @@ const C = Parse(`{ x: number, y: number }`) // const C: TObject<{
1054
1051
  // }>
1055
1052
  ```
1056
1053
 
1057
-
1058
-
1059
- <a name='syntax-compose'></a>
1060
-
1061
- ### Compose
1062
-
1063
- Syntax Types are designed to be interchangeable with standard Types.
1054
+ Syntax Types can compose with Standard Types created via the Type Builder API
1064
1055
 
1065
1056
  ```typescript
1066
1057
  const T = Type.Object({ // const T: TObject<{
1067
1058
  x: Parse('number'), // x: TNumber,
1068
- y: Parse('number'), // y: TNumber,
1069
- z: Parse('number') // z: TNumber
1059
+ y: Parse('string'), // y: TString,
1060
+ z: Parse('boolean') // z: TBoolean
1070
1061
  }) // }>
1071
1062
  ```
1072
1063
 
1073
- <a name='syntax-module'></a>
1064
+ Standard Types can also be passed to and referenced within Syntax Types.
1074
1065
 
1075
- ### Module
1066
+ ```typescript
1067
+ const X = Type.Number()
1068
+ const Y = Type.String()
1069
+ const Z = Type.Boolean()
1070
+
1071
+ const T = Parse({ X, Y, Z }, `{
1072
+ x: X,
1073
+ y: Y,
1074
+ z: Z
1075
+ }`)
1076
+ ```
1076
1077
 
1077
- Syntax Types also support Module parsing. This can provide a more terse syntax for creating Module definitions, but comes with an inference performance cost. Module parsing supports interface and type alias definitions. Generics types are currently unsupported.
1078
+ Syntax Types also support Module parsing.
1078
1079
 
1079
1080
  ```typescript
1080
- const Module = Parse(`module {
1081
-
1081
+ const Foo = Parse(`module Foo {
1082
+
1083
+ export type PartialUser = Pick<User, 'id'> & Partial<Omit<User, 'id'>>
1084
+
1082
1085
  export interface User {
1083
1086
  id: string
1084
1087
  name: string
1085
1088
  email: string
1086
1089
  }
1087
-
1088
- export type PartialUser = (
1089
- Pick<User, 'id'> &
1090
- Partial<Omit<User, 'id'>>
1091
- )
1092
1090
 
1093
1091
  }`)
1094
1092
 
1095
- const PartialUser = Module.Import('PartialUser') // TImport<{...}, 'PartialUser'>
1093
+ const PartialUser = Foo.Import('PartialUser') // TImport<{...}, 'PartialUser'>
1096
1094
 
1097
1095
  type PartialUser = Static<typeof PartialUser> // type PartialUser = {
1098
1096
  // id: string,
@@ -1102,45 +1100,11 @@ type PartialUser = Static<typeof PartialUser> // type PartialUser = {
1102
1100
  // }
1103
1101
  ```
1104
1102
 
1105
- <a name='syntax-context'></a>
1106
-
1107
- ### Context
1108
-
1109
- The Parse function takes an optional leading Context object that contains external types. This Context allows the syntax to reference these external types using the property identifiers provided by the Context. The following passes the external type `T` to Parse.
1110
-
1111
- ```typescript
1112
- const T = Type.Object({ // const T: TObject<{
1113
- x: Type.Number(), // x: TNumber,
1114
- y: Type.Number(), // y: TNumber,
1115
- z: Type.Number() // z: TNumber
1116
- }) // }>
1117
-
1118
- const A = Parse({ T }, 'Partial<T>') // const A: TObject<{
1119
- // x: TOptional<TNumber>,
1120
- // y: TOptional<TNumber>,
1121
- // z: TOptional<TNumber>
1122
- // }>
1123
-
1124
- const B = Parse({ T }, 'keyof T') // const B: TUnion<[
1125
- // TLiteral<'x'>,
1126
- // TLiteral<'y'>,
1127
- // TLiteral<'z'>
1128
- // ]>
1129
-
1130
- const C = Parse({ T }, 'T & { w: number }') // const C: TIntersect<[TObject<{
1131
- // x: TNumber;
1132
- // y: TNumber;
1133
- // z: TNumber;
1134
- // }>, TObject<{
1135
- // w: TNumber;
1136
- // }>]>
1137
- ```
1138
-
1139
1103
  <a name='syntax-static'></a>
1140
1104
 
1141
1105
  ### Static
1142
1106
 
1143
- Syntax Types provide two Static types for inferring TypeScript types and TypeBox schematics from strings.
1107
+ Syntax Types provide two Static types specific to inferring TypeBox and TypeScript types from strings.
1144
1108
 
1145
1109
  ```typescript
1146
1110
  import { StaticParseAsSchema, StaticParseAsType } from '@sinclair/typebox/syntax'
@@ -1154,8 +1118,8 @@ type S = StaticParseAsSchema<{}, '{ x: number }'> // type S: TObject<{
1154
1118
  // Will infer as a type
1155
1119
 
1156
1120
  type T = StaticParseAsType<{}, '{ x: number }'> // type T = {
1157
- // x: number
1158
- //
1121
+ // x: number
1122
+ // }
1159
1123
  ```
1160
1124
 
1161
1125
 
@@ -1163,7 +1127,7 @@ type T = StaticParseAsType<{}, '{ x: number }'> // type T = {
1163
1127
 
1164
1128
  ### Limitations
1165
1129
 
1166
- Syntax Types work by having TypeBox parse TypeScript syntax within the TypeScript type system. This approach can place some strain on the TypeScript compiler and language service, potentially affecting responsiveness. While TypeBox makes a best-effort attempt to optimize for Syntax Types, users should be mindful of the following structures:
1130
+ TypeBox parses TypeScript types directly within the TypeScript type system. This process does come with an inference cost, which scales with the size and complexity of the types being parsed. Although TypeBox strives to optimize Syntax Types, users should be aware of the following structures:
1167
1131
 
1168
1132
  ```typescript
1169
1133
  // Excessively wide structures will result in instantiation limits exceeding
@@ -1190,12 +1154,12 @@ const B = Parse(`{
1190
1154
  }`)
1191
1155
  ```
1192
1156
 
1193
- In cases where Syntax Types exceed TypeScript's instantiation limits, TypeBox offers a fallback ParseOnly function, which will only parse but not infer. This function can also be used to parse types where the syntax is statically unknown to TypeScript (for example, when loading types from disk).
1157
+ If Syntax Types exceed TypeScript's instantiation limits, users are advised to fall back to the Standard Type Builder API. Alternatively, TypeBox offers a `ParseOnly` function that parses the TypeScript syntax at runtime without statically inferring the schema.
1194
1158
 
1195
1159
  ```typescript
1196
1160
  import { ParseOnly } from '@sinclair/typebox/syntax'
1197
1161
 
1198
- // Where A is TSchema | undefined
1162
+ // const A: TSchema | undefined
1199
1163
 
1200
1164
  const A = ParseOnly(`{
1201
1165
  x: {
@@ -1208,7 +1172,7 @@ const A = ParseOnly(`{
1208
1172
  }`)
1209
1173
  ```
1210
1174
 
1211
- For more information on TypeBox's parsing infrastructure, refer to the [ParseBox](https://github.com/sinclairzx81/parsebox) project.
1175
+ For more information on static parsing, refer to the [ParseBox](https://github.com/sinclairzx81/parsebox) project.
1212
1176
 
1213
1177
  <a name='values'></a>
1214
1178
 
@@ -1361,26 +1325,28 @@ const B = Value.Encode(Type.String(), 42) // throw
1361
1325
 
1362
1326
  ### Parse
1363
1327
 
1364
- Use the Parse function to parse a value or throw if invalid. This function internally uses Default, Clean, Convert and Decode to make a best effort attempt to parse the value into the expected type. This function should not be used in performance critical code paths.
1328
+ Use the Parse function to parse a value. This function calls the `Clone` `Clean`, `Default`, `Convert`, `Assert` and `Decode` Value functions in this exact order to process a value.
1365
1329
 
1366
1330
  ```typescript
1367
- const T = Type.Object({ x: Type.Number({ default: 0 }), y: Type.Number({ default: 0 }) })
1331
+ const R = Value.Parse(Type.String(), 'hello') // const R: string = "hello"
1368
1332
 
1369
- // Default
1333
+ const E = Value.Parse(Type.String(), undefined) // throws AssertError
1334
+ ```
1370
1335
 
1371
- const A = Value.Parse(T, { }) // const A = { x: 0, y: 0 }
1336
+ You can override the order in which functions are run, or omit functions entirely using the following.
1372
1337
 
1373
- // Convert
1338
+ ```typescript
1339
+ // Runs no functions.
1374
1340
 
1375
- const B = Value.Parse(T, { x: '1', y: '2' }) // const B = { x: 1, y: 2 }
1341
+ const R = Value.Parse([], Type.String(), 12345)
1376
1342
 
1377
- // Clean
1343
+ // Runs the Assert() function.
1378
1344
 
1379
- const C = Value.Parse(T, { x: 1, y: 2, z: 3 }) // const C = { x: 1, y: 2 }
1345
+ const E = Value.Parse(['Assert'], Type.String(), 12345)
1380
1346
 
1381
- // Assert
1347
+ // Runs the Convert() function followed by the Assert() function.
1382
1348
 
1383
- const D = Value.Parse(T, undefined) // throws AssertError
1349
+ const S = Value.Parse(['Convert', 'Assert'], Type.String(), 12345)
1384
1350
  ```
1385
1351
 
1386
1352
  <a name='values-equal'></a>