@sinclair/typebox 0.32.0-dev-28 → 0.32.0

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.
@@ -5,7 +5,7 @@ import type { TNumber } from '../number/index.mjs';
5
5
  import type { TSchema } from '../schema/index.mjs';
6
6
  import type { TUnion } from '../union/index.mjs';
7
7
  type TFromTemplateLiteral<T extends TTemplateLiteral, R extends string[] = TTemplateLiteralGenerate<T>> = (R);
8
- type TFromUnion<T extends TSchema[], Acc extends string[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, [...TIndexPropertyKeys<L>, ...Acc]> : Acc);
8
+ type TFromUnion<T extends TSchema[], Acc extends string[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, [...Acc, ...TIndexPropertyKeys<L>]> : Acc);
9
9
  type TFromLiteral<T extends TLiteralValue> = (T extends PropertyKey ? [`${T}`] : []);
10
10
  export type TIndexPropertyKeys<T extends TSchema> = (T extends TTemplateLiteral ? TFromTemplateLiteral<T> : T extends TUnion<infer S> ? TFromUnion<S> : T extends TLiteral<infer S> ? TFromLiteral<S> : T extends TNumber ? ['[number]'] : T extends TInteger ? ['[number]'] : [
11
11
  ]);
@@ -5,7 +5,7 @@ import type { TNumber } from '../number/index';
5
5
  import type { TSchema } from '../schema/index';
6
6
  import type { TUnion } from '../union/index';
7
7
  type TFromTemplateLiteral<T extends TTemplateLiteral, R extends string[] = TTemplateLiteralGenerate<T>> = (R);
8
- type TFromUnion<T extends TSchema[], Acc extends string[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, [...TIndexPropertyKeys<L>, ...Acc]> : Acc);
8
+ type TFromUnion<T extends TSchema[], Acc extends string[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, [...Acc, ...TIndexPropertyKeys<L>]> : Acc);
9
9
  type TFromLiteral<T extends TLiteralValue> = (T extends PropertyKey ? [`${T}`] : []);
10
10
  export type TIndexPropertyKeys<T extends TSchema> = (T extends TTemplateLiteral ? TFromTemplateLiteral<T> : T extends TUnion<infer S> ? TFromUnion<S> : T extends TLiteral<infer S> ? TFromLiteral<S> : T extends TNumber ? ['[number]'] : T extends TInteger ? ['[number]'] : [
11
11
  ]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.32.0-dev-28",
3
+ "version": "0.32.0",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -648,12 +648,14 @@ TypeBox provides an extended type set that can be used to create schematics for
648
648
 
649
649
  ### Import
650
650
 
651
- You can import Type to bring in the full type system. This is recommended for most users.
651
+ Import the Type namespace to bring in TypeBox's full type system. This is recommended for most users.
652
652
 
653
653
  ```typescript
654
654
  import { Type, type Static } from '@sinclair/typebox'
655
655
  ```
656
- You can also import types individually. This approach enables modern bundlers to tree shake unused types.
656
+
657
+ You can also selectively import types. This enables modern bundlers to tree shake for unused types.
658
+
657
659
  ```typescript
658
660
  import { Object, Number, String, Boolean, type Static } from '@sinclair/typebox'
659
661
  ```
@@ -906,11 +908,11 @@ const R = Type.Record(K, Type.String()) // const R: TObject<{
906
908
  TypeBox supports indexed access types with Index. This type enables uniform access to interior property and element types without having to extract them from the underlying schema representation. This type is supported for Object, Array, Tuple, Union and Intersect types.
907
909
 
908
910
  ```typescript
909
- const T = Type.Object({ // const T: TObject<{
910
- x: Type.Number(), // x: TNumber,
911
- y: Type.String(), // y: TString,
912
- z: Type.Boolean() // z: TBoolean
913
- }) // }>
911
+ const T = Type.Object({ // type T = {
912
+ x: Type.Number(), // x: number,
913
+ y: Type.String(), // y: string,
914
+ z: Type.Boolean() // z: boolean
915
+ }) // }
914
916
 
915
917
  const A = Type.Index(T, ['x']) // type A = T['x']
916
918
  //
@@ -945,17 +947,17 @@ const C = Type.Index(T, Type.KeyOf(T)) // type C = T[keyof T]
945
947
  TypeBox supports mapped object types with Mapped. This type accepts two arguments, the first is a union type typically derived from KeyOf, the second is a mapping function that receives a mapping key `K` that can be used to index properties of a type. The following implements Partial using mapped types.
946
948
 
947
949
  ```typescript
948
- const T = Type.Object({ // const T: TObject<{
949
- x: Type.Number(), // x: TNumber,
950
- y: Type.String(), // y: TString,
951
- z: Type.Boolean() // z: TBoolean
952
- }) // }>
950
+ const T = Type.Object({ // type T = {
951
+ x: Type.Number(), // x: number,
952
+ y: Type.String(), // y: string,
953
+ z: Type.Boolean() // z: boolean
954
+ }) // }
953
955
 
954
- const P = Type.Mapped(Type.KeyOf(T), K => { // type P = { [K in keyof T]?: T[K] }
956
+ const M = Type.Mapped(Type.KeyOf(T), K => { // type M = { [K in keyof T]?: T[K] }
955
957
  return Type.Optional(Type.Index(T, K)) //
956
958
  }) // ... evaluated as
957
959
  //
958
- // const P: TObject<{
960
+ // const M: TObject<{
959
961
  // x: TOptional<TNumber>,
960
962
  // y: TOptional<TString>,
961
963
  // z: TOptional<TBoolean>
@@ -970,29 +972,29 @@ TypeBox supports runtime conditional types with Extends. This type performs a st
970
972
 
971
973
  ```typescript
972
974
  // Extends
973
- const T = Type.Extends( // type T = string extends number ? true : false
975
+ const A = Type.Extends( // type A = string extends number ? 1 : 2
974
976
  Type.String(), //
975
977
  Type.Number(), // ... evaluated as
976
- Type.Literal(true), //
977
- Type.Literal(false) // const T: TLiteral<false>
978
+ Type.Literal(1), //
979
+ Type.Literal(2) // const A: TLiteral<2>
978
980
  )
979
981
 
980
982
  // Extract
981
- const T = Type.Extract( // type T = Extract<1 | 2 | 3, 1>
983
+ const B = Type.Extract( // type B = Extract<1 | 2 | 3, 1>
982
984
  Type.Union([ //
983
985
  Type.Literal(1), // ... evaluated as
984
986
  Type.Literal(2), //
985
- Type.Literal(3) // const T: TLiteral<1>
987
+ Type.Literal(3) // const B: TLiteral<1>
986
988
  ]),
987
989
  Type.Literal(1)
988
990
  )
989
991
 
990
992
  // Exclude
991
- const T = Type.Exclude( // type T = Exclude<1 | 2 | 3, 1>
993
+ const C = Type.Exclude( // type C = Exclude<1 | 2 | 3, 1>
992
994
  Type.Union([ //
993
995
  Type.Literal(1), // ... evaluated as
994
996
  Type.Literal(2), //
995
- Type.Literal(3) // const T: TUnion<[
997
+ Type.Literal(3) // const C: TUnion<[
996
998
  ]), // TLiteral<2>,
997
999
  Type.Literal(1) // TLiteral<3>,
998
1000
  ) // ]>
@@ -1040,11 +1042,11 @@ TypeBox supports value decoding and encoding with Transform types. These types w
1040
1042
  import { Value } from '@sinclair/typebox/value'
1041
1043
 
1042
1044
  const T = Type.Transform(Type.Number())
1043
- .Decode(value => new Date(value)) // required: number to Date
1044
- .Encode(value => value.getTime()) // required: Date to number
1045
+ .Decode(value => new Date(value)) // decode: number to Date
1046
+ .Encode(value => value.getTime()) // encode: Date to number
1045
1047
 
1046
- const decoded = Value.Decode(T, 0) // const decoded = Date(1970-01-01T00:00:00.000Z)
1047
- const encoded = Value.Encode(T, decoded) // const encoded = 0
1048
+ const D = Value.Decode(T, 0) // const D = Date(1970-01-01T00:00:00.000Z)
1049
+ const E = Value.Encode(T, D) // const E = 0
1048
1050
  ```
1049
1051
  Use the StaticEncode or StaticDecode types to infer a Transform type.
1050
1052
  ```typescript
@@ -1419,15 +1421,18 @@ The TypeBox type system can be extended with additional types and formats using
1419
1421
 
1420
1422
  ### TypeRegistry
1421
1423
 
1422
- Use the TypeRegistry to register a new type. The Kind must match the registered type name.
1424
+ Use the TypeRegistry to register a type. The Kind must match the registered type name.
1423
1425
 
1424
1426
  ```typescript
1425
- import { TypeRegistry, Symbols } from '@sinclair/typebox'
1427
+ import { TSchema, Kind, TypeRegistry } from '@sinclair/typebox'
1426
1428
 
1427
1429
  TypeRegistry.Set('Foo', (schema, value) => value === 'foo')
1428
1430
 
1429
- const A = Value.Check({ [Kind]: 'Foo' }, 'foo') // const A = true
1430
- const B = Value.Check({ [Kind]: 'Foo' }, 'bar') // const B = false
1431
+ const Foo = { [Kind]: 'Foo' } as TSchema
1432
+
1433
+ const A = Value.Check(Foo, 'foo') // const A = true
1434
+
1435
+ const B = Value.Check(Foo, 'bar') // const B = false
1431
1436
  ```
1432
1437
 
1433
1438
  <a name='typeregistry-format'></a>
@@ -1444,6 +1449,7 @@ FormatRegistry.Set('foo', (value) => value === 'foo')
1444
1449
  const T = Type.String({ format: 'foo' })
1445
1450
 
1446
1451
  const A = Value.Check(T, 'foo') // const A = true
1452
+
1447
1453
  const B = Value.Check(T, 'bar') // const B = false
1448
1454
  ```
1449
1455