@sinclair/typebox 0.28.6 → 0.28.8

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.28.6",
3
+ "version": "0.28.8",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -959,7 +959,7 @@ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
959
959
 
960
960
  <a name='types-guards'></a>
961
961
 
962
- ### Type Gaurds
962
+ ### Type Guards
963
963
 
964
964
  TypeBox provides a `TypeGuard` module that can be used for reflection and asserting values as types.
965
965
 
package/typebox.d.ts CHANGED
@@ -96,20 +96,11 @@ export interface TBoolean extends TSchema {
96
96
  }
97
97
  export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = TTuple<T['parameters']>;
98
98
  export type TInstanceType<T extends TConstructor<TSchema[], TSchema>> = T['returns'];
99
- export type TCompositeIsOptional<T extends TSchema> = T extends TOptional<T> | TReadonlyOptional<T> ? true : false;
100
- export type TCompositeOptional<T extends TSchema[]> = T extends [infer L, ...infer R] ? TCompositeIsOptional<AssertType<L>> extends false ? false : TCompositeOptional<AssertRest<R>> : true;
101
- export type TCompositeKeyOfUnion1<T extends TObject> = keyof T['properties'];
102
- export type TCompositeKeyOfUnion2<T extends TObject[]> = T extends [infer L, ...infer R] ? TCompositeKeyOfUnion1<Assert<L, TObject>> | TCompositeKeyOfUnion2<Assert<R, TObject[]>> : never;
103
- export type TCompositeKeyOf<T extends TObject[]> = UnionToTuple<TCompositeKeyOfUnion2<T>>;
104
- export type TCompositePropertiesWithKey1<T extends TObject, K extends Key> = K extends keyof T['properties'] ? [T['properties'][K]] : [];
105
- export type TCompositePropertiesWithKey2<T extends TObject[], K extends Key> = T extends [infer L, ...infer R] ? [...TCompositePropertiesWithKey1<Assert<L, TObject>, K>, ...TCompositePropertiesWithKey2<Assert<R, TObject[]>, K>] : [];
106
- export type TCompositeObjectProperty<T extends TObject[], K extends Key> = TCompositePropertiesWithKey2<T, K> extends infer S ? TCompositeOptional<AssertRest<S>> extends true ? {
107
- [_ in K]: TOptional<IntersectType<AssertRest<S>>>;
108
- } : {
109
- [_ in K]: IntersectType<AssertRest<S>>;
110
- } : {};
111
- export type TCompositeObjectsWithKeys<T extends TObject[], K extends Key[]> = K extends [infer L, ...infer R] ? L extends Key ? TCompositeObjectProperty<T, L> & TCompositeObjectsWithKeys<T, Assert<R, Key[]>> : {} : {};
112
- export type TComposite<T extends TObject[]> = Ensure<TObject<Evaluate<TCompositeObjectsWithKeys<T, Assert<TCompositeKeyOf<T>, Key[]>>>>>;
99
+ export type TCompositeReduce<T extends TIntersect<TObject[]>, K extends string[]> = K extends [infer L, ...infer R] ? {
100
+ [_ in Assert<L, string>]: TIndexType<T, Assert<L, string>>;
101
+ } & TCompositeReduce<T, Assert<R, string[]>> : {};
102
+ export type TCompositeSelect<T extends TIntersect<TObject[]>> = UnionToTuple<keyof Static<T>> extends infer K ? Evaluate<TCompositeReduce<T, Assert<K, string[]>>> : {};
103
+ export type TComposite<T extends TObject[]> = TIntersect<T> extends infer R ? TObject<TCompositeSelect<Assert<R, TIntersect<TObject[]>>>> : TObject<{}>;
113
104
  export type TConstructorParameterArray<T extends readonly TSchema[], P extends unknown[]> = [...{
114
105
  [K in keyof T]: Static<AssertType<T[K]>, P>;
115
106
  }];
@@ -203,7 +194,7 @@ export interface TIntersect<T extends TSchema[] = TSchema[]> extends TSchema, In
203
194
  allOf: [...T];
204
195
  }
205
196
  export type TKeyOfProperties<T extends TSchema> = Static<T> extends infer S ? UnionToTuple<{
206
- [K in keyof S]: TLiteral<`${Assert<K, TLiteralValue>}`>;
197
+ [K in keyof S]: TLiteral<Assert<K, TLiteralValue>>;
207
198
  }[keyof S]> : [];
208
199
  export type TKeyOfIndicesArray<T extends TSchema[]> = UnionToTuple<keyof T & `${number}`>;
209
200
  export type TKeyOfIndices<T extends TSchema[]> = AssertRest<TKeyOfIndicesArray<T> extends infer R ? {
package/typebox.js CHANGED
@@ -1440,7 +1440,8 @@ var IndexedAccessor;
1440
1440
  return exports.Type.Never();
1441
1441
  }
1442
1442
  function Resolve(schema, keys, options = {}) {
1443
- return exports.Type.Union(keys.map((key) => Visit(schema, key.toString())));
1443
+ // prettier-ignore
1444
+ return exports.Type.Union(keys.map((key) => Visit(schema, key.toString())), options);
1444
1445
  }
1445
1446
  IndexedAccessor.Resolve = Resolve;
1446
1447
  })(IndexedAccessor = exports.IndexedAccessor || (exports.IndexedAccessor = {}));
@@ -1900,44 +1901,10 @@ class StandardTypeBuilder extends TypeBuilder {
1900
1901
  }
1901
1902
  /** `[Standard]` Creates a Composite object type. */
1902
1903
  Composite(objects, options) {
1903
- const isOptionalAll = (objects, key) => objects.every((object) => !(key in object.properties) || IsOptional(object.properties[key]));
1904
- const IsOptional = (schema) => TypeGuard.TOptional(schema) || TypeGuard.TReadonlyOptional(schema);
1905
- const [required, optional] = [new Set(), new Set()];
1906
- for (const object of objects) {
1907
- for (const key of globalThis.Object.getOwnPropertyNames(object.properties)) {
1908
- if (isOptionalAll(objects, key))
1909
- optional.add(key);
1910
- }
1911
- }
1912
- for (const object of objects) {
1913
- for (const key of globalThis.Object.getOwnPropertyNames(object.properties)) {
1914
- if (!optional.has(key))
1915
- required.add(key);
1916
- }
1917
- }
1918
- const properties = {};
1919
- for (const object of objects) {
1920
- for (const [key, schema] of Object.entries(object.properties)) {
1921
- const property = TypeClone.Clone(schema, {});
1922
- if (!optional.has(key))
1923
- delete property[exports.Modifier];
1924
- if (key in properties) {
1925
- properties[key] = TypeGuard.TIntersect(properties[key]) ? this.Intersect([...properties[key].allOf, property]) : this.Intersect([properties[key], property]);
1926
- }
1927
- else {
1928
- properties[key] = property;
1929
- }
1930
- }
1931
- }
1932
- for (const key of globalThis.Object.getOwnPropertyNames(properties)) {
1933
- properties[key] = optional.has(key) ? this.Optional(properties[key]) : properties[key];
1934
- }
1935
- if (required.size > 0) {
1936
- return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties, required: [...required] });
1937
- }
1938
- else {
1939
- return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties });
1940
- }
1904
+ const intersect = exports.Type.Intersect(objects, {});
1905
+ const keys = KeyResolver.ResolveKeys(intersect, { includePatterns: false });
1906
+ const properties = keys.reduce((acc, key) => ({ ...acc, [key]: exports.Type.Index(intersect, [key]) }), {});
1907
+ return exports.Type.Object(properties, options);
1941
1908
  }
1942
1909
  /** `[Standard]` Creates a Enum type */
1943
1910
  Enum(item, options = {}) {