@sinclair/typebox 0.24.18 → 0.24.19
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 +1 -1
- package/readme.md +9 -9
- package/typebox.d.ts +13 -7
- package/typebox.js +7 -5
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -492,28 +492,28 @@ Use `Type.Recursive(...)` to create recursive types.
|
|
|
492
492
|
|
|
493
493
|
```typescript
|
|
494
494
|
const Node = Type.Recursive(Node => Type.Object({ // const Node = {
|
|
495
|
-
id: Type.String(), // $id:
|
|
496
|
-
nodes: Type.Array(Node) // type:
|
|
495
|
+
id: Type.String(), // $id: 'Node',
|
|
496
|
+
nodes: Type.Array(Node) // type: 'object',
|
|
497
497
|
}), { $id: 'Node' }) // properties: {
|
|
498
498
|
// id: {
|
|
499
|
-
//
|
|
499
|
+
// type: 'string'
|
|
500
500
|
// },
|
|
501
501
|
// nodes: {
|
|
502
|
-
// type:
|
|
502
|
+
// type: 'array',
|
|
503
503
|
// items: {
|
|
504
|
-
// $ref:
|
|
504
|
+
// $ref: 'Node'
|
|
505
505
|
// }
|
|
506
506
|
// }
|
|
507
507
|
// },
|
|
508
508
|
// required: [
|
|
509
|
-
//
|
|
510
|
-
//
|
|
509
|
+
// 'id',
|
|
510
|
+
// 'nodes'
|
|
511
511
|
// ]
|
|
512
512
|
// }
|
|
513
513
|
|
|
514
514
|
type Node = Static<typeof Node> // type Node = {
|
|
515
515
|
// id: string
|
|
516
|
-
// nodes:
|
|
516
|
+
// nodes: Node[]
|
|
517
517
|
// }
|
|
518
518
|
|
|
519
519
|
function test(node: Node) {
|
|
@@ -650,7 +650,7 @@ The following table shows the TypeBox mappings between TypeScript and JSON schem
|
|
|
650
650
|
│ Type.Union([ │ 'a' | 'b' | 'c', │ anyOf: [{ │
|
|
651
651
|
│ Type.Literal('a'), │ 'a' │ const: 'b', │
|
|
652
652
|
│ Type.Literal('b'), │ > │ type: 'string' │
|
|
653
|
-
│ Type.Literal('c')
|
|
653
|
+
│ Type.Literal('c') │ │ }, { │
|
|
654
654
|
│ ]), │ │ const: 'c', │
|
|
655
655
|
│ Type.Union([ │ │ type: 'string' │
|
|
656
656
|
│ Type.Literal('a') │ │ }] │
|
package/typebox.d.ts
CHANGED
|
@@ -107,9 +107,12 @@ export interface TIntersect<T extends TObject[] = TObject[]> extends TObject {
|
|
|
107
107
|
static: IntersectReduce<unknown, IntersectEvaluate<T, this['params']>>;
|
|
108
108
|
properties: Record<keyof IntersectReduce<unknown, IntersectEvaluate<T, this['params']>>, TSchema>;
|
|
109
109
|
}
|
|
110
|
-
declare type UnionToIntersect<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never;
|
|
111
|
-
declare type UnionLast<U> = UnionToIntersect<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never;
|
|
112
|
-
declare type UnionToTuple<U, L = UnionLast<U>> = [U] extends [never] ? [] : [...UnionToTuple<Exclude<U, L>>, L];
|
|
110
|
+
export declare type UnionToIntersect<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never;
|
|
111
|
+
export declare type UnionLast<U> = UnionToIntersect<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never;
|
|
112
|
+
export declare type UnionToTuple<U, L = UnionLast<U>> = [U] extends [never] ? [] : [...UnionToTuple<Exclude<U, L>>, L];
|
|
113
|
+
export declare type UnionStringLiteralToTuple<T> = T extends TUnion<infer L> ? {
|
|
114
|
+
[I in keyof L]: L[I] extends TLiteral<infer C> ? C : never;
|
|
115
|
+
} : never;
|
|
113
116
|
export declare type TKeyOf<T extends TObject> = {
|
|
114
117
|
[K in ObjectPropertyKeys<T>]: TLiteral<K>;
|
|
115
118
|
} extends infer R ? UnionToTuple<R[keyof R]> : never;
|
|
@@ -324,13 +327,17 @@ export declare class TypeBuilder {
|
|
|
324
327
|
/** Creates an object type with the given properties */
|
|
325
328
|
Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>;
|
|
326
329
|
/** Creates a new object whose properties are omitted from the given object */
|
|
327
|
-
Omit<T extends TObject,
|
|
330
|
+
Omit<T extends TObject, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: ObjectOptions): TOmit<T, UnionStringLiteralToTuple<K>>;
|
|
331
|
+
/** Creates a new object whose properties are omitted from the given object */
|
|
332
|
+
Omit<T extends TObject, K extends ObjectPropertyKeys<T>[]>(schema: T, keys: [...K], options?: ObjectOptions): TOmit<T, K>;
|
|
328
333
|
/** Creates a tuple type from this functions parameters */
|
|
329
334
|
Parameters<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TParameters<T>;
|
|
330
335
|
/** Creates an object type whose properties are all optional */
|
|
331
336
|
Partial<T extends TObject>(schema: T, options?: ObjectOptions): TPartial<T>;
|
|
332
|
-
/** Creates a
|
|
333
|
-
Pick<T extends TObject,
|
|
337
|
+
/** Creates a object whose properties are picked from the given object */
|
|
338
|
+
Pick<T extends TObject, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: ObjectOptions): TPick<T, UnionStringLiteralToTuple<K>>;
|
|
339
|
+
/** Creates a object whose properties are picked from the given object */
|
|
340
|
+
Pick<T extends TObject, K extends ObjectPropertyKeys<T>[]>(schema: T, keys: [...K], options?: ObjectOptions): TPick<T, K>;
|
|
334
341
|
/** Creates a promise type. This type cannot be represented in schema. */
|
|
335
342
|
Promise<T extends TSchema>(item: T, options?: SchemaOptions): TPromise<T>;
|
|
336
343
|
/** Creates an object whose properties are derived from the given string literal union. */
|
|
@@ -372,4 +379,3 @@ export declare class TypeBuilder {
|
|
|
372
379
|
}
|
|
373
380
|
/** JSON Schema Type Builder with Static Type Resolution for TypeScript */
|
|
374
381
|
export declare const Type: TypeBuilder;
|
|
375
|
-
export {};
|
package/typebox.js
CHANGED
|
@@ -179,10 +179,11 @@ class TypeBuilder {
|
|
|
179
179
|
}
|
|
180
180
|
/** Creates a new object whose properties are omitted from the given object */
|
|
181
181
|
Omit(schema, keys, options = {}) {
|
|
182
|
+
const select = keys[exports.Kind] === 'Union' ? keys.anyOf.map((schema) => schema.const) : keys;
|
|
182
183
|
const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Omit' };
|
|
183
|
-
next.required = next.required ? next.required.filter((key) => !
|
|
184
|
+
next.required = next.required ? next.required.filter((key) => !select.includes(key)) : undefined;
|
|
184
185
|
for (const key of Object.keys(next.properties)) {
|
|
185
|
-
if (
|
|
186
|
+
if (select.includes(key))
|
|
186
187
|
delete next.properties[key];
|
|
187
188
|
}
|
|
188
189
|
return this.Create(next);
|
|
@@ -215,12 +216,13 @@ class TypeBuilder {
|
|
|
215
216
|
}
|
|
216
217
|
return this.Create(next);
|
|
217
218
|
}
|
|
218
|
-
/** Creates a
|
|
219
|
+
/** Creates a object whose properties are picked from the given object */
|
|
219
220
|
Pick(schema, keys, options = {}) {
|
|
221
|
+
const select = keys[exports.Kind] === 'Union' ? keys.anyOf.map((schema) => schema.const) : keys;
|
|
220
222
|
const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Pick' };
|
|
221
|
-
next.required = next.required ? next.required.filter((key) =>
|
|
223
|
+
next.required = next.required ? next.required.filter((key) => select.includes(key)) : undefined;
|
|
222
224
|
for (const key of Object.keys(next.properties)) {
|
|
223
|
-
if (!
|
|
225
|
+
if (!select.includes(key))
|
|
224
226
|
delete next.properties[key];
|
|
225
227
|
}
|
|
226
228
|
return this.Create(next);
|