Skip to content

Commit 0133310

Browse files
authored
Version 1.1.27 (#1580)
* Inference Optimization for XStaticProperties * Fix Evaluation for Conditional Keyword Evaluation * ChangeLog * Version
1 parent 1ccfbc7 commit 0133310

10 files changed

Lines changed: 425 additions & 311 deletions

File tree

changelog/1.1.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
---
44

55
### Version Updates
6+
- [Revision 1.1.27](https://github.com/sinclairzx81/typebox/pull/1580)
7+
- Inference Optimization for XStaticProperties
68
- [Revision 1.1.26](https://github.com/sinclairzx81/typebox/pull/1578)
79
- Presentation Update for Record Inference
810
- [Revision 1.1.25](https://github.com/sinclairzx81/typebox/pull/1577)

example/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ const E = Format.IsEmail('user@domain.com')
6767
// Schema
6868
// ------------------------------------------------------------------
6969

70-
const D = Schema.Check({ type: 'string' }, 'hello')
70+
const D = Schema.Parse({ const: 'hello' }, 'hello')

src/schema/static/properties.ts

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

3131
import type { XSchema } from '../types/schema.ts'
32+
import type { XRequired } from '../types/required.ts'
3233
import type { XStaticSchema } from './schema.ts'
33-
import type { XIsReadonly } from './~readonly.ts'
3434

3535
// ------------------------------------------------------------------
36-
// ReadonlyOptionalProperties
36+
// IsReadonly
3737
// ------------------------------------------------------------------
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]>
40-
}
38+
type XIsReadonly<Schema extends XSchema> = (
39+
Schema extends { readOnly: true } ? true :
40+
Schema extends { '~readonly': true } ? true : // review
41+
false
42+
)
43+
// ------------------------------------------------------------------
44+
// RequiredArray
45+
// ------------------------------------------------------------------
46+
type XRequiredArray<Schema extends XSchema,
47+
Result extends PropertyKey[] = Schema extends XRequired<infer Keys extends string[]> ? Keys : []
48+
> = Result
49+
// ------------------------------------------------------------------
50+
// Keys
51+
// ------------------------------------------------------------------
52+
type XReadonlyKeys<Properties extends Record<PropertyKey, XSchema>,
53+
ReadonlyProperties extends Record<PropertyKey, unknown> = { [Key in keyof Properties as XIsReadonly<Properties[Key]> extends true ? Key : never]: unknown },
54+
Result extends PropertyKey = keyof ReadonlyProperties
55+
> = Result
56+
type XRequiredKeys<Properties extends Record<PropertyKey, XSchema>, RequiredArray extends string[],
57+
Result extends PropertyKey = RequiredArray extends [] ? never : Extract<keyof Properties, RequiredArray[number]>
58+
> = Result
59+
type XUnknownKeys<Properties extends Record<PropertyKey, XSchema>, RequiredArray extends string[],
60+
Result extends PropertyKey = Exclude<RequiredArray[number], keyof Properties>
61+
> = Result
62+
type XOptionalKeys<Properties extends Record<PropertyKey, XSchema>, RequiredArray extends string[],
63+
Result extends PropertyKey = RequiredArray extends [] ? keyof Properties : Exclude<keyof Properties, RequiredArray[number]>
64+
> = Result
4165
// ------------------------------------------------------------------
42-
// OptionalProperties
66+
// Properties
4367
// ------------------------------------------------------------------
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]>
68+
type XReadonlyOptionalProperties<Stack extends string[], Root extends XSchema, OptionalKeys extends PropertyKey, Properties extends Record<PropertyKey, XSchema>> = {
69+
readonly [Key in Extract<keyof Properties, OptionalKeys>]?: XStaticSchema<Stack, Root, Properties[Key]>
70+
}
71+
type XReadonlyRequiredProperties<Stack extends string[], Root extends XSchema, RequiredKeys extends PropertyKey, Properties extends Record<PropertyKey, XSchema>> = {
72+
readonly [Key in Extract<keyof Properties, RequiredKeys>]: XStaticSchema<Stack, Root, Properties[Key]>
73+
}
74+
type XOptionalProperties<Stack extends string[], Root extends XSchema, OptionalKeys extends PropertyKey, Properties extends Record<PropertyKey, XSchema>> = {
75+
[Key in Extract<keyof Properties, OptionalKeys>]?: XStaticSchema<Stack, Root, Properties[Key]>
76+
}
77+
type XRequiredProperties<Stack extends string[], Root extends XSchema, RequiredKeys extends PropertyKey, Properties extends Record<PropertyKey, XSchema>> = {
78+
[Key in Extract<keyof Properties, RequiredKeys>]: XStaticSchema<Stack, Root, Properties[Key]>
79+
}
80+
type XUnknownProperties<UnknownKeys extends PropertyKey> = {
81+
[Key in UnknownKeys]: unknown
4682
}
4783
// ------------------------------------------------------------------
4884
// XStaticProperties
4985
// ------------------------------------------------------------------
50-
export type XStaticProperties<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>,
51-
ReadonlyOptional extends Record<PropertyKey, unknown> = XReadonlyOptionalProperties<Stack, Root, Properties>,
52-
Optional extends Record<PropertyKey, unknown> = XOptionalProperties<Stack, Root, Properties>,
53-
Result extends Record<PropertyKey, unknown> = ReadonlyOptional & Optional
86+
export type XStaticProperties<Stack extends string[], Root extends XSchema, Schema extends XSchema, Properties extends Record<PropertyKey, XSchema>,
87+
RequiredArray extends string[] = XRequiredArray<Schema>,
88+
// Keys
89+
ReadonlyKeys extends PropertyKey = XReadonlyKeys<Properties>,
90+
OptionalKeys extends PropertyKey = XOptionalKeys<Properties, RequiredArray>,
91+
RequiredKeys extends PropertyKey = XRequiredKeys<Properties, RequiredArray>,
92+
UnknownKeys extends PropertyKey = XUnknownKeys<Properties, RequiredArray>,
93+
// Properties
94+
ReadonlyOptionalProperties extends Record<PropertyKey, unknown> = XReadonlyOptionalProperties<Stack, Root, Extract<OptionalKeys, ReadonlyKeys>, Properties>,
95+
ReadonlyRequiredProperties extends Record<PropertyKey, unknown> = XReadonlyRequiredProperties<Stack, Root, Extract<RequiredKeys, ReadonlyKeys>, Properties>,
96+
OptionalProperties extends Record<PropertyKey, unknown> = XOptionalProperties<Stack, Root, Exclude<OptionalKeys, ReadonlyKeys>, Properties>,
97+
RequiredProperties extends Record<PropertyKey, unknown> = XRequiredProperties<Stack, Root, Exclude<RequiredKeys, ReadonlyKeys>, Properties>,
98+
UnknownProperties extends Record<PropertyKey, unknown> = XUnknownProperties<UnknownKeys>,
99+
// Properties
100+
Result extends Record<PropertyKey, unknown> = (
101+
ReadonlyOptionalProperties &
102+
ReadonlyRequiredProperties &
103+
OptionalProperties &
104+
RequiredProperties &
105+
UnknownProperties
106+
)
54107
> = Result

src/schema/static/required.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,11 @@ THE SOFTWARE.
3131

3232
import type { XSchema } from '../types/schema.ts'
3333
import type { XProperties } from '../types/properties.ts'
34-
import type { XIsReadonly } from './~readonly.ts'
35-
import type { XStaticSchema } from './schema.ts'
3634

37-
// ------------------------------------------------------------------
38-
// ResolveProperties
39-
// ------------------------------------------------------------------
40-
type XResolveProperties<Schema extends XSchema, Result extends Record<PropertyKey, XSchema> = (
41-
Schema extends XProperties<infer Properties extends Record<PropertyKey, XSchema>> ? Properties : {}
42-
)> = Result
43-
// ------------------------------------------------------------------
44-
// FromKey
45-
// ------------------------------------------------------------------
46-
type XFromKey<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Key extends string,
47-
Readonly extends boolean = Key extends keyof Properties ? XIsReadonly<Properties[Key]> : false,
48-
Value extends unknown = Key extends keyof Properties ? XStaticSchema<Stack, Root, Properties[Key]> : unknown,
49-
Result extends Record<PropertyKey, unknown> = (
50-
Readonly extends true
51-
? { readonly [_ in Key]: Value }
52-
: { [_ in Key]: Value }
53-
)> = Result
54-
// ------------------------------------------------------------------
55-
// FromKeys
56-
// ------------------------------------------------------------------
57-
type XFromKeys<Stack extends string[], Root extends XSchema, Properties extends Record<PropertyKey, XSchema>, Keys extends string[], Result extends Record<PropertyKey, unknown> = {}> = (
58-
Keys extends [infer Left extends string, ...infer Right extends string[]]
59-
? XFromKeys<Stack, Root, Properties, Right, Result & XFromKey<Stack, Root, Properties, Left>>
60-
: Result
61-
)
6235
// ------------------------------------------------------------------
6336
// XStaticRequired
6437
// ------------------------------------------------------------------
6538
export type XStaticRequired<Stack extends string[], Root extends XSchema, Schema extends XSchema, Keys extends string[],
66-
Properties extends Record<PropertyKey, XSchema> = XResolveProperties<Schema>,
67-
Result extends Record<PropertyKey, unknown> = XFromKeys<Stack, Root, Properties, Keys>
39+
// note: We only produce an property set if 'required' is present without 'properties'
40+
Result extends Record<PropertyKey, unknown> = Schema extends XProperties ? {} : Record<Keys[number], unknown>
6841
> = Result

src/schema/static/schema.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import type { XRequired } from '../types/required.ts'
4444
import type { XSchema } from '../types/schema.ts'
4545
import type { XType } from '../types/type.ts'
4646
import type { XUnevaluatedProperties } from '../types/unevaluatedProperties.ts'
47-
4847
import type { XStaticAdditionalProperties } from './additionalProperties.ts'
4948
import type { XStaticAllOf } from './allOf.ts'
5049
import type { XStaticAnyOf } from './anyOf.ts'
@@ -69,13 +68,13 @@ type XFromKeywords<Stack extends string[], Root extends XSchema, Schema extends
6968
Schema extends XAllOf<infer Types extends XSchema[]> ? XStaticAllOf<Stack, Root, Types> : unknown,
7069
Schema extends XAnyOf<infer Types extends XSchema[]> ? XStaticAnyOf<Stack, Root, Types> : unknown,
7170
Schema extends XConst<infer Value extends unknown> ? XStaticConst<Value> : unknown,
72-
Schema extends XIf<infer Type extends XSchema> ? XStaticIf<Stack, Root, Schema, Type> :
71+
Schema extends XIf<infer Type extends XSchema> ? XStaticIf<Stack, Root, Schema, Type> : unknown,
7372
Schema extends XEnum<infer Values extends unknown[]> ? XStaticEnum<Values> : unknown,
7473
Schema extends XItems<infer Types extends XSchema[] | XSchema> ? XStaticItems<Stack, Root, Schema, Types> : unknown,
7574
Schema extends XOneOf<infer Types extends XSchema[]> ? XStaticOneOf<Stack, Root, Types> : unknown,
7675
Schema extends XPatternProperties<infer Properties extends Record<PropertyKey, XSchema>> ? XStaticPatternProperties<Stack, Root, Properties> : unknown,
7776
Schema extends XPrefixItems<infer Types extends XSchema[]> ? XStaticPrefixItems<Stack, Root, Schema, Types> : unknown,
78-
Schema extends XProperties<infer Properties extends Record<PropertyKey, XSchema>> ? XStaticProperties<Stack, Root, Properties> : unknown,
77+
Schema extends XProperties<infer Properties extends Record<PropertyKey, XSchema>> ? XStaticProperties<Stack, Root, Schema, Properties> : unknown,
7978
Schema extends XRef<infer Ref extends string> ? XStaticRef<Stack, Root, Ref> : unknown,
8079
Schema extends XRequired<infer Keys extends string[]> ? XStaticRequired<Stack, Root, Schema, Keys> : unknown,
8180
Schema extends XType<infer TypeName extends string[] | string> ? XStaticType<TypeName> : unknown,

src/schema/static/static.ts

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

3232
import type { XSchema } from '../types/schema.ts'
33-
import type { XNonReadonly } from './~non-readonly.ts'
33+
import type { XCanonical } from './~canonical.ts'
3434
import type { XStaticSchema } from './schema.ts'
3535

3636
// ------------------------------------------------------------------
3737
// XStatic
3838
// ------------------------------------------------------------------
3939
export type XStatic<Value extends unknown,
4040
Schema extends XSchema = Value extends XSchema ? Value : {},
41-
NonReadonly extends XSchema = XNonReadonly<Schema>,
42-
Result extends unknown = XStaticSchema<[], NonReadonly, NonReadonly>
41+
Canonical extends XSchema = XCanonical<Schema>,
42+
Result extends unknown = XStaticSchema<[], Canonical, Canonical>
4343
> = Result
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,32 @@ THE SOFTWARE.
2929
// deno-fmt-ignore-file
3030

3131
// ------------------------------------------------------------------
32-
// Tuple
32+
// CanonicalTuple
3333
// ------------------------------------------------------------------
34-
type XFromTuple<Value extends readonly unknown[]> = (
34+
type XCanonicalTuple<Value extends readonly unknown[]> = (
3535
Value extends [infer Left, ...infer Right extends unknown[]]
36-
? [XNonReadonly<Left>, ...XFromTuple<Right>]
36+
? [XCanonical<Left>, ...XCanonicalTuple<Right>]
3737
: []
3838
)
3939
// ------------------------------------------------------------------
40-
// Array
40+
// CanonicalArray
4141
// ------------------------------------------------------------------
42-
type XFromArray<Value extends unknown,
43-
Result extends unknown[] = XNonReadonly<Value>[]
42+
type XCanonicalArray<Value extends unknown,
43+
Result extends unknown[] = XCanonical<Value>[]
4444
> = Result
4545
// ------------------------------------------------------------------
46-
// Object
46+
// CanonicalObject
4747
// ------------------------------------------------------------------
48-
type XFromObject< Value extends object, Result extends Record<PropertyKey, unknown> = {
49-
-readonly [K in keyof Value]: XNonReadonly<Value[K]>
48+
type XCanonicalObject< Value extends object, Result extends Record<PropertyKey, unknown> = {
49+
-readonly [Key in keyof Value]: XCanonical<Value[Key]>
5050
}> = Result
51+
5152
// ------------------------------------------------------------------
52-
// Mutable
53+
// Canonical (i.e. Non-Readonly)
5354
// ------------------------------------------------------------------
54-
export type XNonReadonly<Value extends unknown> = (
55-
Value extends readonly [...infer Schemas extends unknown[]] ? XFromTuple<Schemas> :
56-
Value extends readonly (infer Schema)[] ? XFromArray<Schema> :
57-
Value extends object ? XFromObject<Value> :
58-
Value
55+
export type XCanonical<Schema extends unknown> = (
56+
Schema extends readonly [...infer Schemas extends unknown[]] ? XCanonicalTuple<Schemas> :
57+
Schema extends readonly (infer Schema)[] ? XCanonicalArray<Schema> :
58+
Schema extends object ? XCanonicalObject<Schema> :
59+
Schema
5960
)

src/schema/static/~readonly.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Metrics } from './task/metrics/index.ts'
99
import { Spec } from './task/spec/index.ts'
1010
import { Task } from 'tasksmith'
1111

12-
const Version = '1.1.26'
12+
const Version = '1.1.27'
1313

1414
// ------------------------------------------------------------------
1515
// Build

0 commit comments

Comments
 (0)