Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
---

### Version Updates
- [Revision 1.0.71](https://github.com/sinclairzx81/typebox/pull/1499)
- Additional Inference for XSchema Types
- [Revision 1.0.70](https://github.com/sinclairzx81/typebox/pull/1498)
- Static Boolean Schematics | Static Template Literal Record Key
- [Revision 1.0.69](https://github.com/sinclairzx81/typebox/pull/1493)
Expand Down
3 changes: 2 additions & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ System.Settings.Set({ enumerableKind: false })
// Guard
// ------------------------------------------------------------------

console.log(Guard.GraphemeCount('📦'))
const A = Guard.GraphemeCount('type-📦') // 6
const B = Guard.HasPropertyKey({ x: 1 }, 'x') // true

// ------------------------------------------------------------------
// Type
Expand Down
12 changes: 6 additions & 6 deletions src/schema/static/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ import type { XStaticSchema } from './schema.ts'
import type { XStaticElements } from './~elements.ts'

// ------------------------------------------------------------------
// TFromSized
// XFromSized
// ------------------------------------------------------------------
type TFromSized<Stack extends string[], Root extends XSchema, Schema extends XSchema, Items extends XSchema[]> = (
type XFromSized<Stack extends string[], Root extends XSchema, Schema extends XSchema, Items extends XSchema[]> = (
XStaticElements<Stack, Root, Schema, Items>
)
// ------------------------------------------------------------------
// TFromUnsized
// XFromUnsized
// ------------------------------------------------------------------
type TFromUnsized<Stack extends string[], Root extends XSchema, Schema extends XSchema> = (
type XFromUnsized<Stack extends string[], Root extends XSchema, Schema extends XSchema> = (
XStaticSchema<Stack, Root, Schema>[]
)
// ------------------------------------------------------------------
// XStaticItems
// ------------------------------------------------------------------
export type XStaticItems<Stack extends string[], Root extends XSchema, Schema extends XSchema, Items extends XSchema[] | XSchema,
Result extends unknown = (
Items extends XSchema[] ? TFromSized<Stack, Root, Schema, [...Items]> :
Items extends XSchema ? TFromUnsized<Stack, Root, Items> :
Items extends XSchema[] ? XFromSized<Stack, Root, Schema, [...Items]> :
Items extends XSchema ? XFromUnsized<Stack, Root, Items> :
never
)
> = Result
14 changes: 7 additions & 7 deletions src/schema/static/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ THE SOFTWARE.

import type { XSchema } from '../types/schema.ts'
import type { XStaticSchema } from './schema.ts'
import type { TIsReadonly } from './~readonly.ts'
import type { XIsReadonly } from './~readonly.ts'

// ------------------------------------------------------------------
// ReadonlyOptionalProperties
// ------------------------------------------------------------------
type TReadonlyOptionalProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>> = {
readonly [Key in keyof Properties as TIsReadonly<Properties[Key]> extends true ? Key : never]?: XStaticSchema<Stack, Root, Properties[Key]>
type XReadonlyOptionalProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>> = {
readonly [Key in keyof Properties as XIsReadonly<Properties[Key]> extends true ? Key : never]?: XStaticSchema<Stack, Root, Properties[Key]>
}
// ------------------------------------------------------------------
// OptionalProperties
// ------------------------------------------------------------------
type TOptionalProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>> = {
[Key in keyof Properties as TIsReadonly<Properties[Key]> extends true ? never : Key]?: XStaticSchema<Stack, Root, Properties[Key]>
type XOptionalProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>> = {
[Key in keyof Properties as XIsReadonly<Properties[Key]> extends true ? never : Key]?: XStaticSchema<Stack, Root, Properties[Key]>
}
// ------------------------------------------------------------------
// XStaticProperties
// ------------------------------------------------------------------
export type XStaticProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>,
ReadonlyOptional extends Record<PropertyKey, unknown> = TReadonlyOptionalProperties<Stack, Root, Properties>,
Optional extends Record<PropertyKey, unknown> = TOptionalProperties<Stack, Root, Properties>,
ReadonlyOptional extends Record<PropertyKey, unknown> = XReadonlyOptionalProperties<Stack, Root, Properties>,
Optional extends Record<PropertyKey, unknown> = XOptionalProperties<Stack, Root, Properties>,
Result extends Record<PropertyKey, unknown> = ReadonlyOptional & Optional
> = Result
18 changes: 9 additions & 9 deletions src/schema/static/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ import type { XPointerGet } from '../pointer/pointer-get.ts'
import type { XStaticSchema } from './schema.ts'

// ------------------------------------------------------------------
// Cyclic
// XCyclicGuard
// ------------------------------------------------------------------
type CyclicCheck<Stack extends unknown[], MaxLength extends number, Buffer extends unknown[] = []> = (
type XCyclicCheck<Stack extends unknown[], MaxLength extends number, Buffer extends unknown[] = []> = (
Stack extends [infer Left, ...infer Right]
? Buffer['length'] extends MaxLength
? false
: CyclicCheck<Right, MaxLength, [...Buffer, Left]>
: XCyclicCheck<Right, MaxLength, [...Buffer, Left]>
: true
)
type CyclicGuard<Stack extends unknown[], Ref extends string> = (
Ref extends Stack[number] ? CyclicCheck<Stack, 2> : true
type XCyclicGuard<Stack extends unknown[], Ref extends string> = (
Ref extends Stack[number] ? XCyclicCheck<Stack, 2> : true
)
// ------------------------------------------------------------------
// Normal
// XNormal
// ------------------------------------------------------------------
type TNormal<Pointer extends string,
type XNormal<Pointer extends string,
Result extends string = (
Pointer extends `#${infer Rest extends string}`
? Rest
Expand All @@ -58,11 +58,11 @@ type TNormal<Pointer extends string,
// XStaticRef
// ------------------------------------------------------------------
export type XStaticRef<Stack extends string[], Root extends XSchema, Ref extends string,
Normal extends string = TNormal<Ref>,
Normal extends string = XNormal<Ref>,
Target extends unknown = XPointerGet<Root, Normal>,
Schema extends XSchema = Target extends XSchema ? Target : {},
Result extends unknown = (
CyclicGuard<Stack, Ref> extends true
XCyclicGuard<Stack, Ref> extends true
? XStaticSchema<[...Stack, Ref], Root, Schema>
: any // terminate-recursive
)> = Result
16 changes: 8 additions & 8 deletions src/schema/static/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ THE SOFTWARE.

import type { XSchema } from '../types/schema.ts'
import type { XProperties } from '../types/properties.ts'
import type { TIsReadonly } from './~readonly.ts'
import type { XIsReadonly } from './~readonly.ts'
import type { XStaticSchema } from './schema.ts'

// ------------------------------------------------------------------
// ResolveProperties
// ------------------------------------------------------------------
type TResolveProperties<Schema extends XSchema, Result extends Record<PropertyKey, XSchema> = (
type XResolveProperties<Schema extends XSchema, Result extends Record<PropertyKey, XSchema> = (
Schema extends XProperties<infer Properties extends Record<PropertyKey, XSchema>> ? Properties : {}
)> = Result
// ------------------------------------------------------------------
// FromKey
// ------------------------------------------------------------------
type TFromKey<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Key extends string,
Readonly extends boolean = Key extends keyof Properties ? TIsReadonly<Properties[Key]> : false,
type XFromKey<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Key extends string,
Readonly extends boolean = Key extends keyof Properties ? XIsReadonly<Properties[Key]> : false,
Value extends unknown = Key extends keyof Properties ? XStaticSchema<Stack, Root, Properties[Key]> : unknown,
Result extends Record<PropertyKey, unknown> = (
Readonly extends true
Expand All @@ -53,15 +53,15 @@ type TFromKey<Stack extends string[], Root extends XSchema, Properties extends R
// ------------------------------------------------------------------
// FromKeys
// ------------------------------------------------------------------
type TFromKeys<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Keys extends string[], Result extends Record<PropertyKey, unknown> = {}> = (
type XFromKeys<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Keys extends string[], Result extends Record<PropertyKey, unknown> = {}> = (
Keys extends [infer Left extends string, ...infer Right extends string[]]
? TFromKeys<Stack, Root, Properties, Right, Result & TFromKey<Stack, Root, Properties, Left>>
? XFromKeys<Stack, Root, Properties, Right, Result & XFromKey<Stack, Root, Properties, Left>>
: Result
)
// ------------------------------------------------------------------
// XStaticRequired
// ------------------------------------------------------------------
export type XStaticRequired<Stack extends string[], Root extends XSchema, Schema extends XSchema, Keys extends string[],
Properties extends Record<PropertyKey, XSchema> = TResolveProperties<Schema>,
Result extends Record<PropertyKey, unknown> = TFromKeys<Stack, Root, Properties, Keys>
Properties extends Record<PropertyKey, XSchema> = XResolveProperties<Schema>,
Result extends Record<PropertyKey, unknown> = XFromKeys<Stack, Root, Properties, Keys>
> = Result
14 changes: 7 additions & 7 deletions src/schema/static/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import type { XStaticUnevaluatedProperties } from './unevaluatedProperties.ts'
// ------------------------------------------------------------------
// Keywords
// ------------------------------------------------------------------
type TFromKeywords<Stack extends string[], Root extends XSchema, Schema extends XSchema, Result extends unknown[] = [
type XFromKeywords<Stack extends string[], Root extends XSchema, Schema extends XSchema, Result extends unknown[] = [
Schema extends XAdditionalProperties<infer Type extends XSchema> ? XStaticAdditionalProperties<Stack, Root, Type> : unknown,
Schema extends XAllOf<infer Types extends XSchema[]> ? XStaticAllOf<Stack, Root, Types> : unknown,
Schema extends XAnyOf<infer Types extends XSchema[]> ? XStaticAnyOf<Stack, Root, Types> : unknown,
Expand All @@ -84,15 +84,15 @@ type TFromKeywords<Stack extends string[], Root extends XSchema, Schema extends
// ------------------------------------------------------------------
// TIntersectKeywords
// ------------------------------------------------------------------
type TKeywordsIntersected<Schemas extends unknown[], Result extends unknown = unknown> = (
type XKeywordsIntersected<Schemas extends unknown[], Result extends unknown = unknown> = (
Schemas extends [infer Left extends unknown, ...infer Right extends unknown[]]
? TKeywordsIntersected<Right, Result & Left>
? XKeywordsIntersected<Right, Result & Left>
: Result
)
// ------------------------------------------------------------------
// XStaticEvaluate
// ------------------------------------------------------------------
type TKeywordsEvaluated<Schema extends unknown,
type XKeywordsEvaluated<Schema extends unknown,
Result extends unknown = Schema extends object
? { [Key in keyof Schema]: Schema[Key] }
: Schema
Expand All @@ -101,9 +101,9 @@ type TKeywordsEvaluated<Schema extends unknown,
// XStaticObject
// ------------------------------------------------------------------
export type XStaticObject<Stack extends string[], Root extends XSchema, Schema extends XSchema,
Keywords extends unknown[] = TFromKeywords<Stack, Root, Schema>,
Intersected extends unknown = TKeywordsIntersected<Keywords>,
Evaluated extends unknown = TKeywordsEvaluated<Intersected>
Keywords extends unknown[] = XFromKeywords<Stack, Root, Schema>,
Intersected extends unknown = XKeywordsIntersected<Keywords>,
Evaluated extends unknown = XKeywordsEvaluated<Intersected>
> = Evaluated
// ------------------------------------------------------------------
// XStaticBoolean
Expand Down
6 changes: 3 additions & 3 deletions src/schema/static/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ THE SOFTWARE.
// deno-fmt-ignore-file

import type { XSchema } from '../types/schema.ts'
import type { XMutable } from './~mutable.ts'
import type { XNonReadonly } from './~non-readonly.ts'
import type { XStaticSchema } from './schema.ts'

// ------------------------------------------------------------------
// XStatic
// ------------------------------------------------------------------
export type XStatic<Value extends unknown,
Schema extends XSchema = Value extends XSchema ? Value : {},
Mutable extends XSchema = XMutable<Schema>,
Result extends unknown = XStaticSchema<[], Mutable, Mutable>
NonReadonly extends XSchema = XNonReadonly<Schema>,
Result extends unknown = XStaticSchema<[], NonReadonly, NonReadonly>
> = Result
24 changes: 16 additions & 8 deletions src/schema/static/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,40 @@ THE SOFTWARE.
// ------------------------------------------------------------------
// FromTypeNames
// ------------------------------------------------------------------
type TFromTypeNames<TypeNames extends string[], Result extends unknown = never> = (
type XFromTypeNames<TypeNames extends string[], Result extends unknown = never> = (
TypeNames extends readonly [infer Left extends string, ...infer Right extends string[]]
? TFromTypeNames<Right, Result | TFromTypeName<Left>>
? XFromTypeNames<Right, Result | XFromTypeName<Left>>
: Result
)
// ------------------------------------------------------------------
// FromTypeName
// ------------------------------------------------------------------
type TFromTypeName<TypeName extends string> = (
type XFromTypeName<TypeName extends string> = (
// jsonschema
TypeName extends 'object' ? object :
TypeName extends 'array' ? {} :
TypeName extends 'bigint' ? bigint :
TypeName extends 'boolean' ? boolean :
TypeName extends 'integer' ? number :
TypeName extends 'object' ? object :
TypeName extends 'null' ? null :
TypeName extends 'number' ? number :
TypeName extends 'null' ? null :
TypeName extends 'string' ? string :
// xschema
TypeName extends 'bigint' ? bigint :
TypeName extends 'symbol' ? symbol :
TypeName extends 'undefined' ? undefined :
TypeName extends 'void' ? void :
// xschema - structural objects
TypeName extends 'asyncIterator' ? {} :
TypeName extends 'constructor' ? {} :
TypeName extends 'function' ? {} :
TypeName extends 'iterator' ? {} :
unknown
)
// ------------------------------------------------------------------
// XStaticType
// ------------------------------------------------------------------
export type XStaticType<TypeName extends string[] | string> = (
TypeName extends string[] ? TFromTypeNames<TypeName> :
TypeName extends string ? TFromTypeName<TypeName> :
TypeName extends string[] ? XFromTypeNames<TypeName> :
TypeName extends string ? XFromTypeName<TypeName> :
unknown
)
24 changes: 18 additions & 6 deletions src/schema/static/~comparer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,37 @@ THE SOFTWARE.

---------------------------------------------------------------------------*/

// deno-fmt-ignore-file

// ------------------------------------------------------------------
// CreateTuple
// XBuildTuple
// ------------------------------------------------------------------
type CreateTuple<Size extends number, Tuple extends unknown[] = []> = Tuple['length'] extends Size ? Tuple : CreateTuple<Size, [...Tuple, unknown]>
type XBuildTuple<Size extends number, Tuple extends unknown[] = []> = (
Tuple['length'] extends Size
? Tuple
: XBuildTuple<Size, [...Tuple, unknown]>
)
// ------------------------------------------------------------------
// LessThan
// ------------------------------------------------------------------
export type XLessThan<Left extends number, Right extends number> = Left extends Right ? false
: CreateTuple<Left> extends [...CreateTuple<Right>, ...infer _Rest] ? false
: XBuildTuple<Left> extends [...XBuildTuple<Right>, ...infer _Rest] ? false
: true
// ------------------------------------------------------------------
// LessThanEqual
// ------------------------------------------------------------------
export type XLessThanEqual<Left extends number, Right extends number> = Left extends Right ? true : XLessThan<Left, Right>
export type XLessThanEqual<Left extends number, Right extends number> = (
Left extends Right ? true : XLessThan<Left, Right>
)
// ------------------------------------------------------------------
// GreaterThan
// ------------------------------------------------------------------
export type XGreaterThan<Left extends number, Right extends number> = XLessThan<Right, Left>
export type XGreaterThan<Left extends number, Right extends number> = (
XLessThan<Right, Left>
)
// ------------------------------------------------------------------
// GreaterThanEqual
// ------------------------------------------------------------------
export type XGreaterThanEqual<Left extends number, Right extends number> = XLessThanEqual<Right, Left>
export type XGreaterThanEqual<Left extends number, Right extends number> = (
XLessThanEqual<Right, Left>
)
Loading