typebox 1.1.29 → 1.1.31

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.
@@ -7,5 +7,9 @@ export type TAwaitedDeferred<Type extends TSchema> = (TDeferred<'Awaited', [Type
7
7
  export declare function AwaitedDeferred<Type extends TSchema>(type: Type, options?: TSchemaOptions): TAwaitedDeferred<Type>;
8
8
  /** Applies an Awaited action to a type. */
9
9
  export type TAwaited<Type extends TSchema> = (TAwaitedAction<Type>);
10
- /** Applies an Awaited action to a type. */
10
+ /**
11
+ * Applies an Awaited action to a type.
12
+ *
13
+ * @deprecated This action is being removed in the next version of TypeBox.
14
+ */
11
15
  export declare function Awaited<Type extends TSchema>(type: Type, options?: TSchemaOptions): TAwaited<Type>;
@@ -5,7 +5,11 @@ import { AwaitedAction } from '../engine/awaited/instantiate.mjs';
5
5
  export function AwaitedDeferred(type, options = {}) {
6
6
  return Deferred('Awaited', [type], options);
7
7
  }
8
- /** Applies an Awaited action to a type. */
8
+ /**
9
+ * Applies an Awaited action to a type.
10
+ *
11
+ * @deprecated This action is being removed in the next version of TypeBox.
12
+ */
9
13
  export function Awaited(type, options = {}) {
10
14
  return AwaitedAction(type, options);
11
15
  }
@@ -1,14 +1,16 @@
1
1
  import { type TProperties } from '../../types/properties.mjs';
2
2
  import { type TSchema } from '../../types/schema.mjs';
3
3
  import { type TNumber } from '../../types/number.mjs';
4
+ import { type TNever } from '../../types/never.mjs';
4
5
  import { type TPropertyKeys } from '../../types/properties.mjs';
5
6
  import { type TEvaluateUnion } from '../evaluate/evaluate.mjs';
6
7
  import { type TToIndexableKeys } from '../indexable/to-indexable-keys.mjs';
7
- type TSelectProperty<Properties extends TProperties, Key extends string, CanonicalKey extends string = keyof Properties extends string | number ? `${keyof Properties}` : never, Result extends TSchema[] = Key extends CanonicalKey ? [Properties[Key]] : []> = Result;
8
- type TSelectProperties<Properties extends TProperties, Keys extends string[], Result extends TSchema[] = []> = (Keys extends [infer Left extends string, ...infer Right extends string[]] ? TSelectProperties<Properties, Right, [...Result, ...TSelectProperty<Properties, Left>]> : Result);
9
- type TFromIndexer<Properties extends TProperties, Indexer extends TSchema, Keys extends string[] = TToIndexableKeys<Indexer>, Variants extends TSchema[] = TSelectProperties<Properties, Keys>, Result extends TSchema = TEvaluateUnion<Variants>> = Result;
8
+ import { type TExpandThis } from '../this/expand-this.mjs';
9
+ type TIndexProperty<Properties extends TProperties, Key extends string, CanonicalKey extends string = keyof Properties extends string | number ? `${keyof Properties}` : never, SelectedType extends TSchema = Key extends CanonicalKey ? Properties[Key] : TNever, Result extends TSchema = TExpandThis<Properties, SelectedType>> = Result;
10
+ type TIndexProperties<Properties extends TProperties, Keys extends string[], Result extends TSchema[] = []> = (Keys extends [infer Left extends string, ...infer Right extends string[]] ? TIndexProperties<Properties, Right, [...Result, TIndexProperty<Properties, Left>]> : Result);
11
+ type TFromIndexer<Properties extends TProperties, Indexer extends TSchema, Keys extends string[] = TToIndexableKeys<Indexer>, Variants extends TSchema[] = TIndexProperties<Properties, Keys>, Result extends TSchema = TEvaluateUnion<Variants>> = Result;
10
12
  type TNumericKeys<Keys extends string[], Result extends string[] = []> = (Keys extends [infer Left extends string, ...infer Right extends string[]] ? Left extends `${infer _ extends number}` ? TNumericKeys<Right, [...Result, Left]> : TNumericKeys<Right, Result> : Result);
11
- type TFromIndexerNumber<Properties extends TProperties, Keys extends string[] = TPropertyKeys<Properties>, NumericKeys extends string[] = TNumericKeys<Keys>, Variants extends TSchema[] = TSelectProperties<Properties, NumericKeys>, Result extends TSchema = TEvaluateUnion<Variants>> = Result;
13
+ type TFromIndexerNumber<Properties extends TProperties, Keys extends string[] = TPropertyKeys<Properties>, NumericKeys extends string[] = TNumericKeys<Keys>, Variants extends TSchema[] = TIndexProperties<Properties, NumericKeys>, Result extends TSchema = TEvaluateUnion<Variants>> = Result;
12
14
  export type TFromObject<Properties extends TProperties, Indexer extends TSchema, Result extends TSchema = Indexer extends TNumber ? TFromIndexerNumber<Properties> : TFromIndexer<Properties, Indexer>> = Result;
13
15
  export declare function FromObject<Properties extends TProperties, Indexer extends TSchema>(properties: Properties, indexer: Indexer): TFromObject<Properties, Indexer>;
14
16
  export {};
@@ -1,21 +1,24 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { IsNumber } from '../../types/number.mjs';
3
+ import { Never } from '../../types/never.mjs';
3
4
  import { PropertyKeys } from '../../types/properties.mjs';
4
5
  import { EvaluateUnion } from '../evaluate/evaluate.mjs';
5
6
  import { ToIndexableKeys } from '../indexable/to-indexable-keys.mjs';
6
7
  import { IntegerKey } from '../../types/record.mjs';
7
- function SelectProperty(properties, key) {
8
- const result = key in properties ? [properties[key]] : [];
8
+ import { ExpandThis } from '../this/expand-this.mjs';
9
+ function IndexProperty(properties, key) {
10
+ const selectedType = key in properties ? properties[key] : Never();
11
+ const result = ExpandThis(properties, selectedType);
9
12
  return result;
10
13
  }
11
- function SelectProperties(properties, keys) {
14
+ function IndexProperties(properties, keys) {
12
15
  return keys.reduce((result, left) => {
13
- return [...result, ...SelectProperty(properties, left)];
16
+ return [...result, IndexProperty(properties, left)];
14
17
  }, []);
15
18
  }
16
19
  function FromIndexer(properties, indexer) {
17
20
  const keys = ToIndexableKeys(indexer);
18
- const variants = SelectProperties(properties, keys);
21
+ const variants = IndexProperties(properties, keys);
19
22
  const result = EvaluateUnion(variants);
20
23
  return result;
21
24
  }
@@ -27,7 +30,7 @@ function NumericKeys(keys) {
27
30
  function FromIndexerNumber(properties) {
28
31
  const keys = PropertyKeys(properties);
29
32
  const numericKeys = NumericKeys(keys);
30
- const variants = SelectProperties(properties, numericKeys);
33
+ const variants = IndexProperties(properties, numericKeys);
31
34
  const result = EvaluateUnion(variants);
32
35
  return result;
33
36
  }
@@ -0,0 +1,19 @@
1
+ import { type TArray } from '../../types/array.mjs';
2
+ import { type TAsyncIterator } from '../../types/async-iterator.mjs';
3
+ import { type TConstructor } from '../../types/constructor.mjs';
4
+ import { type TFunction } from '../../types/function.mjs';
5
+ import { type TIterator } from '../../types/iterator.mjs';
6
+ import { type TIntersect } from '../../types/intersect.mjs';
7
+ import { type TObject } from '../../types/object.mjs';
8
+ import { type TProperties } from '../../types/properties.mjs';
9
+ import { type TSchema } from '../../types/schema.mjs';
10
+ import { type TPromise } from '../../types/promise.mjs';
11
+ import { type TTuple } from '../../types/tuple.mjs';
12
+ import { type TThis } from '../../types/this.mjs';
13
+ import { type TUnion } from '../../types/union.mjs';
14
+ type TFromTypes<Properties extends TProperties, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromTypes<Properties, Right, [...Result, TFromType<Properties, Left>]> : Result);
15
+ export type TFromType<Properties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TArray<TFromType<Properties, Type>> : Type extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TFromType<Properties, Type>> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TConstructor<TFromTypes<Properties, Parameters>, TFromType<Properties, InstanceType>> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TFromTypes<Properties, Parameters>, TFromType<Properties, ReturnType>> : Type extends TIterator<infer Type extends TSchema> ? TIterator<TFromType<Properties, Type>> : Type extends TPromise<infer Type extends TSchema> ? TPromise<TFromType<Properties, Type>> : Type extends TTuple<infer Types extends TSchema[]> ? TTuple<TFromTypes<Properties, Types>> : Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromTypes<Properties, Types>> : Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromTypes<Properties, Types>> : Type extends TThis ? TObject<Properties> : Type);
16
+ export declare function FromType<Properties extends TProperties, Type extends TSchema>(properties: Properties, type: Type): TFromType<Properties, Type>;
17
+ export type TExpandThis<Properties extends TProperties, Type extends TSchema, Result extends TSchema = TFromType<Properties, Type>> = Result;
18
+ export declare function ExpandThis<Properties extends TProperties, Type extends TSchema>(properties: TProperties, type: Type): TExpandThis<Properties, Type>;
19
+ export {};
@@ -0,0 +1,32 @@
1
+ // deno-fmt-ignore-file
2
+ import { IsArray, Array } from '../../types/array.mjs';
3
+ import { IsAsyncIterator, AsyncIterator } from '../../types/async-iterator.mjs';
4
+ import { IsConstructor, Constructor } from '../../types/constructor.mjs';
5
+ import { IsFunction, Function } from '../../types/function.mjs';
6
+ import { IsIterator, Iterator } from '../../types/iterator.mjs';
7
+ import { IsIntersect, Intersect } from '../../types/intersect.mjs';
8
+ import { Object } from '../../types/object.mjs';
9
+ import { IsPromise, Promise } from '../../types/promise.mjs';
10
+ import { IsTuple, Tuple } from '../../types/tuple.mjs';
11
+ import { IsThis } from '../../types/this.mjs';
12
+ import { IsUnion, Union } from '../../types/union.mjs';
13
+ function FromTypes(properties, types) {
14
+ return types.map(type => FromType(properties, type));
15
+ }
16
+ export function FromType(properties, type) {
17
+ return (IsArray(type) ? Array(FromType(properties, type.items)) :
18
+ IsAsyncIterator(type) ? AsyncIterator(FromType(properties, type.iteratorItems)) :
19
+ IsConstructor(type) ? Constructor(FromTypes(properties, type.parameters), FromType(properties, type.instanceType)) :
20
+ IsFunction(type) ? Function(FromTypes(properties, type.parameters), FromType(properties, type.returnType)) :
21
+ IsIterator(type) ? Iterator(FromType(properties, type.iteratorItems)) :
22
+ IsPromise(type) ? Promise(FromType(properties, type.item)) :
23
+ IsTuple(type) ? Tuple(FromTypes(properties, type.items)) :
24
+ IsUnion(type) ? Union(FromTypes(properties, type.anyOf)) :
25
+ IsIntersect(type) ? Intersect(FromTypes(properties, type.allOf)) :
26
+ IsThis(type) ? Object(properties) :
27
+ type);
28
+ }
29
+ export function ExpandThis(properties, type) {
30
+ const result = FromType(properties, type);
31
+ return result;
32
+ }
@@ -8,7 +8,11 @@ export interface TAsyncIterator<Type extends TSchema = TSchema> extends TSchema
8
8
  type: 'asyncIterator';
9
9
  iteratorItems: Type;
10
10
  }
11
- /** Creates a AsyncIterator type. */
11
+ /**
12
+ * Creates a AsyncIterator type.
13
+ *
14
+ * @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
15
+ */
12
16
  export declare function AsyncIterator<Type extends TSchema>(iteratorItems: Type, options?: TSchemaOptions): TAsyncIterator<Type>;
13
17
  /** Returns true if the given value is a TAsyncIterator */
14
18
  export declare function IsAsyncIterator(value: unknown): value is TAsyncIterator;
@@ -4,7 +4,11 @@ import { IsKind } from './schema.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  // Factory
6
6
  // ------------------------------------------------------------------
7
- /** Creates a AsyncIterator type. */
7
+ /**
8
+ * Creates a AsyncIterator type.
9
+ *
10
+ * @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
11
+ */
8
12
  export function AsyncIterator(iteratorItems, options) {
9
13
  return Memory.Create({ '~kind': 'AsyncIterator' }, { type: 'asyncIterator', iteratorItems }, options);
10
14
  }
@@ -8,7 +8,11 @@ export interface TIterator<Type extends TSchema = TSchema> extends TSchema {
8
8
  type: 'iterator';
9
9
  iteratorItems: Type;
10
10
  }
11
- /** Creates a Iterator type. */
11
+ /**
12
+ * Creates a Iterator type.
13
+ *
14
+ * @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
15
+ */
12
16
  export declare function Iterator<Type extends TSchema>(iteratorItems: Type, options?: TSchemaOptions): TIterator<Type>;
13
17
  /** Returns true if the given value is TIterator. */
14
18
  export declare function IsIterator(value: unknown): value is TIterator;
@@ -4,7 +4,11 @@ import { IsKind } from './schema.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  // Factory
6
6
  // ------------------------------------------------------------------
7
- /** Creates a Iterator type. */
7
+ /**
8
+ * Creates a Iterator type.
9
+ *
10
+ * @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
11
+ */
8
12
  export function Iterator(iteratorItems, options) {
9
13
  return Memory.Create({ '~kind': 'Iterator' }, { type: 'iterator', iteratorItems }, options);
10
14
  }
@@ -8,7 +8,11 @@ export interface TPromise<Type extends TSchema = TSchema> extends TSchema {
8
8
  type: 'promise';
9
9
  item: Type;
10
10
  }
11
- /** Creates a Promise type. */
11
+ /**
12
+ * Creates a Promise type.
13
+ *
14
+ * @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
15
+ */
12
16
  export declare function _Promise_<Type extends TSchema>(item: Type, options?: TSchemaOptions): TPromise<Type>;
13
17
  export { _Promise_ as Promise };
14
18
  /** Returns true if the given type is TPromise. */
@@ -4,7 +4,11 @@ import { IsKind } from './schema.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  // Factory
6
6
  // ------------------------------------------------------------------
7
- /** Creates a Promise type. */
7
+ /**
8
+ * Creates a Promise type.
9
+ *
10
+ * @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
11
+ */
8
12
  export function _Promise_(item, options) {
9
13
  return Memory.Create({ ['~kind']: 'Promise' }, { type: 'promise', item }, options);
10
14
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
4
- "version": "1.1.29",
4
+ "version": "1.1.31",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"
@@ -16,37 +16,37 @@
16
16
  "types": "./build/index.d.mts",
17
17
  "module": "./build/index.mjs",
18
18
  "exports": {
19
+ "./value": {
20
+ "import": "./build/value/index.mjs",
21
+ "default": "./build/value/index.mjs"
22
+ },
19
23
  "./guard": {
20
24
  "import": "./build/guard/index.mjs",
21
25
  "default": "./build/guard/index.mjs"
22
26
  },
23
- "./error": {
24
- "import": "./build/error/index.mjs",
25
- "default": "./build/error/index.mjs"
26
- },
27
- "./compile": {
28
- "import": "./build/compile/index.mjs",
29
- "default": "./build/compile/index.mjs"
27
+ "./schema": {
28
+ "import": "./build/schema/index.mjs",
29
+ "default": "./build/schema/index.mjs"
30
30
  },
31
31
  "./system": {
32
32
  "import": "./build/system/index.mjs",
33
33
  "default": "./build/system/index.mjs"
34
34
  },
35
- "./format": {
36
- "import": "./build/format/index.mjs",
37
- "default": "./build/format/index.mjs"
38
- },
39
35
  "./type": {
40
36
  "import": "./build/type/index.mjs",
41
37
  "default": "./build/type/index.mjs"
42
38
  },
43
- "./schema": {
44
- "import": "./build/schema/index.mjs",
45
- "default": "./build/schema/index.mjs"
39
+ "./compile": {
40
+ "import": "./build/compile/index.mjs",
41
+ "default": "./build/compile/index.mjs"
46
42
  },
47
- "./value": {
48
- "import": "./build/value/index.mjs",
49
- "default": "./build/value/index.mjs"
43
+ "./error": {
44
+ "import": "./build/error/index.mjs",
45
+ "default": "./build/error/index.mjs"
46
+ },
47
+ "./format": {
48
+ "import": "./build/format/index.mjs",
49
+ "default": "./build/format/index.mjs"
50
50
  },
51
51
  ".": {
52
52
  "import": "./build/index.mjs",
@@ -55,29 +55,29 @@
55
55
  },
56
56
  "typesVersions": {
57
57
  "*": {
58
+ "value": [
59
+ "./build/value/index.d.mts"
60
+ ],
58
61
  "guard": [
59
62
  "./build/guard/index.d.mts"
60
63
  ],
61
- "error": [
62
- "./build/error/index.d.mts"
63
- ],
64
- "compile": [
65
- "./build/compile/index.d.mts"
64
+ "schema": [
65
+ "./build/schema/index.d.mts"
66
66
  ],
67
67
  "system": [
68
68
  "./build/system/index.d.mts"
69
69
  ],
70
- "format": [
71
- "./build/format/index.d.mts"
72
- ],
73
70
  "type": [
74
71
  "./build/type/index.d.mts"
75
72
  ],
76
- "schema": [
77
- "./build/schema/index.d.mts"
73
+ "compile": [
74
+ "./build/compile/index.d.mts"
78
75
  ],
79
- "value": [
80
- "./build/value/index.d.mts"
76
+ "error": [
77
+ "./build/error/index.d.mts"
78
+ ],
79
+ "format": [
80
+ "./build/format/index.d.mts"
81
81
  ],
82
82
  ".": [
83
83
  "./build/index.d.mts"
package/readme.md CHANGED
@@ -80,7 +80,6 @@ The following creates a User type and infers with Static.
80
80
  ```typescript
81
81
  import Type from 'typebox'
82
82
 
83
-
84
83
  // Type
85
84
 
86
85
  const User = Type.Object({ // const User = {
@@ -100,7 +99,6 @@ const User = Type.Object({ // const User = {
100
99
  // ]
101
100
  // }
102
101
 
103
-
104
102
  // Static
105
103
 
106
104
  type User = Type.Static<typeof User> // type User = {
@@ -124,7 +122,6 @@ The following uses the Script function to parse TypeScript interfaces into JSON
124
122
  ```typescript
125
123
  import Type from 'typebox'
126
124
 
127
-
128
125
  // Script
129
126
 
130
127
  const { User, UserUpdate } = Type.Script(`
@@ -145,7 +142,6 @@ const { User, UserUpdate } = Type.Script(`
145
142
 
146
143
  `)
147
144
 
148
-
149
145
  // Reflect
150
146
 
151
147
  console.log(User) // {
@@ -172,7 +168,6 @@ console.log(UserUpdate) // {
172
168
  // required: ['id']
173
169
  // }
174
170
 
175
-
176
171
  // Static
177
172
 
178
173
  type User = Type.Static<typeof User> // type User = {
@@ -205,7 +200,6 @@ The following uses the Schema submodule to compile a TypeBox type.
205
200
  ```typescript
206
201
  import Schema from 'typebox/schema'
207
202
 
208
-
209
203
  // Compile
210
204
 
211
205
  const User = Schema.Compile(Type.Object({ // const User = Validator<Type.TObject<{
@@ -214,7 +208,6 @@ const User = Schema.Compile(Type.Object({ // const User = Validator<Type.
214
208
  email: Type.String({ format: 'email' }) // email: Type.TString;
215
209
  })) // }>, { ... }>
216
210
 
217
-
218
211
  // Parse
219
212
 
220
213
  const user = User.Parse({ // const user: {