Skip to content

Commit

Permalink
Esm
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 27, 2023
1 parent a8e3f83 commit cebdc82
Show file tree
Hide file tree
Showing 35 changed files with 607 additions and 555 deletions.
4 changes: 2 additions & 2 deletions benchmark/compression/module/typebox.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Type } from '@sinclair/typebox'
import { String } from '@sinclair/typebox'

const T = Type.String()
const T = String()
9 changes: 5 additions & 4 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Type } from '@sinclair/typebox'
import Type, { type Static } from '@sinclair/typebox'

const T = Type.TemplateLiteral('${A|B|C}')

const A = Type.Extract(T, Type.Literal('D'))
const Customer = Type.Object({
n: Type.Any(),
x: Type.Never(),
})
8 changes: 4 additions & 4 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export namespace TypeCompiler {
// Guards
// ----------------------------------------------------------------------
function IsAnyOrUnknown(schema: Types.TSchema) {
return schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown'
return schema[Types.Symbols.Kind] === 'Any' || schema[Types.Symbols.Kind] === 'Unknown'
}
// -------------------------------------------------------------------
// Types
Expand Down Expand Up @@ -399,7 +399,7 @@ export namespace TypeCompiler {
function* TKind(schema: Types.TSchema, references: Types.TSchema[], value: string): IterableIterator<string> {
const instance = state.instances.size
state.instances.set(instance, schema)
yield `kind('${schema[Types.Kind]}', ${instance}, ${value})`
yield `kind('${schema[Types.Symbols.Kind]}', ${instance}, ${value})`
}
function* Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: string, useHoisting: boolean = true): IterableIterator<string> {
const references_ = IsString(schema.$id) ? [...references, schema] : references
Expand All @@ -417,7 +417,7 @@ export namespace TypeCompiler {
return yield `${functionName}(${value})`
}
}
switch (schema_[Types.Kind]) {
switch (schema_[Types.Symbols.Kind]) {
case 'Any':
return yield* TAny(schema_, references_, value)
case 'Array':
Expand Down Expand Up @@ -479,7 +479,7 @@ export namespace TypeCompiler {
case 'Void':
return yield* TVoid(schema_, references_, value)
default:
if (!TypeRegistry.Has(schema_[Types.Kind])) throw new TypeCompilerUnknownTypeError(schema)
if (!TypeRegistry.Has(schema_[Types.Symbols.Kind])) throw new TypeCompilerUnknownTypeError(schema)
return yield* TKind(schema_, references_, value)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,13 @@ function* TVoid(schema: Types.TVoid, references: Types.TSchema[], path: string,
if (!TypeSystemPolicy.IsVoidLike(value)) yield Create(ValueErrorType.Void, schema, path, value)
}
function* TKind(schema: Types.TSchema, references: Types.TSchema[], path: string, value: any): IterableIterator<ValueError> {
const check = TypeRegistry.Get(schema[Types.Kind])!
const check = TypeRegistry.Get(schema[Types.Symbols.Kind])!
if (!check(schema, value)) yield Create(ValueErrorType.Kind, schema, path, value)
}
function* Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[], path: string, value: any): IterableIterator<ValueError> {
const references_ = IsDefined<string>(schema.$id) ? [...references, schema] : references
const schema_ = schema as any
switch (schema_[Types.Kind]) {
switch (schema_[Types.Symbols.Kind]) {
case 'Any':
return yield* TAny(schema_, references_, path, value)
case 'Array':
Expand Down Expand Up @@ -536,7 +536,7 @@ function* Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[],
case 'Void':
return yield* TVoid(schema_, references_, path, value)
default:
if (!TypeRegistry.Has(schema_[Types.Kind])) throw new ValueErrorsUnknownTypeError(schema)
if (!TypeRegistry.Has(schema_[Types.Symbols.Kind])) throw new ValueErrorsUnknownTypeError(schema)
return yield* TKind(schema_, references_, path, value)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/system/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export namespace TypeSystem {
export function Type<Type, Options = Record<PropertyKey, unknown>>(kind: string, check: (options: Options, value: unknown) => boolean) {
if (TypeRegistry.Has(kind)) throw new TypeSystemDuplicateTypeKind(kind)
TypeRegistry.Set(kind, check)
return (options: Partial<Options> = {}) => Types.Type.Unsafe<Type>({ ...options, [Types.Kind]: kind })
return (options: Partial<Options> = {}) => Types.Type.Unsafe<Type>({ ...options, [Types.Symbols.Kind]: kind })
}
/** Creates a new string format */
export function Format<F extends string>(format: F, check: (value: string) => boolean): F {
Expand Down Expand Up @@ -253,7 +253,7 @@ export function DefaultErrorFunction(schema: Types.TSchema, errorType: ValueErro
case ValueErrorType.Void:
return 'Expected void'
case ValueErrorType.Kind:
return `Expected kind '${schema[Types.Kind]}'`
return `Expected kind '${schema[Types.Symbols.Kind]}'`
default:
return 'Unknown error type'
}
Expand Down
27 changes: 13 additions & 14 deletions src/type/guard/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import {
TRecord,
TString,
TUnion,
Hint,
Kind,
Symbols,
Optional,
Readonly,
TAdditionalProperties,
Expand Down Expand Up @@ -157,11 +156,11 @@ export namespace TypeGuard {
// ----------------------------------------------------------------
/** Returns true if this value has a Readonly symbol */
export function TReadonly<T extends TSchema>(schema: T): schema is TReadonly<T> {
return ValueGuard.IsObject(schema) && schema[Readonly] === 'Readonly'
return ValueGuard.IsObject(schema) && schema[Symbols.Readonly] === 'Readonly'
}
/** Returns true if this value has a Optional symbol */
export function TOptional<T extends TSchema>(schema: T): schema is TOptional<T> {
return ValueGuard.IsObject(schema) && schema[Optional] === 'Optional'
return ValueGuard.IsObject(schema) && schema[Symbols.Optional] === 'Optional'
}
// ----------------------------------------------------------------
// Types
Expand Down Expand Up @@ -296,8 +295,8 @@ export namespace TypeGuard {
)
}
/** Returns true if the given value is a TKind with the given name. */
export function TKindOf<T extends string>(schema: unknown, kind: T): schema is Record<PropertyKey, unknown> & { [Kind]: T } {
return ValueGuard.IsObject(schema) && Kind in schema && schema[Kind] === kind
export function TKindOf<T extends string>(schema: unknown, kind: T): schema is Record<PropertyKey, unknown> & { [Symbols.Kind]: T } {
return ValueGuard.IsObject(schema) && Symbols.Kind in schema && schema[Symbols.Kind] === kind
}
/** Returns true if the given value is TLiteral<string> */
export function TLiteralString(schema: unknown): schema is TLiteral<string> {
Expand Down Expand Up @@ -407,8 +406,8 @@ export namespace TypeGuard {
)
}
/** Returns true if this value is TRecursive */
export function TRecursive(schema: unknown): schema is { [Hint]: 'Recursive' } {
return ValueGuard.IsObject(schema) && Hint in schema && schema[Hint] === 'Recursive'
export function TRecursive(schema: unknown): schema is { [Symbols.Hint]: 'Recursive' } {
return ValueGuard.IsObject(schema) && Symbols.Hint in schema && schema[Symbols.Hint] === 'Recursive'
}
/** Returns true if the given value is TRef */
export function TRef(schema: unknown): schema is TRef {
Expand Down Expand Up @@ -462,8 +461,8 @@ export namespace TypeGuard {
)
}
/** Returns true of this value is TTransform */
export function TTransform(schema: unknown): schema is { [Transform]: TransformOptions } {
return ValueGuard.IsObject(schema) && Transform in schema
export function TTransform(schema: unknown): schema is { [Symbols.Transform]: TransformOptions } {
return ValueGuard.IsObject(schema) && Symbols.Transform in schema
}
/** Returns true if the given value is TTuple */
export function TTuple(schema: unknown): schema is TTuple {
Expand Down Expand Up @@ -542,12 +541,12 @@ export namespace TypeGuard {
)
}
/** Returns true if the given value is TKind */
export function TKind(schema: unknown): schema is Record<PropertyKey, unknown> & { [Kind]: string } {
export function TKind(schema: unknown): schema is Record<PropertyKey, unknown> & { [Symbols.Kind]: string } {
return (
ValueGuard.IsObject(schema) &&
Kind in schema
&& ValueGuard.IsString(schema[Kind]) &&
!KnownKinds.includes(schema[Kind])
Symbols.Kind in schema
&& ValueGuard.IsString(schema[Symbols.Kind]) &&
!KnownKinds.includes(schema[Symbols.Kind] as string)
)
}
/** Returns true if the given value is TSchema */
Expand Down
7 changes: 3 additions & 4 deletions src/type/resolve/extends/extends-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
TUnknown,
TNot,
TNumber,
Hint,
Symbols,
TPromise,
PatternNumberExact,
PatternStringExact,
Expand All @@ -62,7 +62,6 @@ import {
TTuple,
TString,
TSymbol,
Kind,
} from '../../../typebox'

export class ExtendsResolverError extends Error {}
Expand Down Expand Up @@ -426,7 +425,7 @@ export namespace ExtendsCheck {
(TypeGuard.TRecord(left) && TypeGuard.TString(RecordKey(left))) ? (() => {
// When expressing a Record with literal key values, the Record is converted into a Object with
// the Hint assigned as `Record`. This is used to invert the extends logic.
return right[Hint] === 'Record' ? ExtendsResult.True : ExtendsResult.False
return right[Symbols.Hint] === 'Record' ? ExtendsResult.True : ExtendsResult.False
})() :
(TypeGuard.TRecord(left) && TypeGuard.TNumber(RecordKey(left))) ? (() => {
return IsObjectPropertyCount(right, 0) ? ExtendsResult.True : ExtendsResult.False
Expand Down Expand Up @@ -697,7 +696,7 @@ export namespace ExtendsCheck {
TypeGuard.TUnion(left) ? TUnion(left, right) :
TypeGuard.TUnknown(left) ? TUnknown(left, right) :
TypeGuard.TVoid(left) ? TVoid(left, right) :
Throw(`Unknown left type operand '${left[Kind]}'`)
Throw(`Unknown left type operand '${left[Symbols.Kind]}'`)
)
}
export function Check(left: TSchema, right: TSchema): ExtendsResult {
Expand Down
10 changes: 5 additions & 5 deletions src/type/resolve/extends/extends-undefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TSchema, TIntersect, TUnion, Kind, TNot } from '../../../typebox'
import { TSchema, TIntersect, TUnion, Symbols, TNot } from '../../../typebox'

// ------------------------------------------------------------------
// ExtendsUndefined
Expand All @@ -45,10 +45,10 @@ export namespace ExtendsUndefinedCheck {
// prettier-ignore
export function Resolve(schema: TSchema): boolean {
return (
schema[Kind] === 'Intersect' ? TIntersect(schema as TIntersect) :
schema[Kind] === 'Union' ? TUnion(schema as TUnion) :
schema[Kind] === 'Not' ? TNot(schema as TNot) :
schema[Kind] === 'Undefined' ? true :
schema[Symbols.Kind] === 'Intersect' ? TIntersect(schema as TIntersect) :
schema[Symbols.Kind] === 'Union' ? TUnion(schema as TUnion) :
schema[Symbols.Kind] === 'Not' ? TNot(schema as TNot) :
schema[Symbols.Kind] === 'Undefined' ? true :
false
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/type/resolve/modifiers/modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { Type, TypeGuard, TSchema, TIntersect, TUnion, TOptional, TReadonly, Readonly, Optional } from '../../../typebox'
import { Type, TypeGuard, TSchema, TIntersect, TUnion, TOptional, TReadonly, Symbols } from '../../../typebox'
import { Discard } from '../discard/index'

// ----------------------------------------------------------------
Expand Down Expand Up @@ -98,7 +98,7 @@ export namespace Modifiers {
)
export function RemoveReadonly<T extends TSchema>(T: T): RemoveReadonly<T> {
return (
Discard.Keys(T, [Readonly])
Discard.Keys(T, [Symbols.Readonly])
) as RemoveReadonly<T>
}
// ----------------------------------------------------------------
Expand Down Expand Up @@ -126,7 +126,7 @@ export namespace Modifiers {
)
export function RemoveOptional<T extends TSchema>(T: T): RemoveOptional<T> {
return (
Discard.Keys(T, [Optional])
Discard.Keys(T, [Symbols.Optional])
) as RemoveOptional<T>
}
// ----------------------------------------------------------------
Expand Down
7 changes: 3 additions & 4 deletions src/type/resolve/record/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import {
TEnum,
Type,
TRecord,
Kind,
Hint,
Symbols,
Static,
TSchema,
TTemplateLiteral,
Expand Down Expand Up @@ -63,11 +62,11 @@ export namespace RecordResolver {
// FromKeys
// ----------------------------------------------------------------
function FromPattern(pattern: string, T: TSchema) {
return { [Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Type(T) } }
return { [Symbols.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Type(T) } }
}
function FromKeys(keys: string[], T: TSchema) {
const properties = keys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Type(T) }), {} as TProperties)
return Type.Object(properties, { [Hint]: 'Record' })
return Type.Object(properties, { [Symbols.Hint]: 'Record' })
}
// ----------------------------------------------------------------
// TemplateLiteral (Fast Inference)
Expand Down
4 changes: 2 additions & 2 deletions src/type/resolve/required/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { Type, TSchema, TypeGuard, TObject, TProperties, TIntersect, TUnion, TOptional, TRecursive, TReadonlyOptional, TReadonly, Optional } from '../../../typebox'
import { Type, Symbols, TSchema, TypeGuard, TObject, TProperties, TIntersect, TUnion, TOptional, TRecursive, TReadonlyOptional, TReadonly } from '../../../typebox'
import { Discard } from '../discard'

// prettier-ignore
Expand Down Expand Up @@ -76,7 +76,7 @@ export namespace RequiredResolver {
}>
export function Properties<T extends TProperties>(T: T) {
return globalThis.Object.getOwnPropertyNames(T).reduce((Acc, K) => {
return { ...Acc, [K]: Discard.Keys(T[K], [Optional]) as TSchema }
return { ...Acc, [K]: Discard.Keys(T[K], [Symbols.Optional]) as TSchema }
}, {} as TProperties)
}
// ----------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/type/resolve/template-literal/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TSchema, TTemplateLiteralKind, TypeGuard, Kind, PatternNumber, PatternString, PatternBoolean } from '../../../typebox'
import { TSchema, TTemplateLiteralKind, TypeGuard, Symbols, PatternNumber, PatternString, PatternBoolean } from '../../../typebox'

// ------------------------------------------------------------------
// TemplateLiteralPattern
Expand All @@ -51,7 +51,7 @@ export namespace TemplateLiteralPattern {
TypeGuard.TString(schema) ? `${acc}${PatternString}` :
TypeGuard.TLiteral(schema) ? `${acc}${Escape(schema.const.toString())}` :
TypeGuard.TBoolean(schema) ? `${acc}${PatternBoolean}` :
Throw(`Unexpected Kind '${schema[Kind]}'`)
Throw(`Unexpected Kind '${schema[Symbols.Kind]}'`)
)
}
export function Create(kinds: TTemplateLiteralKind[]): string {
Expand Down
Loading

0 comments on commit cebdc82

Please sign in to comment.