@sinclair/typebox 0.32.7 → 0.32.9

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.
@@ -4,6 +4,8 @@ export { TypeRegistry, FormatRegistry } from './type/registry/index.mjs';
4
4
  export { TypeGuard, ValueGuard } from './type/guard/index.mjs';
5
5
  export { CloneType, CloneRest } from './type/clone/type.mjs';
6
6
  export { TypeBoxError } from './type/error/index.mjs';
7
+ export { SetComplement, SetDistinct, SetIncludes, SetIntersect, SetIntersectMany, SetIsSubset, SetUnion, SetUnionMany, type TSetComplement, type TSetDistinct, type TSetIncludes, type TSetIntersect, type TSetIntersectMany, type TSetIsSubset, type TSetUnion, type TSetUnionMany, } from './type/sets/index.mjs';
8
+ export { Increment, type TIncrement, type Assert, type AssertType, type AssertRest, type AssertProperties, type Ensure, type Evaluate, type TupleToIntersect, type TupleToUnion, type UnionToTuple } from './type/helpers/index.mjs';
7
9
  export { Any, type TAny } from './type/any/index.mjs';
8
10
  export { Array, type TArray, type ArrayOptions } from './type/array/index.mjs';
9
11
  export { AsyncIterator, type TAsyncIterator } from './type/async-iterator/index.mjs';
@@ -21,7 +23,6 @@ export { Exclude, type TExclude, type TExcludeFromMappedResult } from './type/ex
21
23
  export { Extends, ExtendsCheck, ExtendsResult, ExtendsUndefinedCheck, type TExtends, type ExtendsFromMappedResult, type ExtendsFromMappedKey } from './type/extends/index.mjs';
22
24
  export { Extract, type TExtract, type TExtractFromMappedResult } from './type/extract/index.mjs';
23
25
  export { Function, type TFunction } from './type/function/index.mjs';
24
- export { Increment, type Assert, type AssertType, type AssertRest, type AssertProperties, type Ensure, type Evaluate, type TupleToIntersect, type TupleToUnion, type UnionToTuple } from './type/helpers/index.mjs';
25
26
  export { Index, IndexPropertyKeys, IndexFromPropertyKeys, IndexFromPropertyKey, IndexFromMappedKey, IndexFromMappedResult, type TIndex, type TIndexPropertyKeys, type TIndexFromPropertyKeys, type TIndexFromPropertyKey, type TIndexFromMappedKey, type TIndexFromMappedResult, } from './type/indexed/index.mjs';
26
27
  export { InstanceType, type TInstanceType } from './type/instance-type/index.mjs';
27
28
  export { Integer, type TInteger, type IntegerOptions } from './type/integer/index.mjs';
@@ -55,7 +56,7 @@ export { type TSchema, type TKind, type SchemaOptions, type TAnySchema } from '.
55
56
  export { type Static, type StaticDecode, type StaticEncode, type TDecodeType, type TDecodeRest, type TDecodeProperties } from './type/static/index.mjs';
56
57
  export { Strict } from './type/strict/index.mjs';
57
58
  export { String, type TString, type StringOptions, type StringFormatOption, type StringContentEncodingOption } from './type/string/index.mjs';
58
- export { Symbol, type TSymbol, type TSymbolValue as SymbolValue } from './type/symbol/index.mjs';
59
+ export { Symbol, type TSymbol, type TSymbolValue } from './type/symbol/index.mjs';
59
60
  export { TemplateLiteral, TemplateLiteralSyntax, TemplateLiteralGenerate, TemplateLiteralParse, TemplateLiteralParseExact, IsTemplateLiteralFinite, TemplateLiteralExpressionGenerate, IsTemplateLiteralExpressionFinite, type TTemplateLiteral, type TTemplateLiteralSyntax, type TTemplateLiteralGenerate, type TTemplateLiteralKind, type TIsTemplateLiteralFinite, } from './type/template-literal/index.mjs';
60
61
  export { Transform, TransformDecodeBuilder, TransformEncodeBuilder, type TTransform, type TransformOptions, type TransformFunction } from './type/transform/index.mjs';
61
62
  export { Tuple, type TTuple } from './type/tuple/index.mjs';
@@ -6,9 +6,20 @@ export { PatternBoolean, PatternBooleanExact, PatternNumber, PatternNumberExact,
6
6
  export { TypeRegistry, FormatRegistry } from './type/registry/index.mjs';
7
7
  export { TypeGuard, ValueGuard } from './type/guard/index.mjs';
8
8
  export { CloneType, CloneRest } from './type/clone/type.mjs';
9
+ // ------------------------------------------------------------------
10
+ // Error
11
+ // ------------------------------------------------------------------
9
12
  export { TypeBoxError } from './type/error/index.mjs';
10
13
  // ------------------------------------------------------------------
11
- // Type
14
+ // Sets
15
+ // ------------------------------------------------------------------
16
+ export { SetComplement, SetDistinct, SetIncludes, SetIntersect, SetIntersectMany, SetIsSubset, SetUnion, SetUnionMany, } from './type/sets/index.mjs';
17
+ // ------------------------------------------------------------------
18
+ // Helpers
19
+ // ------------------------------------------------------------------
20
+ export { Increment } from './type/helpers/index.mjs';
21
+ // ------------------------------------------------------------------
22
+ // Types
12
23
  // ------------------------------------------------------------------
13
24
  export { Any } from './type/any/index.mjs';
14
25
  export { Array } from './type/array/index.mjs';
@@ -27,7 +38,6 @@ export { Exclude } from './type/exclude/index.mjs';
27
38
  export { Extends, ExtendsCheck, ExtendsResult, ExtendsUndefinedCheck } from './type/extends/index.mjs';
28
39
  export { Extract } from './type/extract/index.mjs';
29
40
  export { Function } from './type/function/index.mjs';
30
- export { Increment } from './type/helpers/index.mjs';
31
41
  export { Index, IndexPropertyKeys, IndexFromPropertyKeys, IndexFromPropertyKey, IndexFromMappedKey, IndexFromMappedResult, } from './type/indexed/index.mjs';
32
42
  export { InstanceType } from './type/instance-type/index.mjs';
33
43
  export { Integer } from './type/integer/index.mjs';
@@ -1,13 +1,18 @@
1
- import type { UnionToTuple, Assert, Ensure, Evaluate } from '../helpers/index.mjs';
1
+ import type { TSchema } from '../schema/index.mjs';
2
+ import type { Evaluate } from '../helpers/index.mjs';
3
+ import { type TIntersectEvaluated } from '../intersect/index.mjs';
4
+ import { type TIndexFromPropertyKeys } from '../indexed/index.mjs';
5
+ import { type TKeyOfPropertyKeys } from '../keyof/index.mjs';
6
+ import { type TNever } from '../never/index.mjs';
2
7
  import { type TObject, type TProperties, type ObjectOptions } from '../object/index.mjs';
3
- import { type TIntersect } from '../intersect/index.mjs';
4
- import { type TIndex } from '../indexed/index.mjs';
5
- type TCompositeKeys<T extends TObject[], Acc extends PropertyKey = never> = T extends [infer L extends TObject, ...infer R extends TObject[]] ? TCompositeKeys<R, Acc | keyof L['properties']> : Acc;
6
- type TCompositeIndex<T extends TIntersect<TObject[]>, K extends string[], Acc extends TProperties = {}> = K extends [infer L extends string, ...infer R extends string[]] ? TCompositeIndex<T, R, Acc & {
7
- [_ in L]: TIndex<T, [L]>;
8
- }> : Acc;
9
- type TCompositeReduce<T extends TObject[]> = UnionToTuple<TCompositeKeys<T>> extends infer K ? Evaluate<TCompositeIndex<TIntersect<T>, Assert<K, string[]>>> : {};
10
- export type TComposite<T extends TObject[]> = TIntersect<T> extends TIntersect ? Ensure<TObject<TCompositeReduce<T>>> : Ensure<TObject<{}>>;
11
- /** `[Json]` Creates a Composite object type */
12
- export declare function Composite<T extends TObject[]>(T: [...T], options?: ObjectOptions): TComposite<T>;
8
+ import { TSetDistinct } from '../sets/index.mjs';
9
+ type TCompositeKeys<T extends TSchema[], Acc extends PropertyKey[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TCompositeKeys<R, TSetDistinct<[...Acc, ...TKeyOfPropertyKeys<L>]>> : Acc);
10
+ type TFilterNever<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? L extends TNever ? Acc : TFilterNever<R, [...Acc, L]> : Acc);
11
+ type TCompositeProperty<T extends TSchema[], K extends PropertyKey, Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TCompositeProperty<R, K, TFilterNever<[...Acc, ...TIndexFromPropertyKeys<L, [K]>]>> : Acc);
12
+ type TCompositeProperties<T extends TSchema[], K extends PropertyKey[], Acc = {}> = (K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? TCompositeProperties<T, R, Acc & {
13
+ [_ in L]: TIntersectEvaluated<TCompositeProperty<T, L>>;
14
+ }> : Acc);
15
+ type TCompositeEvaluate<T extends TSchema[], K extends PropertyKey[] = TCompositeKeys<T>, P extends TProperties = Evaluate<TCompositeProperties<T, K>>, R extends TObject = TObject<P>> = R;
16
+ export type TComposite<T extends TSchema[]> = TCompositeEvaluate<T>;
17
+ export declare function Composite<T extends TSchema[]>(T: [...T], options?: ObjectOptions): TComposite<T>;
13
18
  export {};
@@ -1,11 +1,38 @@
1
- import { Object } from '../object/index.mjs';
2
- import { Intersect } from '../intersect/index.mjs';
3
- import { Index } from '../indexed/index.mjs';
1
+ import { IntersectEvaluated } from '../intersect/index.mjs';
2
+ import { IndexFromPropertyKeys } from '../indexed/index.mjs';
4
3
  import { KeyOfPropertyKeys } from '../keyof/index.mjs';
5
- /** `[Json]` Creates a Composite object type */
6
- export function Composite(T, options) {
7
- const intersect = Intersect(T, {});
8
- const keys = KeyOfPropertyKeys(intersect);
9
- const properties = keys.reduce((acc, key) => ({ ...acc, [key]: Index(intersect, [key]) }), {});
10
- return Object(properties, options);
4
+ import { Object } from '../object/index.mjs';
5
+ import { SetDistinct } from '../sets/index.mjs';
6
+ // ------------------------------------------------------------------
7
+ // TypeGuard
8
+ // ------------------------------------------------------------------
9
+ import { IsNever } from '../guard/type.mjs';
10
+ // prettier-ignore
11
+ function CompositeKeys(T) {
12
+ return T.reduce((Acc, L) => {
13
+ return SetDistinct([...Acc, ...KeyOfPropertyKeys(L)]);
14
+ }, []);
15
+ }
16
+ // prettier-ignore
17
+ function FilterNever(T) {
18
+ return T.filter(L => !IsNever(L));
19
+ }
20
+ // prettier-ignore
21
+ function CompositeProperty(T, K) {
22
+ return T.reduce((Acc, L) => {
23
+ return FilterNever([...Acc, ...IndexFromPropertyKeys(L, [K])]);
24
+ }, []);
25
+ }
26
+ // prettier-ignore
27
+ function CompositeProperties(T, K) {
28
+ return K.reduce((Acc, L) => {
29
+ return { ...Acc, [L]: IntersectEvaluated(CompositeProperty(T, L)) };
30
+ }, {});
31
+ }
32
+ // prettier-ignore
33
+ export function Composite(T, options = {}) {
34
+ const K = CompositeKeys(T);
35
+ const P = CompositeProperties(T, K);
36
+ const R = Object(P, options);
37
+ return R;
11
38
  }
@@ -33,9 +33,9 @@ type IncrementBase = {
33
33
  type IncrementTake<T extends keyof IncrementBase> = IncrementBase[T];
34
34
  type IncrementStep<T extends string> = T extends IncrementBase['m'] ? IncrementBase['t'] : T extends `${infer L extends keyof IncrementBase}${infer R}` ? L extends IncrementBase['m'] ? `${IncrementTake<L>}${IncrementStep<R>}` : `${IncrementTake<L>}${R}` : never;
35
35
  type IncrementReverse<T extends string> = T extends `${infer L}${infer R}` ? `${IncrementReverse<R>}${L}` : T;
36
- export type Increment<T extends string> = IncrementReverse<IncrementStep<IncrementReverse<T>>>;
36
+ export type TIncrement<T extends string> = IncrementReverse<IncrementStep<IncrementReverse<T>>>;
37
37
  /** Increments the given string value + 1 */
38
- export declare function Increment<T extends string>(T: T): Increment<T>;
38
+ export declare function Increment<T extends string>(T: T): TIncrement<T>;
39
39
  export type AssertProperties<T> = T extends TProperties ? T : TProperties;
40
40
  export type AssertRest<T, E extends TSchema[] = TSchema[]> = T extends E ? T : [];
41
41
  export type AssertType<T, E extends TSchema = TSchema> = T extends E ? T : TNever;
@@ -1,5 +1,5 @@
1
1
  import type { TSchema } from '../schema/index.mjs';
2
- import { type ZeroString, type UnionToTuple, Increment } from '../helpers/index.mjs';
2
+ import { type ZeroString, type UnionToTuple, type TIncrement } from '../helpers/index.mjs';
3
3
  import type { TRecursive } from '../recursive/index.mjs';
4
4
  import type { TIntersect } from '../intersect/index.mjs';
5
5
  import type { TUnion } from '../union/index.mjs';
@@ -8,16 +8,14 @@ import type { TArray } from '../array/index.mjs';
8
8
  import type { TObject, TProperties } from '../object/index.mjs';
9
9
  import { type TSetUnionMany, type TSetIntersectMany } from '../sets/index.mjs';
10
10
  type TFromRest<T extends TSchema[], Acc extends PropertyKey[][] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Acc, TKeyOfPropertyKeys<L>]> : Acc);
11
- type FromIntersect<T extends TSchema[], C extends PropertyKey[][] = TFromRest<T>, R extends PropertyKey[] = TSetUnionMany<C>> = R;
12
- declare function FromIntersect<T extends TSchema[]>(T: [...T]): FromIntersect<T>;
13
- type FromUnion<T extends TSchema[], C extends PropertyKey[][] = TFromRest<T>, R extends PropertyKey[] = TSetIntersectMany<C>> = R;
14
- declare function FromUnion<T extends TSchema[]>(T: [...T]): FromUnion<T>;
15
- type TFromTuple<T extends TSchema[], I extends string = ZeroString, Acc extends PropertyKey[] = []> = T extends [infer _ extends TSchema, ...infer R extends TSchema[]] ? TFromTuple<R, Increment<I>, [...Acc, I]> : Acc;
11
+ type TFromIntersect<T extends TSchema[], C extends PropertyKey[][] = TFromRest<T>, R extends PropertyKey[] = TSetUnionMany<C>> = R;
12
+ type TFromUnion<T extends TSchema[], C extends PropertyKey[][] = TFromRest<T>, R extends PropertyKey[] = TSetIntersectMany<C>> = R;
13
+ type TFromTuple<T extends TSchema[], I extends string = ZeroString, Acc extends PropertyKey[] = []> = T extends [infer _ extends TSchema, ...infer R extends TSchema[]] ? TFromTuple<R, TIncrement<I>, [...Acc, I]> : Acc;
16
14
  type TFromArray<_ extends TSchema> = ([
17
15
  '[number]'
18
16
  ]);
19
17
  type TFromProperties<T extends TProperties> = (UnionToTuple<keyof T>);
20
- export type TKeyOfPropertyKeys<T extends TSchema> = (T extends TRecursive<infer S> ? TKeyOfPropertyKeys<S> : T extends TIntersect<infer S> ? FromIntersect<S> : T extends TUnion<infer S> ? FromUnion<S> : T extends TTuple<infer S> ? TFromTuple<S> : T extends TArray<infer S> ? TFromArray<S> : T extends TObject<infer S> ? TFromProperties<S> : [
18
+ export type TKeyOfPropertyKeys<T extends TSchema> = (T extends TRecursive<infer S> ? TKeyOfPropertyKeys<S> : T extends TIntersect<infer S> ? TFromIntersect<S> : T extends TUnion<infer S> ? TFromUnion<S> : T extends TTuple<infer S> ? TFromTuple<S> : T extends TArray<infer S> ? TFromArray<S> : T extends TObject<infer S> ? TFromProperties<S> : [
21
19
  ]);
22
20
  /** Returns a tuple of PropertyKeys derived from the given TSchema. */
23
21
  export declare function KeyOfPropertyKeys<T extends TSchema>(T: T): TKeyOfPropertyKeys<T>;
@@ -1,24 +1,28 @@
1
1
  export type TSetIncludes<T extends PropertyKey[], S extends PropertyKey> = (T extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? S extends L ? true : TSetIncludes<R, S> : false);
2
- /** Returns true if element S is in the set of T */
2
+ /** Returns true if element right is in the set of left */
3
3
  export declare function SetIncludes<T extends PropertyKey[], S extends PropertyKey>(T: [...T], S: S): TSetIncludes<T, S>;
4
- export type SetIsSubset<T extends PropertyKey[], S extends PropertyKey[]> = (T extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? TSetIncludes<S, L> extends true ? SetIsSubset<R, S> : false : true);
5
- /** Returns true if T is a subset of S */
6
- export declare function SetIsSubset<T extends PropertyKey[], S extends PropertyKey[]>(T: [...T], S: [...S]): SetIsSubset<T, S>;
4
+ export type TSetIsSubset<T extends PropertyKey[], S extends PropertyKey[]> = (T extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? TSetIncludes<S, L> extends true ? TSetIsSubset<R, S> : false : true);
5
+ /** Returns true if left is a subset of right */
6
+ export declare function SetIsSubset<T extends PropertyKey[], S extends PropertyKey[]>(T: [...T], S: [...S]): TSetIsSubset<T, S>;
7
7
  export type TSetDistinct<T extends PropertyKey[], Acc extends PropertyKey[] = []> = T extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? TSetIncludes<Acc, L> extends false ? TSetDistinct<R, [...Acc, L]> : TSetDistinct<R, [...Acc]> : Acc;
8
8
  /** Returns a distinct set of elements */
9
9
  export declare function SetDistinct<T extends PropertyKey[]>(T: [...T]): TSetDistinct<T>;
10
- export type TSetIntersect<T extends PropertyKey[], S extends PropertyKey[], Acc extends PropertyKey[] = []> = (T extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? TSetIncludes<S, L> extends true ? TSetIntersect<R, S, [L, ...Acc]> : TSetIntersect<R, S, [...Acc]> : Acc);
10
+ export type TSetIntersect<T extends PropertyKey[], S extends PropertyKey[], Acc extends PropertyKey[] = []> = (T extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? TSetIncludes<S, L> extends true ? TSetIntersect<R, S, [...Acc, L]> : TSetIntersect<R, S, [...Acc]> : Acc);
11
11
  /** Returns the Intersect of the given sets */
12
12
  export declare function SetIntersect<T extends PropertyKey[], S extends PropertyKey[]>(T: [...T], S: [...S]): TSetIntersect<T, S>;
13
- export type TSetUnion<T extends PropertyKey[], S extends PropertyKey[], Acc extends PropertyKey[] = S> = (T extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? TSetUnion<R, S, [...Acc, L]> : Acc);
13
+ export type TSetUnion<T extends PropertyKey[], S extends PropertyKey[]> = ([
14
+ ...T,
15
+ ...S
16
+ ]);
14
17
  /** Returns the Union of the given sets */
15
18
  export declare function SetUnion<T extends PropertyKey[], S extends PropertyKey[]>(T: [...T], S: [...S]): TSetUnion<T, S>;
16
19
  export type TSetComplement<T extends PropertyKey[], S extends PropertyKey[], Acc extends PropertyKey[] = []> = (T extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] ? TSetIncludes<S, L> extends true ? TSetComplement<R, S, [...Acc]> : TSetComplement<R, S, [...Acc, L]> : Acc);
17
20
  /** Returns the Complement by omitting elements in T that are in S */
18
21
  export declare function SetComplement<T extends PropertyKey[], S extends PropertyKey[]>(T: [...T], S: [...S]): TSetComplement<T, S>;
19
- export type TSetIntersectMany<T extends PropertyKey[][], Acc extends PropertyKey[] = []> = (T extends [infer L extends PropertyKey[]] ? L : T extends [infer L extends PropertyKey[], ...infer R extends PropertyKey[][]] ? TSetIntersectMany<R, TSetIntersect<Acc, L>> : Acc);
20
- /** Returns the Intersect of multiple sets */
22
+ type TSetIntersectManyResolve<T extends PropertyKey[][], Acc extends PropertyKey[]> = (T extends [infer L extends PropertyKey[], ...infer R extends PropertyKey[][]] ? TSetIntersectManyResolve<R, TSetIntersect<Acc, L>> : Acc);
23
+ export type TSetIntersectMany<T extends PropertyKey[][]> = (T extends [infer L extends PropertyKey[]] ? L : T extends [infer L extends PropertyKey[], ...infer R extends PropertyKey[][]] ? TSetIntersectManyResolve<R, L> : []);
21
24
  export declare function SetIntersectMany<T extends PropertyKey[][]>(T: [...T]): TSetIntersectMany<T>;
22
25
  export type TSetUnionMany<T extends PropertyKey[][], Acc extends PropertyKey[] = []> = (T extends [infer L extends PropertyKey[], ...infer R extends PropertyKey[][]] ? TSetUnionMany<R, TSetUnion<Acc, L>> : Acc);
23
26
  /** Returns the Union of multiple sets */
24
27
  export declare function SetUnionMany<T extends PropertyKey[][]>(T: [...T]): TSetUnionMany<T>;
28
+ export {};
@@ -1,9 +1,9 @@
1
- /** Returns true if element S is in the set of T */
1
+ /** Returns true if element right is in the set of left */
2
2
  // prettier-ignore
3
3
  export function SetIncludes(T, S) {
4
4
  return T.includes(S);
5
5
  }
6
- /** Returns true if T is a subset of S */
6
+ /** Returns true if left is a subset of right */
7
7
  export function SetIsSubset(T, S) {
8
8
  return T.every((L) => SetIncludes(S, L));
9
9
  }
@@ -24,10 +24,20 @@ export function SetUnion(T, S) {
24
24
  export function SetComplement(T, S) {
25
25
  return T.filter(L => !S.includes(L));
26
26
  }
27
- /** Returns the Intersect of multiple sets */
27
+ // prettier-ignore
28
+ function SetIntersectManyResolve(T, Init) {
29
+ return T.reduce((Acc, L) => {
30
+ return SetIntersect(Acc, L);
31
+ }, Init);
32
+ }
28
33
  // prettier-ignore
29
34
  export function SetIntersectMany(T) {
30
- return (T.length === 1 ? T[0] : T.reduce((Acc, L) => [...SetIntersect(Acc, L)], []));
35
+ return (T.length === 1
36
+ ? T[0]
37
+ // Use left to initialize the accumulator for resolve
38
+ : T.length > 1
39
+ ? SetIntersectManyResolve(T.slice(1), T[0])
40
+ : []);
31
41
  }
32
42
  /** Returns the Union of multiple sets */
33
43
  export function SetUnionMany(T) {
@@ -71,7 +71,7 @@ export declare class JsonTypeBuilder {
71
71
  /** `[Json]` Intrinsic function to Capitalize LiteralString types */
72
72
  Capitalize<T extends TSchema>(schema: T, options?: SchemaOptions): TCapitalize<T>;
73
73
  /** `[Json]` Creates a Composite object type */
74
- Composite<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TComposite<T>;
74
+ Composite<T extends TSchema[]>(schemas: [...T], options?: ObjectOptions): TComposite<T>;
75
75
  /** `[JavaScript]` Creates a readonly const type from the given value. */
76
76
  Const</* const (not supported in 4.0) */ T>(value: T, options?: SchemaOptions): TConst<T>;
77
77
  /** `[Json]` Creates a dereferenced type */
@@ -83,8 +83,8 @@ export class JsonTypeBuilder {
83
83
  return Capitalize(schema, options);
84
84
  }
85
85
  /** `[Json]` Creates a Composite object type */
86
- Composite(objects, options) {
87
- return Composite(objects, options); // (error) TS 5.4.0-dev - review TComposite implementation
86
+ Composite(schemas, options) {
87
+ return Composite(schemas, options); // (error) TS 5.4.0-dev - review TComposite implementation
88
88
  }
89
89
  /** `[JavaScript]` Creates a readonly const type from the given value. */
90
90
  Const(value, options = {}) {
@@ -53,7 +53,7 @@ function SelectUnion(union, references, value) {
53
53
  }
54
54
  function CastUnion(union, references, value) {
55
55
  if ('default' in union) {
56
- return union.default;
56
+ return typeof value === 'function' ? union.default : Clone(union.default);
57
57
  }
58
58
  else {
59
59
  const schema = SelectUnion(union, references, value);
@@ -1,5 +1,6 @@
1
1
  import { HasPropertyKey, IsString } from '../guard/index.mjs';
2
2
  import { Check } from '../check/index.mjs';
3
+ import { Clone } from '../clone/index.mjs';
3
4
  import { Deref } from '../deref/index.mjs';
4
5
  import { TemplateLiteralGenerate, IsTemplateLiteralFinite } from '../../type/template-literal/index.mjs';
5
6
  import { PatternStringExact, PatternNumberExact } from '../../type/patterns/index.mjs';
@@ -17,11 +18,17 @@ export class ValueCreateError extends TypeBoxError {
17
18
  }
18
19
  }
19
20
  // ------------------------------------------------------------------
21
+ // Default
22
+ // ------------------------------------------------------------------
23
+ function FromDefault(value) {
24
+ return typeof value === 'function' ? value : Clone(value);
25
+ }
26
+ // ------------------------------------------------------------------
20
27
  // Create
21
28
  // ------------------------------------------------------------------
22
29
  function FromAny(schema, references) {
23
30
  if (HasPropertyKey(schema, 'default')) {
24
- return schema.default;
31
+ return FromDefault(schema.default);
25
32
  }
26
33
  else {
27
34
  return {};
@@ -35,7 +42,7 @@ function FromArray(schema, references) {
35
42
  throw new ValueCreateError(schema, 'Array with the contains constraint requires a default value');
36
43
  }
37
44
  else if ('default' in schema) {
38
- return schema.default;
45
+ return FromDefault(schema.default);
39
46
  }
40
47
  else if (schema.minItems !== undefined) {
41
48
  return Array.from({ length: schema.minItems }).map((item) => {
@@ -48,7 +55,7 @@ function FromArray(schema, references) {
48
55
  }
49
56
  function FromAsyncIterator(schema, references) {
50
57
  if (HasPropertyKey(schema, 'default')) {
51
- return schema.default;
58
+ return FromDefault(schema.default);
52
59
  }
53
60
  else {
54
61
  return (async function* () { })();
@@ -56,7 +63,7 @@ function FromAsyncIterator(schema, references) {
56
63
  }
57
64
  function FromBigInt(schema, references) {
58
65
  if (HasPropertyKey(schema, 'default')) {
59
- return schema.default;
66
+ return FromDefault(schema.default);
60
67
  }
61
68
  else {
62
69
  return BigInt(0);
@@ -64,7 +71,7 @@ function FromBigInt(schema, references) {
64
71
  }
65
72
  function FromBoolean(schema, references) {
66
73
  if (HasPropertyKey(schema, 'default')) {
67
- return schema.default;
74
+ return FromDefault(schema.default);
68
75
  }
69
76
  else {
70
77
  return false;
@@ -72,7 +79,7 @@ function FromBoolean(schema, references) {
72
79
  }
73
80
  function FromConstructor(schema, references) {
74
81
  if (HasPropertyKey(schema, 'default')) {
75
- return schema.default;
82
+ return FromDefault(schema.default);
76
83
  }
77
84
  else {
78
85
  const value = Visit(schema.returns, references);
@@ -94,7 +101,7 @@ function FromConstructor(schema, references) {
94
101
  }
95
102
  function FromDate(schema, references) {
96
103
  if (HasPropertyKey(schema, 'default')) {
97
- return schema.default;
104
+ return FromDefault(schema.default);
98
105
  }
99
106
  else if (schema.minimumTimestamp !== undefined) {
100
107
  return new Date(schema.minimumTimestamp);
@@ -105,7 +112,7 @@ function FromDate(schema, references) {
105
112
  }
106
113
  function FromFunction(schema, references) {
107
114
  if (HasPropertyKey(schema, 'default')) {
108
- return schema.default;
115
+ return FromDefault(schema.default);
109
116
  }
110
117
  else {
111
118
  return () => Visit(schema.returns, references);
@@ -113,7 +120,7 @@ function FromFunction(schema, references) {
113
120
  }
114
121
  function FromInteger(schema, references) {
115
122
  if (HasPropertyKey(schema, 'default')) {
116
- return schema.default;
123
+ return FromDefault(schema.default);
117
124
  }
118
125
  else if (schema.minimum !== undefined) {
119
126
  return schema.minimum;
@@ -124,7 +131,7 @@ function FromInteger(schema, references) {
124
131
  }
125
132
  function FromIntersect(schema, references) {
126
133
  if (HasPropertyKey(schema, 'default')) {
127
- return schema.default;
134
+ return FromDefault(schema.default);
128
135
  }
129
136
  else {
130
137
  // --------------------------------------------------------------
@@ -145,7 +152,7 @@ function FromIntersect(schema, references) {
145
152
  }
146
153
  function FromIterator(schema, references) {
147
154
  if (HasPropertyKey(schema, 'default')) {
148
- return schema.default;
155
+ return FromDefault(schema.default);
149
156
  }
150
157
  else {
151
158
  return (function* () { })();
@@ -153,7 +160,7 @@ function FromIterator(schema, references) {
153
160
  }
154
161
  function FromLiteral(schema, references) {
155
162
  if (HasPropertyKey(schema, 'default')) {
156
- return schema.default;
163
+ return FromDefault(schema.default);
157
164
  }
158
165
  else {
159
166
  return schema.const;
@@ -161,7 +168,7 @@ function FromLiteral(schema, references) {
161
168
  }
162
169
  function FromNever(schema, references) {
163
170
  if (HasPropertyKey(schema, 'default')) {
164
- return schema.default;
171
+ return FromDefault(schema.default);
165
172
  }
166
173
  else {
167
174
  throw new ValueCreateError(schema, 'Never types cannot be created. Consider using a default value.');
@@ -169,7 +176,7 @@ function FromNever(schema, references) {
169
176
  }
170
177
  function FromNot(schema, references) {
171
178
  if (HasPropertyKey(schema, 'default')) {
172
- return schema.default;
179
+ return FromDefault(schema.default);
173
180
  }
174
181
  else {
175
182
  throw new ValueCreateError(schema, 'Not types must have a default value');
@@ -177,7 +184,7 @@ function FromNot(schema, references) {
177
184
  }
178
185
  function FromNull(schema, references) {
179
186
  if (HasPropertyKey(schema, 'default')) {
180
- return schema.default;
187
+ return FromDefault(schema.default);
181
188
  }
182
189
  else {
183
190
  return null;
@@ -185,7 +192,7 @@ function FromNull(schema, references) {
185
192
  }
186
193
  function FromNumber(schema, references) {
187
194
  if (HasPropertyKey(schema, 'default')) {
188
- return schema.default;
195
+ return FromDefault(schema.default);
189
196
  }
190
197
  else if (schema.minimum !== undefined) {
191
198
  return schema.minimum;
@@ -196,11 +203,11 @@ function FromNumber(schema, references) {
196
203
  }
197
204
  function FromObject(schema, references) {
198
205
  if (HasPropertyKey(schema, 'default')) {
199
- return schema.default;
206
+ return FromDefault(schema.default);
200
207
  }
201
208
  else {
202
209
  const required = new Set(schema.required);
203
- return (schema.default ||
210
+ return (FromDefault(schema.default) ||
204
211
  Object.entries(schema.properties).reduce((acc, [key, schema]) => {
205
212
  return required.has(key) ? { ...acc, [key]: Visit(schema, references) } : { ...acc };
206
213
  }, {}));
@@ -208,7 +215,7 @@ function FromObject(schema, references) {
208
215
  }
209
216
  function FromPromise(schema, references) {
210
217
  if (HasPropertyKey(schema, 'default')) {
211
- return schema.default;
218
+ return FromDefault(schema.default);
212
219
  }
213
220
  else {
214
221
  return Promise.resolve(Visit(schema.item, references));
@@ -217,7 +224,7 @@ function FromPromise(schema, references) {
217
224
  function FromRecord(schema, references) {
218
225
  const [keyPattern, valueSchema] = Object.entries(schema.patternProperties)[0];
219
226
  if (HasPropertyKey(schema, 'default')) {
220
- return schema.default;
227
+ return FromDefault(schema.default);
221
228
  }
222
229
  else if (!(keyPattern === PatternStringExact || keyPattern === PatternNumberExact)) {
223
230
  const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
@@ -231,7 +238,7 @@ function FromRecord(schema, references) {
231
238
  }
232
239
  function FromRef(schema, references) {
233
240
  if (HasPropertyKey(schema, 'default')) {
234
- return schema.default;
241
+ return FromDefault(schema.default);
235
242
  }
236
243
  else {
237
244
  return Visit(Deref(schema, references), references);
@@ -239,7 +246,7 @@ function FromRef(schema, references) {
239
246
  }
240
247
  function FromRegExp(schema, references) {
241
248
  if (HasPropertyKey(schema, 'default')) {
242
- return schema.default;
249
+ return FromDefault(schema.default);
243
250
  }
244
251
  else {
245
252
  throw new ValueCreateError(schema, 'RegExp types cannot be created. Consider using a default value.');
@@ -251,7 +258,7 @@ function FromString(schema, references) {
251
258
  throw new ValueCreateError(schema, 'String types with patterns must specify a default value');
252
259
  }
253
260
  else {
254
- return schema.default;
261
+ return FromDefault(schema.default);
255
262
  }
256
263
  }
257
264
  else if (schema.format !== undefined) {
@@ -259,12 +266,12 @@ function FromString(schema, references) {
259
266
  throw new ValueCreateError(schema, 'String types with formats must specify a default value');
260
267
  }
261
268
  else {
262
- return schema.default;
269
+ return FromDefault(schema.default);
263
270
  }
264
271
  }
265
272
  else {
266
273
  if (HasPropertyKey(schema, 'default')) {
267
- return schema.default;
274
+ return FromDefault(schema.default);
268
275
  }
269
276
  else if (schema.minLength !== undefined) {
270
277
  // prettier-ignore
@@ -277,7 +284,7 @@ function FromString(schema, references) {
277
284
  }
278
285
  function FromSymbol(schema, references) {
279
286
  if (HasPropertyKey(schema, 'default')) {
280
- return schema.default;
287
+ return FromDefault(schema.default);
281
288
  }
282
289
  else if ('value' in schema) {
283
290
  return Symbol.for(schema.value);
@@ -288,7 +295,7 @@ function FromSymbol(schema, references) {
288
295
  }
289
296
  function FromTemplateLiteral(schema, references) {
290
297
  if (HasPropertyKey(schema, 'default')) {
291
- return schema.default;
298
+ return FromDefault(schema.default);
292
299
  }
293
300
  if (!IsTemplateLiteralFinite(schema))
294
301
  throw new ValueCreateError(schema, 'Can only create template literals that produce a finite variants. Consider using a default value.');
@@ -299,7 +306,7 @@ function FromThis(schema, references) {
299
306
  if (recursiveDepth++ > recursiveMaxDepth)
300
307
  throw new ValueCreateError(schema, 'Cannot create recursive type as it appears possibly infinite. Consider using a default.');
301
308
  if (HasPropertyKey(schema, 'default')) {
302
- return schema.default;
309
+ return FromDefault(schema.default);
303
310
  }
304
311
  else {
305
312
  return Visit(Deref(schema, references), references);
@@ -307,7 +314,7 @@ function FromThis(schema, references) {
307
314
  }
308
315
  function FromTuple(schema, references) {
309
316
  if (HasPropertyKey(schema, 'default')) {
310
- return schema.default;
317
+ return FromDefault(schema.default);
311
318
  }
312
319
  if (schema.items === undefined) {
313
320
  return [];
@@ -318,7 +325,7 @@ function FromTuple(schema, references) {
318
325
  }
319
326
  function FromUndefined(schema, references) {
320
327
  if (HasPropertyKey(schema, 'default')) {
321
- return schema.default;
328
+ return FromDefault(schema.default);
322
329
  }
323
330
  else {
324
331
  return undefined;
@@ -326,7 +333,7 @@ function FromUndefined(schema, references) {
326
333
  }
327
334
  function FromUnion(schema, references) {
328
335
  if (HasPropertyKey(schema, 'default')) {
329
- return schema.default;
336
+ return FromDefault(schema.default);
330
337
  }
331
338
  else if (schema.anyOf.length === 0) {
332
339
  throw new Error('ValueCreate.Union: Cannot create Union with zero variants');
@@ -337,7 +344,7 @@ function FromUnion(schema, references) {
337
344
  }
338
345
  function FromUint8Array(schema, references) {
339
346
  if (HasPropertyKey(schema, 'default')) {
340
- return schema.default;
347
+ return FromDefault(schema.default);
341
348
  }
342
349
  else if (schema.minByteLength !== undefined) {
343
350
  return new Uint8Array(schema.minByteLength);
@@ -348,7 +355,7 @@ function FromUint8Array(schema, references) {
348
355
  }
349
356
  function FromUnknown(schema, references) {
350
357
  if (HasPropertyKey(schema, 'default')) {
351
- return schema.default;
358
+ return FromDefault(schema.default);
352
359
  }
353
360
  else {
354
361
  return {};
@@ -356,7 +363,7 @@ function FromUnknown(schema, references) {
356
363
  }
357
364
  function FromVoid(schema, references) {
358
365
  if (HasPropertyKey(schema, 'default')) {
359
- return schema.default;
366
+ return FromDefault(schema.default);
360
367
  }
361
368
  else {
362
369
  return void 0;
@@ -364,7 +371,7 @@ function FromVoid(schema, references) {
364
371
  }
365
372
  function FromKind(schema, references) {
366
373
  if (HasPropertyKey(schema, 'default')) {
367
- return schema.default;
374
+ return FromDefault(schema.default);
368
375
  }
369
376
  else {
370
377
  throw new Error('User defined types must specify a default value');
@@ -4,6 +4,8 @@ export { TypeRegistry, FormatRegistry } from './type/registry/index';
4
4
  export { TypeGuard, ValueGuard } from './type/guard/index';
5
5
  export { CloneType, CloneRest } from './type/clone/type';
6
6
  export { TypeBoxError } from './type/error/index';
7
+ export { SetComplement, SetDistinct, SetIncludes, SetIntersect, SetIntersectMany, SetIsSubset, SetUnion, SetUnionMany, type TSetComplement, type TSetDistinct, type TSetIncludes, type TSetIntersect, type TSetIntersectMany, type TSetIsSubset, type TSetUnion, type TSetUnionMany, } from './type/sets/index';
8
+ export { Increment, type TIncrement, type Assert, type AssertType, type AssertRest, type AssertProperties, type Ensure, type Evaluate, type TupleToIntersect, type TupleToUnion, type UnionToTuple } from './type/helpers/index';
7
9
  export { Any, type TAny } from './type/any/index';
8
10
  export { Array, type TArray, type ArrayOptions } from './type/array/index';
9
11
  export { AsyncIterator, type TAsyncIterator } from './type/async-iterator/index';
@@ -21,7 +23,6 @@ export { Exclude, type TExclude, type TExcludeFromMappedResult } from './type/ex
21
23
  export { Extends, ExtendsCheck, ExtendsResult, ExtendsUndefinedCheck, type TExtends, type ExtendsFromMappedResult, type ExtendsFromMappedKey } from './type/extends/index';
22
24
  export { Extract, type TExtract, type TExtractFromMappedResult } from './type/extract/index';
23
25
  export { Function, type TFunction } from './type/function/index';
24
- export { Increment, type Assert, type AssertType, type AssertRest, type AssertProperties, type Ensure, type Evaluate, type TupleToIntersect, type TupleToUnion, type UnionToTuple } from './type/helpers/index';
25
26
  export { Index, IndexPropertyKeys, IndexFromPropertyKeys, IndexFromPropertyKey, IndexFromMappedKey, IndexFromMappedResult, type TIndex, type TIndexPropertyKeys, type TIndexFromPropertyKeys, type TIndexFromPropertyKey, type TIndexFromMappedKey, type TIndexFromMappedResult, } from './type/indexed/index';
26
27
  export { InstanceType, type TInstanceType } from './type/instance-type/index';
27
28
  export { Integer, type TInteger, type IntegerOptions } from './type/integer/index';
@@ -55,7 +56,7 @@ export { type TSchema, type TKind, type SchemaOptions, type TAnySchema } from '.
55
56
  export { type Static, type StaticDecode, type StaticEncode, type TDecodeType, type TDecodeRest, type TDecodeProperties } from './type/static/index';
56
57
  export { Strict } from './type/strict/index';
57
58
  export { String, type TString, type StringOptions, type StringFormatOption, type StringContentEncodingOption } from './type/string/index';
58
- export { Symbol, type TSymbol, type TSymbolValue as SymbolValue } from './type/symbol/index';
59
+ export { Symbol, type TSymbol, type TSymbolValue } from './type/symbol/index';
59
60
  export { TemplateLiteral, TemplateLiteralSyntax, TemplateLiteralGenerate, TemplateLiteralParse, TemplateLiteralParseExact, IsTemplateLiteralFinite, TemplateLiteralExpressionGenerate, IsTemplateLiteralExpressionFinite, type TTemplateLiteral, type TTemplateLiteralSyntax, type TTemplateLiteralGenerate, type TTemplateLiteralKind, type TIsTemplateLiteralFinite, } from './type/template-literal/index';
60
61
  export { Transform, TransformDecodeBuilder, TransformEncodeBuilder, type TTransform, type TransformOptions, type TransformFunction } from './type/transform/index';
61
62
  export { Tuple, type TTuple } from './type/tuple/index';