Skip to content

Commit 6ebc3fd

Browse files
committed
Engine Optimization Work
1 parent b6d44c8 commit 6ebc3fd

5 files changed

Lines changed: 93 additions & 73 deletions

File tree

example/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import * as Type from 'typebox'
99
// Options
1010
// Partial
1111
// Required
12+
// KeyOf
1213

13-
const P = Type.Partial(Type.Object({
14+
const P = Type.Object({
1415
x: Type.String(),
1516
y: Type.Number(),
1617
z: Type.Options(Type.Boolean(), { description: 123 })
17-
}))
18+
})
1819

19-
const A = Type.Script(`Partial<{ x: 1, y: 2 }>`)
2020

21-
function test(value: Type.Static<typeof A>) {
21+
function test(value: Type.Static<typeof P>) {
2222

2323
}
2424

src/type/action/keyof.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ THE SOFTWARE.
2626
2727
---------------------------------------------------------------------------*/
2828

29-
// deno-lint-ignore-file ban-types
3029
// deno-fmt-ignore-file
3130

3231
import { type TSchema, type TSchemaOptions } from '../types/schema.ts'
3332
import { type TDeferred, Deferred } from '../types/deferred.ts'
34-
import { type TInstantiate, Instantiate } from '../engine/instantiate.ts'
33+
import { type TKeyOfAction, KeyOfAction } from '../engine/keyof/instantiate.ts'
3534

3635
// ------------------------------------------------------------------
3736
// Deferred
@@ -48,10 +47,6 @@ export function KeyOfDeferred<Type extends TSchema>(type: Type, options: TSchema
4847
// Type
4948
// ------------------------------------------------------------------
5049
/** Applies a KeyOf action to the given type. */
51-
export type TKeyOf<Type extends TSchema> = (
52-
TInstantiate<{}, TKeyOfDeferred<Type>>
53-
)
54-
/** Applies a KeyOf action to the given type. */
55-
export function KeyOf<Type extends TSchema>(type: Type, options: TSchemaOptions = {}): TKeyOf<Type> {
56-
return Instantiate({}, KeyOfDeferred(type, options)) as never
50+
export function KeyOf<Type extends TSchema>(type: Type, options: TSchemaOptions = {}): TKeyOfAction<Type> {
51+
return KeyOfAction(type, options)
5752
}

src/type/engine/keyof/from-type.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*--------------------------------------------------------------------------
2+
3+
TypeBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2017-2026 Haydn Paterson
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
---------------------------------------------------------------------------*/
28+
29+
// deno-fmt-ignore-file
30+
31+
import { type TSchema } from '../../types/schema.ts'
32+
import { type TProperties } from '../../types/properties.ts'
33+
import { type TAny, IsAny } from '../../types/any.ts'
34+
import { type TArray, IsArray } from '../../types/array.ts'
35+
import { type TNever, Never } from '../../types/never.ts'
36+
import { type TObject, IsObject } from '../../types/object.ts'
37+
import { type TRecord, IsRecord } from '../../types/record.ts'
38+
import { type TTuple, IsTuple } from '../../types/tuple.ts'
39+
40+
// ------------------------------------------------------------------
41+
// Computed
42+
// ------------------------------------------------------------------
43+
import { type TFromAny, FromAny } from './from-any.ts'
44+
import { type TFromArray, FromArray } from './from-array.ts'
45+
import { type TFromObject, FromObject } from './from-object.ts'
46+
import { type TFromRecord, FromRecord } from './from-record.ts'
47+
import { type TFromTuple, FromTuple } from './from-tuple.ts'
48+
49+
// ------------------------------------------------------------------
50+
// Action
51+
// ------------------------------------------------------------------
52+
export type TFromType<Type extends TSchema> = (
53+
Type extends TAny ? TFromAny :
54+
Type extends TArray<infer Type extends TSchema> ? TFromArray<Type> :
55+
Type extends TObject<infer Properties extends TProperties> ? TFromObject<Properties> :
56+
Type extends TRecord ? TFromRecord<Type> :
57+
Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<Types> :
58+
TNever
59+
)
60+
export function FromType<Type extends TSchema>(type: Type): TFromType<Type> {
61+
return (
62+
IsAny(type) ? FromAny() :
63+
IsArray(type) ? FromArray(type.items) :
64+
IsObject(type) ? FromObject(type.properties) :
65+
IsRecord(type) ? FromRecord(type) :
66+
IsTuple(type) ? FromTuple(type.items) :
67+
Never()
68+
) as never
69+
}

src/type/engine/keyof/instantiate.ts

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,17 @@ THE SOFTWARE.
3131
import { Memory } from '../../../system/memory/index.ts'
3232
import { type TSchema, type TSchemaOptions } from '../../types/schema.ts'
3333
import { type TProperties } from '../../types/properties.ts'
34-
import { type TAny, IsAny } from '../../types/any.ts'
35-
import { type TArray, IsArray } from '../../types/array.ts'
3634
import { type TCyclic, IsCyclic } from '../../types/cyclic.ts'
3735
import { type TIntersect, IsIntersect } from '../../types/intersect.ts'
38-
import { type TNever, Never } from '../../types/never.ts'
39-
import { type TObject, IsObject } from '../../types/object.ts'
40-
import { type TRecord, IsRecord } from '../../types/record.ts'
41-
import { type TTuple, IsTuple } from '../../types/tuple.ts'
4236
import { type TUnion, IsUnion } from '../../types/union.ts'
4337
import { type TKeyOfDeferred, KeyOfDeferred } from '../../action/keyof.ts'
44-
import { type TState, type TInstantiateType, type TCanInstantiate, InstantiateType, CanInstantiate } from '../instantiate.ts'
45-
38+
import { type TState, type TInstantiateType, type TCanInstantiateFast, InstantiateType, CanInstantiateFast } from '../instantiate.ts'
4639
import { type TCollapseToObject, CollapseToObject } from '../object/index.ts'
4740

4841
// ------------------------------------------------------------------
4942
// Computed
5043
// ------------------------------------------------------------------
51-
import { type TFromAny, FromAny } from './from-any.ts'
52-
import { type TFromArray, FromArray } from './from-array.ts'
53-
import { type TFromObject, FromObject } from './from-object.ts'
54-
import { type TFromRecord, FromRecord } from './from-record.ts'
55-
import { type TFromTuple, FromTuple } from './from-tuple.ts'
44+
import { type TFromType, FromType } from './from-type.ts'
5645

5746
// ------------------------------------------------------------------
5847
//
@@ -67,69 +56,36 @@ import { type TFromTuple, FromTuple } from './from-tuple.ts'
6756
//
6857
// ------------------------------------------------------------------
6958
type TNormalizeType<Type extends TSchema,
70-
Result extends TSchema = (
71-
Type extends TCyclic | TIntersect | TUnion ? TCollapseToObject<Type> :
72-
Type
73-
)
59+
Result extends TSchema = (Type extends TCyclic | TIntersect | TUnion ? TCollapseToObject<Type> : Type)
7460
> = Result
7561
function NormalizeType<Type extends TSchema>(type: Type): TNormalizeType<Type> {
76-
const result = (
77-
IsCyclic(type) || IsIntersect(type) || IsUnion(type) ? CollapseToObject(type) :
78-
type
79-
)
62+
const result = (IsCyclic(type) || IsIntersect(type) || IsUnion(type) ? CollapseToObject(type) : type)
8063
return result as never
8164
}
8265
// ------------------------------------------------------------------
8366
// Action
8467
// ------------------------------------------------------------------
8568
export type TKeyOfAction<Type extends TSchema,
86-
Normal extends TSchema = TNormalizeType<Type>
87-
> = (
88-
Normal extends TAny ? TFromAny :
89-
Normal extends TArray<infer Type extends TSchema> ? TFromArray<Type> :
90-
Normal extends TObject<infer Properties extends TProperties> ? TFromObject<Properties> :
91-
Normal extends TRecord ? TFromRecord<Normal> :
92-
Normal extends TTuple<infer Types extends TSchema[]> ? TFromTuple<Types> :
93-
TNever
94-
)
95-
export function KeyOfAction<Type extends TSchema>(type: Type): TKeyOfAction<Type> {
96-
const normal = NormalizeType(type)
69+
Result extends TSchema = TCanInstantiateFast<[Type]> extends true
70+
? TFromType<TNormalizeType<Type>>
71+
: TKeyOfDeferred<Type>
72+
> = Result
73+
export function KeyOfAction<Type extends TSchema>(type: Type, options: TSchemaOptions): TKeyOfAction<Type> {
9774
return (
98-
IsAny(normal) ? FromAny() :
99-
IsArray(normal) ? FromArray(normal.items) :
100-
IsObject(normal) ? FromObject(normal.properties) :
101-
IsRecord(normal) ? FromRecord(normal) :
102-
IsTuple(normal) ? FromTuple(normal.items) :
103-
Never()
75+
CanInstantiateFast([type])
76+
? Memory.Update(FromType(NormalizeType(type)), {}, options)
77+
: KeyOfDeferred(type, options)
10478
) as never
10579
}
10680
// ------------------------------------------------------------------
107-
// Immediate
81+
// Instantiate
10882
// ------------------------------------------------------------------
109-
export type TKeyOfImmediate<Context extends TProperties, State extends TState, Type extends TSchema,
83+
export type TKeyOfInstantiate<Context extends TProperties, State extends TState, Type extends TSchema,
11084
InstantiatedType extends TSchema = TInstantiateType<Context, State, Type>
11185
> = TKeyOfAction<InstantiatedType>
112-
113-
export function KeyOfImmediate<Context extends TProperties, State extends TState, Type extends TSchema>
114-
(context: Context, state: State, type: Type, options: TSchemaOptions):
115-
TKeyOfImmediate<Context, State, Type> {
116-
const instantiatedType = InstantiateType(context, state, type)
117-
return Memory.Update(KeyOfAction(instantiatedType), {}, options) as never
118-
}
119-
// ------------------------------------------------------------------
120-
// Instantiate
121-
// ------------------------------------------------------------------
122-
export type TKeyOfInstantiate<Context extends TProperties, State extends TState, Type extends TSchema> =
123-
TCanInstantiate<Context, [Type]> extends true
124-
? TKeyOfImmediate<Context, State, Type>
125-
: TKeyOfDeferred<Type>
126-
12786
export function KeyOfInstantiate<Context extends TProperties, State extends TState, Type extends TSchema>
12887
(context: Context, state: State, type: Type, options: TSchemaOptions):
12988
TKeyOfInstantiate<Context, State, Type> {
130-
return (
131-
CanInstantiate(context, [type])
132-
? KeyOfImmediate(context, state, type, options)
133-
: KeyOfDeferred(type, options)
134-
) as never
89+
const instantiatedType = InstantiateType(context, state, type)
90+
return KeyOfAction(instantiatedType, options) as never
13591
}

src/typebox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export { Extract, type TExtract, type TExtractDeferred } from './type/action/ext
5454
export { Index, type TIndex, type TIndexDeferred } from './type/action/index.ts'
5555
export { InstanceType, type TInstanceType, type TInstanceTypeDeferred } from './type/action/instance-type.ts'
5656
export { Interface, type TInterface, type TInterfaceDeferred } from './type/action/interface.ts'
57-
export { KeyOf, type TKeyOf, type TKeyOfDeferred } from './type/action/keyof.ts'
57+
export { KeyOf, type TKeyOfDeferred } from './type/action/keyof.ts'
5858
export { Lowercase, type TLowercase, type TLowercaseDeferred } from './type/action/lowercase.ts'
5959
export { Mapped, type TMapped, type TMappedDeferred } from './type/action/mapped.ts'
6060
export { Module, type TModule, type TModuleDeferred } from './type/action/module.ts'

0 commit comments

Comments
 (0)