Skip to content

Commit 4728162

Browse files
committed
XSchema Inference Updates
1 parent 1773b2e commit 4728162

File tree

13 files changed

+184
-117
lines changed

13 files changed

+184
-117
lines changed

example/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ System.Settings.Set({ enumerableKind: false })
1616
// Guard
1717
// ------------------------------------------------------------------
1818

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

2122
// ------------------------------------------------------------------
2223
// Type

src/schema/static/items.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ import type { XStaticSchema } from './schema.ts'
3333
import type { XStaticElements } from './~elements.ts'
3434

3535
// ------------------------------------------------------------------
36-
// TFromSized
36+
// XFromSized
3737
// ------------------------------------------------------------------
38-
type TFromSized<Stack extends string[], Root extends XSchema, Schema extends XSchema, Items extends XSchema[]> = (
38+
type XFromSized<Stack extends string[], Root extends XSchema, Schema extends XSchema, Items extends XSchema[]> = (
3939
XStaticElements<Stack, Root, Schema, Items>
4040
)
4141
// ------------------------------------------------------------------
42-
// TFromUnsized
42+
// XFromUnsized
4343
// ------------------------------------------------------------------
44-
type TFromUnsized<Stack extends string[], Root extends XSchema, Schema extends XSchema> = (
44+
type XFromUnsized<Stack extends string[], Root extends XSchema, Schema extends XSchema> = (
4545
XStaticSchema<Stack, Root, Schema>[]
4646
)
4747
// ------------------------------------------------------------------
4848
// XStaticItems
4949
// ------------------------------------------------------------------
5050
export type XStaticItems<Stack extends string[], Root extends XSchema, Schema extends XSchema, Items extends XSchema[] | XSchema,
5151
Result extends unknown = (
52-
Items extends XSchema[] ? TFromSized<Stack, Root, Schema, [...Items]> :
53-
Items extends XSchema ? TFromUnsized<Stack, Root, Items> :
52+
Items extends XSchema[] ? XFromSized<Stack, Root, Schema, [...Items]> :
53+
Items extends XSchema ? XFromUnsized<Stack, Root, Items> :
5454
never
5555
)
5656
> = Result

src/schema/static/properties.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ THE SOFTWARE.
3030

3131
import type { XSchema } from '../types/schema.ts'
3232
import type { XStaticSchema } from './schema.ts'
33-
import type { TIsReadonly } from './~readonly.ts'
33+
import type { XIsReadonly } from './~readonly.ts'
3434

3535
// ------------------------------------------------------------------
3636
// ReadonlyOptionalProperties
3737
// ------------------------------------------------------------------
38-
type TReadonlyOptionalProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>> = {
39-
readonly [Key in keyof Properties as TIsReadonly<Properties[Key]> extends true ? Key : never]?: XStaticSchema<Stack, Root, Properties[Key]>
38+
type XReadonlyOptionalProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>> = {
39+
readonly [Key in keyof Properties as XIsReadonly<Properties[Key]> extends true ? Key : never]?: XStaticSchema<Stack, Root, Properties[Key]>
4040
}
4141
// ------------------------------------------------------------------
4242
// OptionalProperties
4343
// ------------------------------------------------------------------
44-
type TOptionalProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>> = {
45-
[Key in keyof Properties as TIsReadonly<Properties[Key]> extends true ? never : Key]?: XStaticSchema<Stack, Root, Properties[Key]>
44+
type XOptionalProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>> = {
45+
[Key in keyof Properties as XIsReadonly<Properties[Key]> extends true ? never : Key]?: XStaticSchema<Stack, Root, Properties[Key]>
4646
}
4747
// ------------------------------------------------------------------
4848
// XStaticProperties
4949
// ------------------------------------------------------------------
5050
export type XStaticProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>,
51-
ReadonlyOptional extends Record<PropertyKey, unknown> = TReadonlyOptionalProperties<Stack, Root, Properties>,
52-
Optional extends Record<PropertyKey, unknown> = TOptionalProperties<Stack, Root, Properties>,
51+
ReadonlyOptional extends Record<PropertyKey, unknown> = XReadonlyOptionalProperties<Stack, Root, Properties>,
52+
Optional extends Record<PropertyKey, unknown> = XOptionalProperties<Stack, Root, Properties>,
5353
Result extends Record<PropertyKey, unknown> = ReadonlyOptional & Optional
5454
> = Result

src/schema/static/ref.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ import type { XPointerGet } from '../pointer/pointer-get.ts'
3333
import type { XStaticSchema } from './schema.ts'
3434

3535
// ------------------------------------------------------------------
36-
// Cyclic
36+
// XCyclicGuard
3737
// ------------------------------------------------------------------
38-
type CyclicCheck<Stack extends unknown[], MaxLength extends number, Buffer extends unknown[] = []> = (
38+
type XCyclicCheck<Stack extends unknown[], MaxLength extends number, Buffer extends unknown[] = []> = (
3939
Stack extends [infer Left, ...infer Right]
4040
? Buffer['length'] extends MaxLength
4141
? false
42-
: CyclicCheck<Right, MaxLength, [...Buffer, Left]>
42+
: XCyclicCheck<Right, MaxLength, [...Buffer, Left]>
4343
: true
4444
)
45-
type CyclicGuard<Stack extends unknown[], Ref extends string> = (
46-
Ref extends Stack[number] ? CyclicCheck<Stack, 2> : true
45+
type XCyclicGuard<Stack extends unknown[], Ref extends string> = (
46+
Ref extends Stack[number] ? XCyclicCheck<Stack, 2> : true
4747
)
4848
// ------------------------------------------------------------------
49-
// Normal
49+
// XNormal
5050
// ------------------------------------------------------------------
51-
type TNormal<Pointer extends string,
51+
type XNormal<Pointer extends string,
5252
Result extends string = (
5353
Pointer extends `#${infer Rest extends string}`
5454
? Rest
@@ -58,11 +58,11 @@ type TNormal<Pointer extends string,
5858
// XStaticRef
5959
// ------------------------------------------------------------------
6060
export type XStaticRef<Stack extends string[], Root extends XSchema, Ref extends string,
61-
Normal extends string = TNormal<Ref>,
61+
Normal extends string = XNormal<Ref>,
6262
Target extends unknown = XPointerGet<Root, Normal>,
6363
Schema extends XSchema = Target extends XSchema ? Target : {},
6464
Result extends unknown = (
65-
CyclicGuard<Stack, Ref> extends true
65+
XCyclicGuard<Stack, Ref> extends true
6666
? XStaticSchema<[...Stack, Ref], Root, Schema>
6767
: any // terminate-recursive
6868
)> = Result

src/schema/static/required.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ THE SOFTWARE.
3030

3131
import type { XSchema } from '../types/schema.ts'
3232
import type { XProperties } from '../types/properties.ts'
33-
import type { TIsReadonly } from './~readonly.ts'
33+
import type { XIsReadonly } from './~readonly.ts'
3434
import type { XStaticSchema } from './schema.ts'
3535

3636
// ------------------------------------------------------------------
3737
// ResolveProperties
3838
// ------------------------------------------------------------------
39-
type TResolveProperties<Schema extends XSchema, Result extends Record<PropertyKey, XSchema> = (
39+
type XResolveProperties<Schema extends XSchema, Result extends Record<PropertyKey, XSchema> = (
4040
Schema extends XProperties<infer Properties extends Record<PropertyKey, XSchema>> ? Properties : {}
4141
)> = Result
4242
// ------------------------------------------------------------------
4343
// FromKey
4444
// ------------------------------------------------------------------
45-
type TFromKey<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Key extends string,
46-
Readonly extends boolean = Key extends keyof Properties ? TIsReadonly<Properties[Key]> : false,
45+
type XFromKey<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Key extends string,
46+
Readonly extends boolean = Key extends keyof Properties ? XIsReadonly<Properties[Key]> : false,
4747
Value extends unknown = Key extends keyof Properties ? XStaticSchema<Stack, Root, Properties[Key]> : unknown,
4848
Result extends Record<PropertyKey, unknown> = (
4949
Readonly extends true
@@ -53,15 +53,15 @@ type TFromKey<Stack extends string[], Root extends XSchema, Properties extends R
5353
// ------------------------------------------------------------------
5454
// FromKeys
5555
// ------------------------------------------------------------------
56-
type TFromKeys<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Keys extends string[], Result extends Record<PropertyKey, unknown> = {}> = (
56+
type XFromKeys<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Keys extends string[], Result extends Record<PropertyKey, unknown> = {}> = (
5757
Keys extends [infer Left extends string, ...infer Right extends string[]]
58-
? TFromKeys<Stack, Root, Properties, Right, Result & TFromKey<Stack, Root, Properties, Left>>
58+
? XFromKeys<Stack, Root, Properties, Right, Result & XFromKey<Stack, Root, Properties, Left>>
5959
: Result
6060
)
6161
// ------------------------------------------------------------------
6262
// XStaticRequired
6363
// ------------------------------------------------------------------
6464
export type XStaticRequired<Stack extends string[], Root extends XSchema, Schema extends XSchema, Keys extends string[],
65-
Properties extends Record<PropertyKey, XSchema> = TResolveProperties<Schema>,
66-
Result extends Record<PropertyKey, unknown> = TFromKeys<Stack, Root, Properties, Keys>
65+
Properties extends Record<PropertyKey, XSchema> = XResolveProperties<Schema>,
66+
Result extends Record<PropertyKey, unknown> = XFromKeys<Stack, Root, Properties, Keys>
6767
> = Result

src/schema/static/schema.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import type { XStaticUnevaluatedProperties } from './unevaluatedProperties.ts'
6464
// ------------------------------------------------------------------
6565
// Keywords
6666
// ------------------------------------------------------------------
67-
type TFromKeywords<Stack extends string[], Root extends XSchema, Schema extends XSchema, Result extends unknown[] = [
67+
type XFromKeywords<Stack extends string[], Root extends XSchema, Schema extends XSchema, Result extends unknown[] = [
6868
Schema extends XAdditionalProperties<infer Type extends XSchema> ? XStaticAdditionalProperties<Stack, Root, Type> : unknown,
6969
Schema extends XAllOf<infer Types extends XSchema[]> ? XStaticAllOf<Stack, Root, Types> : unknown,
7070
Schema extends XAnyOf<infer Types extends XSchema[]> ? XStaticAnyOf<Stack, Root, Types> : unknown,
@@ -84,15 +84,15 @@ type TFromKeywords<Stack extends string[], Root extends XSchema, Schema extends
8484
// ------------------------------------------------------------------
8585
// TIntersectKeywords
8686
// ------------------------------------------------------------------
87-
type TKeywordsIntersected<Schemas extends unknown[], Result extends unknown = unknown> = (
87+
type XKeywordsIntersected<Schemas extends unknown[], Result extends unknown = unknown> = (
8888
Schemas extends [infer Left extends unknown, ...infer Right extends unknown[]]
89-
? TKeywordsIntersected<Right, Result & Left>
89+
? XKeywordsIntersected<Right, Result & Left>
9090
: Result
9191
)
9292
// ------------------------------------------------------------------
9393
// XStaticEvaluate
9494
// ------------------------------------------------------------------
95-
type TKeywordsEvaluated<Schema extends unknown,
95+
type XKeywordsEvaluated<Schema extends unknown,
9696
Result extends unknown = Schema extends object
9797
? { [Key in keyof Schema]: Schema[Key] }
9898
: Schema
@@ -101,9 +101,9 @@ type TKeywordsEvaluated<Schema extends unknown,
101101
// XStaticObject
102102
// ------------------------------------------------------------------
103103
export type XStaticObject<Stack extends string[], Root extends XSchema, Schema extends XSchema,
104-
Keywords extends unknown[] = TFromKeywords<Stack, Root, Schema>,
105-
Intersected extends unknown = TKeywordsIntersected<Keywords>,
106-
Evaluated extends unknown = TKeywordsEvaluated<Intersected>
104+
Keywords extends unknown[] = XFromKeywords<Stack, Root, Schema>,
105+
Intersected extends unknown = XKeywordsIntersected<Keywords>,
106+
Evaluated extends unknown = XKeywordsEvaluated<Intersected>
107107
> = Evaluated
108108
// ------------------------------------------------------------------
109109
// XStaticBoolean

src/schema/static/static.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ THE SOFTWARE.
2929
// deno-fmt-ignore-file
3030

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

3535
// ------------------------------------------------------------------
3636
// XStatic
3737
// ------------------------------------------------------------------
3838
export type XStatic<Value extends unknown,
3939
Schema extends XSchema = Value extends XSchema ? Value : {},
40-
Mutable extends XSchema = XMutable<Schema>,
41-
Result extends unknown = XStaticSchema<[], Mutable, Mutable>
40+
NonReadonly extends XSchema = XNonReadonly<Schema>,
41+
Result extends unknown = XStaticSchema<[], NonReadonly, NonReadonly>
4242
> = Result

src/schema/static/type.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,40 @@ THE SOFTWARE.
3131
// ------------------------------------------------------------------
3232
// FromTypeNames
3333
// ------------------------------------------------------------------
34-
type TFromTypeNames<TypeNames extends string[], Result extends unknown = never> = (
34+
type XFromTypeNames<TypeNames extends string[], Result extends unknown = never> = (
3535
TypeNames extends readonly [infer Left extends string, ...infer Right extends string[]]
36-
? TFromTypeNames<Right, Result | TFromTypeName<Left>>
36+
? XFromTypeNames<Right, Result | XFromTypeName<Left>>
3737
: Result
3838
)
3939
// ------------------------------------------------------------------
4040
// FromTypeName
4141
// ------------------------------------------------------------------
42-
type TFromTypeName<TypeName extends string> = (
42+
type XFromTypeName<TypeName extends string> = (
43+
// jsonschema
44+
TypeName extends 'object' ? object :
4345
TypeName extends 'array' ? {} :
44-
TypeName extends 'bigint' ? bigint :
4546
TypeName extends 'boolean' ? boolean :
4647
TypeName extends 'integer' ? number :
47-
TypeName extends 'object' ? object :
48-
TypeName extends 'null' ? null :
4948
TypeName extends 'number' ? number :
49+
TypeName extends 'null' ? null :
5050
TypeName extends 'string' ? string :
51+
// xschema
52+
TypeName extends 'bigint' ? bigint :
5153
TypeName extends 'symbol' ? symbol :
5254
TypeName extends 'undefined' ? undefined :
55+
TypeName extends 'void' ? void :
56+
// xschema - structural objects
57+
TypeName extends 'asyncIterator' ? {} :
58+
TypeName extends 'constructor' ? {} :
59+
TypeName extends 'function' ? {} :
60+
TypeName extends 'iterator' ? {} :
5361
unknown
5462
)
5563
// ------------------------------------------------------------------
5664
// XStaticType
5765
// ------------------------------------------------------------------
5866
export type XStaticType<TypeName extends string[] | string> = (
59-
TypeName extends string[] ? TFromTypeNames<TypeName> :
60-
TypeName extends string ? TFromTypeName<TypeName> :
67+
TypeName extends string[] ? XFromTypeNames<TypeName> :
68+
TypeName extends string ? XFromTypeName<TypeName> :
6169
unknown
6270
)

src/schema/static/~comparer.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,37 @@ THE SOFTWARE.
2626
2727
---------------------------------------------------------------------------*/
2828

29+
// deno-fmt-ignore-file
30+
2931
// ------------------------------------------------------------------
30-
// CreateTuple
32+
// XBuildTuple
3133
// ------------------------------------------------------------------
32-
type CreateTuple<Size extends number, Tuple extends unknown[] = []> = Tuple['length'] extends Size ? Tuple : CreateTuple<Size, [...Tuple, unknown]>
34+
type XBuildTuple<Size extends number, Tuple extends unknown[] = []> = (
35+
Tuple['length'] extends Size
36+
? Tuple
37+
: XBuildTuple<Size, [...Tuple, unknown]>
38+
)
3339
// ------------------------------------------------------------------
3440
// LessThan
3541
// ------------------------------------------------------------------
3642
export type XLessThan<Left extends number, Right extends number> = Left extends Right ? false
37-
: CreateTuple<Left> extends [...CreateTuple<Right>, ...infer _Rest] ? false
43+
: XBuildTuple<Left> extends [...XBuildTuple<Right>, ...infer _Rest] ? false
3844
: true
3945
// ------------------------------------------------------------------
4046
// LessThanEqual
4147
// ------------------------------------------------------------------
42-
export type XLessThanEqual<Left extends number, Right extends number> = Left extends Right ? true : XLessThan<Left, Right>
48+
export type XLessThanEqual<Left extends number, Right extends number> = (
49+
Left extends Right ? true : XLessThan<Left, Right>
50+
)
4351
// ------------------------------------------------------------------
4452
// GreaterThan
4553
// ------------------------------------------------------------------
46-
export type XGreaterThan<Left extends number, Right extends number> = XLessThan<Right, Left>
54+
export type XGreaterThan<Left extends number, Right extends number> = (
55+
XLessThan<Right, Left>
56+
)
4757
// ------------------------------------------------------------------
4858
// GreaterThanEqual
4959
// ------------------------------------------------------------------
50-
export type XGreaterThanEqual<Left extends number, Right extends number> = XLessThanEqual<Right, Left>
60+
export type XGreaterThanEqual<Left extends number, Right extends number> = (
61+
XLessThanEqual<Right, Left>
62+
)

0 commit comments

Comments
 (0)