From 1c090f699b03f89a9b702d43c770bcabe4b73244 Mon Sep 17 00:00:00 2001 From: sinclair Date: Tue, 28 Nov 2023 19:38:01 +0900 Subject: [PATCH] Esm --- examples/collections/array.ts | 2 +- examples/collections/map.ts | 2 +- examples/collections/set.ts | 2 +- examples/index.ts | 43 ++++++++-- examples/prototypes/const.ts | 61 ------------- examples/prototypes/evaluate.ts | 3 +- examples/prototypes/index.ts | 1 - examples/prototypes/partial-deep.ts | 9 +- examples/prototypes/union-enum.ts | 6 +- examples/prototypes/union-oneof.ts | 6 +- examples/typedef/typedef.ts | 129 +++++++++++++--------------- src/index.ts | 25 +++--- src/type/record/record.ts | 6 +- todo.md | 3 +- 14 files changed, 128 insertions(+), 170 deletions(-) delete mode 100644 examples/prototypes/const.ts diff --git a/examples/collections/array.ts b/examples/collections/array.ts index 4b18f953e..1a7a7550b 100644 --- a/examples/collections/array.ts +++ b/examples/collections/array.ts @@ -27,7 +27,7 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ import { TypeCheck, TypeCompiler, ValueError } from '@sinclair/typebox/compiler' -import { TSchema, Static, TypeBoxError } from '@sinclair/typebox' +import { TSchema, Static } from '@sinclair/typebox' import { Value } from '@sinclair/typebox/value' // ---------------------------------------------------------------- diff --git a/examples/collections/map.ts b/examples/collections/map.ts index 1ff9e5eb4..2fa3dffed 100644 --- a/examples/collections/map.ts +++ b/examples/collections/map.ts @@ -27,7 +27,7 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ import { TypeCheck, TypeCompiler, ValueError } from '@sinclair/typebox/compiler' -import { TSchema, Static, TypeBoxError } from '@sinclair/typebox' +import { TSchema, Static } from '@sinclair/typebox' import { Value } from '@sinclair/typebox/value' // ---------------------------------------------------------------- diff --git a/examples/collections/set.ts b/examples/collections/set.ts index 51e6f8010..4e8d31213 100644 --- a/examples/collections/set.ts +++ b/examples/collections/set.ts @@ -27,7 +27,7 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ import { TypeCheck, TypeCompiler, ValueError } from '@sinclair/typebox/compiler' -import { TSchema, Static, TypeBoxError } from '@sinclair/typebox' +import { TSchema, Static } from '@sinclair/typebox' import { Value } from '@sinclair/typebox/value' // ---------------------------------------------------------------- diff --git a/examples/index.ts b/examples/index.ts index f8c2929b9..15226e4ec 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -1,11 +1,40 @@ -import { Static, String, Object, Number } from 'typebox' +import { TypeSystem } from '@sinclair/typebox/system' +import { TypeCompiler } from '@sinclair/typebox/compiler' +import { Value, ValuePointer } from '@sinclair/typebox/value' +import { Type, TypeGuard, Symbols, Static, TSchema } from '@sinclair/typebox' -const A = Object({ - x: Number(), - y: Number(), - z: String(), +// ----------------------------------------------------------- +// Create: Type +// ----------------------------------------------------------- + +const T = Type.Object({ + x: Type.Number(), + y: Type.Number(), + z: Type.Number(), }) -type A = Static +type T = Static + +console.log(T) + +// ----------------------------------------------------------- +// Create: Value +// ----------------------------------------------------------- + +const V = Value.Create(T) + +console.log(V) + +// ----------------------------------------------------------- +// Compile: Type +// ----------------------------------------------------------- + +const C = TypeCompiler.Compile(T) + +console.log(C.Code()) + +// ----------------------------------------------------------- +// Check: Value +// ----------------------------------------------------------- -console.log(A) +console.log(C.Check(V)) diff --git a/examples/prototypes/const.ts b/examples/prototypes/const.ts deleted file mode 100644 index fa6765a16..000000000 --- a/examples/prototypes/const.ts +++ /dev/null @@ -1,61 +0,0 @@ -/*-------------------------------------------------------------------------- - -@sinclair/typebox/prototypes - -The MIT License (MIT) - -Copyright (c) 2017-2023 Haydn Paterson (sinclair) - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - ----------------------------------------------------------------------------*/ - -// import { Type, ObjectMap, TypeGuard, TSchema, TIntersect, TUnion, TObject, TProperties, TReadonly, AssertProperties, AssertType, AssertRest, Evaluate, ModifiersRemoveReadonly } from '@sinclair/typebox' - -// // ------------------------------------------------------------------------------------- -// // TConst -// // ------------------------------------------------------------------------------------- -// // prettier-ignore -// export type TConstArray = T extends [infer L, ...infer R] -// ? [TConst, E>, ...TConstArray, E>] -// : [] -// // prettier-ignore -// export type TConstProperties = Evaluate, E> -// }>> -// // prettier-ignore -// export type TConst = -// T extends TIntersect ? TIntersect> : -// T extends TUnion ? TUnion> : -// T extends TObject ? TObject> : -// E extends true ? T : TReadonly -// // ------------------------------------------------------------------------------------- -// // Const -// // ------------------------------------------------------------------------------------- -// /** `[Experimental]` Assigns readonly to all interior properties */ -// export function Const(schema: T): TConst { -// const mappable = (TypeGuard.TIntersect(schema) || TypeGuard.TUnion(schema) || TypeGuard.TObject(schema)) -// // prettier-ignore -// return mappable ? ObjectMap.Map(schema, (object) => { -// const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => { -// return { ...acc, [key]: Type.Readonly(object.properties[key] )} -// }, {} as TProperties) -// return Type.Object(properties, {...object}) -// }, {}) : schema as any -// } \ No newline at end of file diff --git a/examples/prototypes/evaluate.ts b/examples/prototypes/evaluate.ts index 8c28b9a30..5260def0f 100644 --- a/examples/prototypes/evaluate.ts +++ b/examples/prototypes/evaluate.ts @@ -44,7 +44,6 @@ import { TTuple, TProperties, TIntersect, - IntersectType, TUnion, TNever } from '@sinclair/typebox' @@ -75,7 +74,7 @@ export type TEvaluateArray = T extends [infer L, ...infer [] // prettier-ignore export type TEvaluate = - T extends TIntersect ? IntersectType.Resolve> : + T extends TIntersect ? TIntersect> : T extends TUnion ? TUnion> : T extends TConstructor ? TConstructor, TEvaluate> : T extends TFunction ? TFunction, TEvaluate> : diff --git a/examples/prototypes/index.ts b/examples/prototypes/index.ts index ef44e7d95..4a70931c8 100644 --- a/examples/prototypes/index.ts +++ b/examples/prototypes/index.ts @@ -26,7 +26,6 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ -export * from './const' export * from './evaluate' export * from './partial-deep' export * from './union-enum' diff --git a/examples/prototypes/partial-deep.ts b/examples/prototypes/partial-deep.ts index 1315b65b0..aa27a07aa 100644 --- a/examples/prototypes/partial-deep.ts +++ b/examples/prototypes/partial-deep.ts @@ -26,7 +26,7 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ -import { TypeGuard, Type, TSchema, TIntersect, TUnion, TObject, TPartial, TProperties, AssertRest, AssertType, Evaluate } from '@sinclair/typebox' +import { TypeGuard, Type, TSchema, TIntersect, TUnion, TObject, TPartial, TProperties, Evaluate } from '@sinclair/typebox' // ------------------------------------------------------------------------------------- // TDeepPartial @@ -34,9 +34,10 @@ import { TypeGuard, Type, TSchema, TIntersect, TUnion, TObject, TPartial, TPrope export type TPartialDeepProperties = { [K in keyof T]: TPartial } -export type TPartialDeepRest = T extends [infer L, ...infer R] - ? [TPartial>, ...TPartialDeepRest>] - : [] +export type TPartialDeepRest = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? [TPartial, ...TPartialDeepRest] + : [] export type TPartialDeep = T extends TIntersect ? TIntersect> : T extends TUnion ? TUnion> : diff --git a/examples/prototypes/union-enum.ts b/examples/prototypes/union-enum.ts index ee55d202c..c4b1d7a05 100644 --- a/examples/prototypes/union-enum.ts +++ b/examples/prototypes/union-enum.ts @@ -26,13 +26,13 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ -import { TypeRegistry, Kind, TSchema, SchemaOptions } from '@sinclair/typebox' +import { TypeRegistry, Symbols, TSchema, SchemaOptions } from '@sinclair/typebox' // ------------------------------------------------------------------------------------- // TUnionEnum // ------------------------------------------------------------------------------------- export interface TUnionEnum extends TSchema { - [Kind]: 'UnionEnum' + [Symbols.Kind]: 'UnionEnum' static: T[number] enum: T } @@ -45,5 +45,5 @@ export function UnionEnum(values: [...T], options return (typeof value === 'string' || typeof value === 'number') && schema.enum.includes(value) } if (!TypeRegistry.Has('UnionEnum')) TypeRegistry.Set('UnionEnum', UnionEnumCheck) - return { ...options, [Kind]: 'UnionEnum', enum: values } as TUnionEnum + return { ...options, [Symbols.Kind]: 'UnionEnum', enum: values } as TUnionEnum } \ No newline at end of file diff --git a/examples/prototypes/union-oneof.ts b/examples/prototypes/union-oneof.ts index 5129d457a..22559d028 100644 --- a/examples/prototypes/union-oneof.ts +++ b/examples/prototypes/union-oneof.ts @@ -26,14 +26,14 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ -import { TypeRegistry, Kind, Static, TSchema, SchemaOptions } from '@sinclair/typebox' +import { TypeRegistry, Symbols, Static, TSchema, SchemaOptions } from '@sinclair/typebox' import { Value } from '@sinclair/typebox/value' // ------------------------------------------------------------------------------------- // TUnionOneOf // ------------------------------------------------------------------------------------- export interface TUnionOneOf extends TSchema { - [Kind]: 'UnionOneOf' + [Symbols.Kind]: 'UnionOneOf' static: { [K in keyof T]: Static }[number] oneOf: T } @@ -46,5 +46,5 @@ export function UnionOneOf(oneOf: [...T], options: SchemaOp return 1 === schema.oneOf.reduce((acc: number, schema: any) => (Value.Check(schema, value) ? acc + 1 : acc), 0) } if (!TypeRegistry.Has('UnionOneOf')) TypeRegistry.Set('UnionOneOf', UnionOneOfCheck) - return { ...options, [Kind]: 'UnionOneOf', oneOf } as TUnionOneOf + return { ...options, [Symbols.Kind]: 'UnionOneOf', oneOf } as TUnionOneOf } \ No newline at end of file diff --git a/examples/typedef/typedef.ts b/examples/typedef/typedef.ts index ae369e2b7..2bf44754d 100644 --- a/examples/typedef/typedef.ts +++ b/examples/typedef/typedef.ts @@ -27,21 +27,10 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ import { TypeSystemErrorFunction, DefaultErrorFunction } from '@sinclair/typebox/system' -import * as Types from '@sinclair/typebox' +import * as Types from '@sinclair/typebox/type' // -------------------------------------------------------------------------- -// Utility Types -// -------------------------------------------------------------------------- -export type Assert = T extends U ? T : never -export type Base = { m: string, t: string } -export type Base16 = { m: 'F', t: '01', '0': '1', '1': '2', '2': '3', '3': '4', '4': '5', '5': '6', '6': '7', '7': '8', '8': '9', '9': 'A', 'A': 'B', 'B': 'C', 'C': 'D', 'D': 'E', 'E': 'F', 'F': '0' } -export type Base10 = { m: '9', t: '01', '0': '1', '1': '2', '2': '3', '3': '4', '4': '5', '5': '6', '6': '7', '7': '8', '8': '9', '9': '0' } -export type Reverse = T extends `${infer L}${infer R}` ? `${Reverse}${L}` : T -export type Tick = T extends keyof B ? B[T] : never -export type Next = T extends Assert['m'] ? Assert['t'] : T extends `${infer L}${infer R}` ? L extends Assert['m'] ? `${Assert, string>}${Next}` : `${Assert, string>}${R}` : never -export type Increment = Reverse, B>> -// -------------------------------------------------------------------------- -// SchemaOptions +// Metadata // -------------------------------------------------------------------------- export interface Metadata { [name: string]: any @@ -50,7 +39,7 @@ export interface Metadata { // TArray // -------------------------------------------------------------------------- export interface TArray extends Types.TSchema { - [Types.Kind]: 'TypeDef:Array' + [Types.Symbols.Kind]: 'TypeDef:Array' static: Types.Static[] elements: T } @@ -58,20 +47,20 @@ export interface TArray extends Types.T // TBoolean // -------------------------------------------------------------------------- export interface TBoolean extends Types.TSchema { - [Types.Kind]: 'TypeDef:Boolean' + [Types.Symbols.Kind]: 'TypeDef:Boolean' static: 'boolean' type: 'boolean' } // -------------------------------------------------------------------------- // TUnion // -------------------------------------------------------------------------- -// type InferUnion = T extends [infer L, ...infer R] -// ? Types.Evaluate<{ [_ in D]: Index } & Types.Static>> | InferUnion, D, Increment>> -// : never +export type InferUnion = T extends [infer L, ...infer R] + ? Types.Evaluate<{ [_ in D]: Index } & Types.Static>> | InferUnion, D, Types.Increment>> + : never export interface TUnion extends Types.TSchema { - [Types.Kind]: 'TypeDef:Union' - static: unknown //InferUnion + [Types.Symbols.Kind]: 'TypeDef:Union' + static: InferUnion discriminator: D, mapping: T } @@ -79,7 +68,7 @@ export interface TUnion extends Types.TSchema { - [Types.Kind]: 'TypeDef:Enum' + [Types.Symbols.Kind]: 'TypeDef:Enum' static: T[number] enum: [...T] } @@ -87,7 +76,7 @@ export interface TEnum extends Types.TSchema { // TFloat32 // -------------------------------------------------------------------------- export interface TFloat32 extends Types.TSchema { - [Types.Kind]: 'TypeDef:Float32' + [Types.Symbols.Kind]: 'TypeDef:Float32' type: 'float32' static: number } @@ -95,7 +84,7 @@ export interface TFloat32 extends Types.TSchema { // TFloat64 // -------------------------------------------------------------------------- export interface TFloat64 extends Types.TSchema { - [Types.Kind]: 'TypeDef:Float64' + [Types.Symbols.Kind]: 'TypeDef:Float64' type: 'float64' static: number } @@ -103,7 +92,7 @@ export interface TFloat64 extends Types.TSchema { // TInt8 // -------------------------------------------------------------------------- export interface TInt8 extends Types.TSchema { - [Types.Kind]: 'TypeDef:Int8' + [Types.Symbols.Kind]: 'TypeDef:Int8' type: 'int8' static: number } @@ -111,7 +100,7 @@ export interface TInt8 extends Types.TSchema { // TInt16 // -------------------------------------------------------------------------- export interface TInt16 extends Types.TSchema { - [Types.Kind]: 'TypeDef:Int16' + [Types.Symbols.Kind]: 'TypeDef:Int16' type: 'int16' static: number } @@ -119,7 +108,7 @@ export interface TInt16 extends Types.TSchema { // TInt32 // -------------------------------------------------------------------------- export interface TInt32 extends Types.TSchema { - [Types.Kind]: 'TypeDef:Int32' + [Types.Symbols.Kind]: 'TypeDef:Int32' type: 'int32' static: number } @@ -127,7 +116,7 @@ export interface TInt32 extends Types.TSchema { // TUint8 // -------------------------------------------------------------------------- export interface TUint8 extends Types.TSchema { - [Types.Kind]: 'TypeDef:Uint8' + [Types.Symbols.Kind]: 'TypeDef:Uint8' type: 'uint8' static: number } @@ -135,7 +124,7 @@ export interface TUint8 extends Types.TSchema { // TUint16 // -------------------------------------------------------------------------- export interface TUint16 extends Types.TSchema { - [Types.Kind]: 'TypeDef:Uint16' + [Types.Symbols.Kind]: 'TypeDef:Uint16' type: 'uint16' static: number } @@ -143,7 +132,7 @@ export interface TUint16 extends Types.TSchema { // TUint32 // -------------------------------------------------------------------------- export interface TUint32 extends Types.TSchema { - [Types.Kind]: 'TypeDef:Uint32' + [Types.Symbols.Kind]: 'TypeDef:Uint32' type: 'uint32' static: number } @@ -155,7 +144,7 @@ export type TFields = Record // TRecord // -------------------------------------------------------------------------- export interface TRecord extends Types.TSchema { - [Types.Kind]: 'TypeDef:Record' + [Types.Symbols.Kind]: 'TypeDef:Record' static: Record> values: T } @@ -163,7 +152,7 @@ export interface TRecord extends Types. // TString // -------------------------------------------------------------------------- export interface TString extends Types.TSchema { - [Types.Kind]: 'TypeDef:String' + [Types.Symbols.Kind]: 'TypeDef:String' type: 'string' static: string } @@ -176,8 +165,8 @@ export interface StructMetadata extends Metadata { additionalProperties?: boolean } export interface TStruct extends Types.TSchema, StructMetadata { - [Types.Kind]: 'TypeDef:Struct' - static: Types.PropertiesReduce + [Types.Symbols.Kind]: 'TypeDef:Struct' + static: Types.ObjectResolve optionalProperties: { [K in Types.Assert, keyof T>]: T[K] } properties: { [K in Types.Assert, keyof T>]: T[K] } } @@ -185,7 +174,7 @@ export interface TStruct extends Types.TSchema, Str // TTimestamp // -------------------------------------------------------------------------- export interface TTimestamp extends Types.TSchema { - [Types.Kind]: 'TypeDef:Timestamp' + [Types.Symbols.Kind]: 'TypeDef:Timestamp' type: 'timestamp' static: string } @@ -339,7 +328,7 @@ export namespace ValueCheck { } function Visit(schema: Types.TSchema, value: unknown): boolean { const anySchema = schema as any - switch (anySchema[Types.Kind]) { + switch (anySchema[Types.Symbols.Kind]) { case 'TypeDef:Array': return Array(anySchema, value) case 'TypeDef:Boolean': return Boolean(anySchema, value) case 'TypeDef:Union': return Union(anySchema, value) @@ -389,50 +378,50 @@ export namespace TypeGuard { // Types // ------------------------------------------------------------------------ export function TArray(schema: unknown): schema is TArray { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Array' && TSchema(schema['elements']) + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Array' && TSchema(schema['elements']) } export function TBoolean(schema: unknown): schema is TBoolean { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Boolean' && schema['type'] === 'boolean' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Boolean' && schema['type'] === 'boolean' } export function TUnion(schema: unknown): schema is TUnion { - if(!(IsObject(schema) && schema[Types.Kind] === 'TypeDef:Union' && IsString(schema['discriminator']) && IsObject(schema['mapping']))) return false + if(!(IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Union' && IsString(schema['discriminator']) && IsObject(schema['mapping']))) return false return globalThis.Object.getOwnPropertyNames(schema['mapping']).every(key => TSchema((schema['mapping'] as any)[key])) } export function TEnum(schema: unknown): schema is TEnum { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Enum' && IsArray(schema['enum']) && schema['enum'].every(item => IsString(item)) + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Enum' && IsArray(schema['enum']) && schema['enum'].every(item => IsString(item)) } export function TFloat32(schema: unknown): schema is TFloat32 { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Float32' && schema['type'] === 'float32' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Float32' && schema['type'] === 'float32' } export function TFloat64(schema: unknown): schema is TFloat64 { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Float64' && schema['type'] === 'float64' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Float64' && schema['type'] === 'float64' } export function TInt8(schema: unknown): schema is TInt8 { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Int8' && schema['type'] === 'int8' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Int8' && schema['type'] === 'int8' } export function TInt16(schema: unknown): schema is TInt16 { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Int16' && schema['type'] === 'int16' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Int16' && schema['type'] === 'int16' } export function TInt32(schema: unknown): schema is TInt32 { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Int32' && schema['type'] === 'int32' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Int32' && schema['type'] === 'int32' } export function TUint8(schema: unknown): schema is TUint8 { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Uint8' && schema['type'] === 'uint8' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Uint8' && schema['type'] === 'uint8' } export function TUint16(schema: unknown): schema is TUint16 { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Uint16' && schema['type'] === 'uint16' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Uint16' && schema['type'] === 'uint16' } export function TUint32(schema: unknown): schema is TUint32 { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Uint32' && schema['type'] === 'uint32' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Uint32' && schema['type'] === 'uint32' } export function TRecord(schema: unknown): schema is TRecord { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Record' && TSchema(schema['values']) + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Record' && TSchema(schema['values']) } export function TString(schema: unknown): schema is TString { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:String' && schema['type'] === 'string' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:String' && schema['type'] === 'string' } export function TStruct(schema: unknown): schema is TStruct { - if(!(IsObject(schema) && schema[Types.Kind] === 'TypeDef:Struct' && IsOptionalBoolean(schema['additionalProperties']))) return false + if(!(IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Struct' && IsOptionalBoolean(schema['additionalProperties']))) return false const optionalProperties = schema['optionalProperties'] const requiredProperties = schema['properties'] const optionalCheck = optionalProperties === undefined || IsObject(optionalProperties) && globalThis.Object.getOwnPropertyNames(optionalProperties).every(key => TSchema(optionalProperties[key])) @@ -440,10 +429,10 @@ export namespace TypeGuard { return optionalCheck && requiredCheck } export function TTimestamp(schema: unknown): schema is TTimestamp { - return IsObject(schema) && schema[Types.Kind] === 'TypeDef:Timestamp' && schema['type'] === 'timestamp' + return IsObject(schema) && schema[Types.Symbols.Kind] === 'TypeDef:Timestamp' && schema['type'] === 'timestamp' } export function TKind(schema: unknown): schema is Types.TKind { - return IsObject(schema) && Types.Kind in schema && typeof (schema as any)[Types.Kind] === 'string' // TS 4.1.5: any required for symbol indexer + return IsObject(schema) && Types.Symbols.Kind in schema && typeof (schema as any)[Types.Symbols.Kind] === 'string' // TS 4.1.5: any required for symbol indexer } export function TSchema(schema: unknown): schema is Types.TSchema { // prettier-ignore @@ -464,7 +453,7 @@ export namespace TypeGuard { TString(schema) || TStruct(schema) || TTimestamp(schema) || - (TKind(schema) && Types.TypeRegistry.Has(schema[Types.Kind])) + (TKind(schema) && Types.TypeRegistry.Has(schema[Types.Symbols.Kind])) ) } } @@ -488,7 +477,7 @@ Types.TypeRegistry.Set('TypeDef:Timestamp', (schema, value) => Value // TypeSystemErrorFunction // -------------------------------------------------------------------------- TypeSystemErrorFunction.Set((schema, type) => { - switch(schema[Types.Kind]) { + switch(schema[Types.Symbols.Kind]) { case 'TypeDef:Array': return 'Expected Array' case 'TypeDef:Boolean': return 'Expected Boolean' case 'TypeDef:Union': return 'Expected Union' @@ -536,55 +525,55 @@ export class TypeDefBuilder { // ------------------------------------------------------------------------ /** [Standard] Creates a Array type */ public Array(elements: T, metadata: Metadata = {}): TArray { - return this.Create({ [Types.Kind]: 'TypeDef:Array', elements }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Array', elements }, metadata) } /** [Standard] Creates a Boolean type */ public Boolean(metadata: Metadata = {}): TBoolean { - return this.Create({ [Types.Kind]: 'TypeDef:Boolean', type: 'boolean' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Boolean', type: 'boolean' }, metadata) } /** [Standard] Creates a Enum type */ public Enum(values: [...T], metadata: Metadata = {}): TEnum { - return this.Create({[Types.Kind]: 'TypeDef:Enum', enum: values }, metadata ) + return this.Create({[Types.Symbols.Kind]: 'TypeDef:Enum', enum: values }, metadata ) } /** [Standard] Creates a Float32 type */ public Float32(metadata: Metadata = {}): TFloat32 { - return this.Create({ [Types.Kind]: 'TypeDef:Float32', type: 'float32' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Float32', type: 'float32' }, metadata) } /** [Standard] Creates a Float64 type */ public Float64(metadata: Metadata = {}): TFloat64 { - return this.Create({ [Types.Kind]: 'TypeDef:Float64', type: 'float64' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Float64', type: 'float64' }, metadata) } /** [Standard] Creates a Int8 type */ public Int8(metadata: Metadata = {}): TInt8 { - return this.Create({ [Types.Kind]: 'TypeDef:Int8', type: 'int8' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Int8', type: 'int8' }, metadata) } /** [Standard] Creates a Int16 type */ public Int16(metadata: Metadata = {}): TInt16 { - return this.Create({ [Types.Kind]: 'TypeDef:Int16', type: 'int16' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Int16', type: 'int16' }, metadata) } /** [Standard] Creates a Int32 type */ public Int32(metadata: Metadata = {}): TInt32 { - return this.Create({ [Types.Kind]: 'TypeDef:Int32', type: 'int32' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Int32', type: 'int32' }, metadata) } /** [Standard] Creates a Uint8 type */ public Uint8(metadata: Metadata = {}): TUint8 { - return this.Create({ [Types.Kind]: 'TypeDef:Uint8', type: 'uint8' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Uint8', type: 'uint8' }, metadata) } /** [Standard] Creates a Uint16 type */ public Uint16(metadata: Metadata = {}): TUint16 { - return this.Create({ [Types.Kind]: 'TypeDef:Uint16', type: 'uint16' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Uint16', type: 'uint16' }, metadata) } /** [Standard] Creates a Uint32 type */ public Uint32(metadata: Metadata = {}): TUint32 { - return this.Create({ [Types.Kind]: 'TypeDef:Uint32', type: 'uint32' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Uint32', type: 'uint32' }, metadata) } /** [Standard] Creates a Record type */ public Record(values: T, metadata: Metadata = {}): TRecord { - return this.Create({ [Types.Kind]: 'TypeDef:Record', values },metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Record', values },metadata) } /** [Standard] Creates a String type */ public String(metadata: Metadata = {}): TString { - return this.Create({ [Types.Kind]: 'TypeDef:String', type: 'string' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:String', type: 'string' }, metadata) } /** [Standard] Creates a Struct type */ public Struct(fields: T, metadata: StructMetadata = {}): TStruct { @@ -592,18 +581,18 @@ export class TypeDefBuilder { const properties = globalThis.Object.getOwnPropertyNames(fields).reduce((acc, key) => (Types.TypeGuard.TOptional(fields[key]) ? { ...acc } : { ...acc, [key]: fields[key] }), {} as TFields) const optionalObject = globalThis.Object.getOwnPropertyNames(optionalProperties).length > 0 ? { optionalProperties: optionalProperties } : {} const requiredObject = globalThis.Object.getOwnPropertyNames(properties).length === 0 ? {} : { properties: properties } - return this.Create({ [Types.Kind]: 'TypeDef:Struct', ...requiredObject, ...optionalObject }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Struct', ...requiredObject, ...optionalObject }, metadata) } /** [Standard] Creates a Union type */ public Union[], D extends string = 'type'>(structs: [...T], discriminator?: D): TUnion { discriminator = (discriminator || 'type') as D if (structs.length === 0) throw new Error('TypeDefBuilder: Union types must contain at least one struct') const mapping = structs.reduce((acc, current, index) => ({ ...acc, [index.toString()]: current }), {}) - return this.Create({ [Types.Kind]: 'TypeDef:Union', discriminator, mapping }, {}) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Union', discriminator, mapping }, {}) } /** [Standard] Creates a Timestamp type */ public Timestamp(metadata: Metadata = {}): TTimestamp { - return this.Create({ [Types.Kind]: 'TypeDef:Timestamp', type: 'timestamp' }, metadata) + return this.Create({ [Types.Symbols.Kind]: 'TypeDef:Timestamp', type: 'timestamp' }, metadata) } } diff --git a/src/index.ts b/src/index.ts index 70c8dad46..693ebaacd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,25 +30,26 @@ THE SOFTWARE. // Types // ------------------------------------------------------------------ export { type TAny, Any } from './type/any/index' -export { type TArray, Array } from './type/array/index' +export { type TArray, type ArrayOptions, Array } from './type/array/index' export { type TAsyncIterator, AsyncIterator } from './type/async-iterator/index' export { type TAwaited, Awaited } from './type/awaited/index' -export { type TBigInt, BigInt } from './type/bigint/index' +export { type TBigInt, BigIntOptions, BigInt } from './type/bigint/index' export { type TBoolean, Boolean } from './type/boolean/index' export { type TCapitalize, Capitalize } from './type/intrinsic/index' export { type TComposite, Composite } from './type/composite/index' export { type TConstructor, Constructor } from './type/constructor/index' export { type TConstructorParameters, ConstructorParameters } from './type/constructor-parameters/index' -export { type TDate, Date } from './type/date/index' +export { type TDate, type DateOptions, Date } from './type/date/index' export { type TEnum, Enum } from './type/enum/index' export { type TExclude, Exclude } from './type/exclude/index' export { type TExtends, Extends } from './type/extends/index' export { type TExtract, Extract } from './type/extract/index' export { type TFunction, Function } from './type/function/index' +export { type Assert, type AssertType, type AssertRest, type AssertProperties, type Ensure, type Evaluate } from './type/helpers/index' export { type TIndex, Index } from './type/indexed/index' export { type TInstanceType, InstanceType } from './type/instance-type/index' -export { type TInteger, Integer } from './type/integer/index' -export { type TIntersect, Intersect } from './type/intersect/index' +export { type TInteger, type IntegerOptions, Integer } from './type/integer/index' +export { type TIntersect, type IntersectOptions, Intersect } from './type/intersect/index' export { type TIterator, Iterator } from './type/iterator/index' export { type TKeyOf, KeyOf } from './type/keyof/index' export { type TLiteral, Literal } from './type/literal/index' @@ -56,8 +57,8 @@ export { type TLowercase, Lowercase } from './type/intrinsic/index' export { type TNever, Never } from './type/never/index' export { type TNot, Not } from './type/not/index' export { type TNull, Null } from './type/null/index' -export { type TNumber, Number } from './type/number/index' -export { type TObject, Object as Object } from './type/object/index' +export { type TNumber, type NumberOptions, Number } from './type/number/index' +export { type TObject, type TProperties, type ObjectOptions, Object } from './type/object/index' export { type TOmit, Omit } from './type/omit/index' export { type TOptional, Optional } from './type/optional/index' export { type TParameters, Parameters } from './type/parameters/index' @@ -73,14 +74,14 @@ export { type TRegExp, RegExp } from './type/regexp/index' export { type TRequired, Required } from './type/required/index' export { type TRest, Rest } from './type/rest/index' export { type TReturnType, ReturnType } from './type/return-type/index' -export { type TSchema } from './type/schema/index' +export { type TSchema, type SchemaOptions } from './type/schema/index' export { type Static, type StaticDecode, type StaticEncode } from './type/static/index' -export { type TString, String } from './type/string/index' -export { type TSymbol, Symbol } from './type/symbol/index' +export { type TString, type StringOptions, type StringFormatOption, type StringContentEncodingOption, String } from './type/string/index' +export { type TSymbol, type SymbolValue, Symbol } from './type/symbol/index' export { type TTemplateLiteral, type TTemplateLiteralKind, TemplateLiteral } from './type/template-literal/index' -export { type TTransform, Transform } from './type/transform/index' +export { type TTransform, type TransformOptions, type TransformFunction, TransformDecodeBuilder, TransformEncodeBuilder, Transform } from './type/transform/index' export { type TTuple, Tuple } from './type/tuple/index' -export { type TUint8Array, Uint8Array } from './type/uint8array/index' +export { type TUint8Array, type Uint8ArrayOptions, Uint8Array } from './type/uint8array/index' export { type TUncapitalize, Uncapitalize } from './type/intrinsic/index' export { type TUndefined, Undefined } from './type/undefined/index' export { type TUnion, Union } from './type/union/index' diff --git a/src/type/record/record.ts b/src/type/record/record.ts index ae63e9d6e..f8c5267bd 100644 --- a/src/type/record/record.ts +++ b/src/type/record/record.ts @@ -123,7 +123,7 @@ export function FromLiteralKey(K: K, // ------------------------------------------------------------------ // prettier-ignore export type FromStringKey<_ extends TString, T extends TSchema> = ( - TRecord + Ensure> ) // prettier-ignore export function FromStringKey(K: K, T: T): FromStringKey { @@ -135,7 +135,7 @@ export function FromStringKey(K: K, T: T): // ------------------------------------------------------------------ // prettier-ignore export type FromIntegerKey<_ extends TSchema, T extends TSchema> = ( - TRecord + Ensure> ) // prettier-ignore export function FromIntegerKey(_: K, T: T): FromIntegerKey { @@ -146,7 +146,7 @@ export function FromIntegerKey(_: K, T: T // ------------------------------------------------------------------ // prettier-ignore export type FromNumberKey<_ extends TSchema, T extends TSchema> = ( - TRecord + Ensure> ) // prettier-ignore export function FromNumberKey(_: K, T: T): FromIntegerKey { diff --git a/todo.md b/todo.md index ffe9b88a0..15f842e1a 100644 --- a/todo.md +++ b/todo.md @@ -3,4 +3,5 @@ - Investigate Transform Issue: https://github.com/sinclairzx81/typebox/issues/554 - Clean up Composite (Should technically support Intersect and Union) - Exclude, Extract and Extends could use a clean up -- Look for invalid import references \ No newline at end of file +- Look for invalid import references +- The Symbols Change May be a bit too breaking. Move Kind, Hint, and others to the top (again) \ No newline at end of file