Skip to content

Commit

Permalink
Revision 0.34.4 (#1085)
Browse files Browse the repository at this point in the history
* Module Dereference Path for TEnum

* Version

* ChangeLog
  • Loading branch information
sinclairzx81 authored Nov 19, 2024
1 parent ab4f0db commit cc5f2a3
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
2 changes: 2 additions & 0 deletions changelog/0.34.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### 0.34.0

- [Revision 0.34.4](https://github.com/sinclairzx81/typebox/pull/1085)
- Inference Path for Enum within Modules
- [Revision 0.34.3](https://github.com/sinclairzx81/typebox/pull/1083)
- Retain Array Elements on Value Default
- [Revision 0.34.2](https://github.com/sinclairzx81/typebox/pull/1082)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.34.3",
"version": "0.34.4",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
32 changes: 17 additions & 15 deletions src/type/module/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { AsyncIterator, type TAsyncIterator } from '../async-iterator/index'
import { TComputed } from '../computed/index'
import { Constructor, type TConstructor } from '../constructor/index'
import { Index, type TIndex } from '../indexed/index'
import { TEnum, TEnumRecord } from '../enum/index'
import { Function, type TFunction } from '../function/index'
import { Intersect, type TIntersect, type TIntersectEvaluated } from '../intersect/index'
import { Iterator, type TIterator } from '../iterator/index'
Expand Down Expand Up @@ -339,34 +340,35 @@ function FromRest<ModuleProperties extends TProperties, Types extends TSchema[]>
// ------------------------------------------------------------------
// prettier-ignore
export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (
Type extends TComputed<infer T extends string, infer S extends TSchema[]> ? TFromComputed<ModuleProperties, T, S> :
Type extends TObject<infer S extends TProperties> ? TFromObject<ModuleProperties, S> :
Type extends TConstructor<infer S extends TSchema[], infer R extends TSchema> ? TFromConstructor<ModuleProperties, S, R> :
Type extends TFunction<infer S extends TSchema[], infer R extends TSchema> ? TFromFunction<ModuleProperties, S, R> :
Type extends TTuple<infer S extends TSchema[]> ? TFromTuple<ModuleProperties, S> :
Type extends TIntersect<infer S extends TSchema[]> ? TFromIntersect<ModuleProperties, S> :
Type extends TUnion<infer S extends TSchema[]> ? TFromUnion<ModuleProperties, S> :
Type extends TArray<infer S extends TSchema> ? TFromArray<ModuleProperties, S> :
Type extends TAsyncIterator<infer S extends TSchema> ? TFromAsyncIterator<ModuleProperties, S> :
Type extends TIterator<infer S extends TSchema> ? TFromIterator<ModuleProperties, S> :
Type extends TArray<infer Type extends TSchema> ? TFromArray<ModuleProperties, Type> :
Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<ModuleProperties, Type> :
Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<ModuleProperties, Target, Parameters> :
Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<ModuleProperties, Parameters, InstanceType> :
Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<ModuleProperties, Parameters, ReturnType> :
Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<ModuleProperties, Types> :
Type extends TIterator<infer Type extends TSchema> ? TFromIterator<ModuleProperties, Type> :
Type extends TObject<infer Properties extends TProperties> ? TFromObject<ModuleProperties, Properties> :
Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<ModuleProperties, Types> :
Type extends TEnum<infer _ extends TEnumRecord> ? Type : // intercept enum before union
Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<ModuleProperties, Types> :
Type
)
// prettier-ignore
export function FromType<ModuleProperties extends TProperties, Type extends TSchema>(moduleProperties: ModuleProperties, type: Type): TFromType<ModuleProperties, Type> {
return (
KindGuard.IsArray(type) ? CreateType(FromArray(moduleProperties, type.items), type) :
KindGuard.IsAsyncIterator(type) ? CreateType(FromAsyncIterator(moduleProperties, type.items), type) :
// Note: The 'as never' is required due to excessive resolution of TIndex. In fact TIndex, TPick, TOmit and
// all need re-implementation to remove the PropertyKey[] selector. Reimplementation of these types should
// be a priority as there is a potential for the current inference to break on TS compiler changes.
KindGuard.IsComputed(type) ? CreateType(FromComputed(moduleProperties, type.target, type.parameters) as never) :
KindGuard.IsObject(type) ? CreateType(FromObject(moduleProperties, type.properties), type) :
KindGuard.IsConstructor(type) ? CreateType(FromConstructor(moduleProperties, type.parameters, type.returns), type) :
KindGuard.IsFunction(type) ? CreateType(FromFunction(moduleProperties, type.parameters, type.returns), type) :
KindGuard.IsTuple(type)? CreateType(FromTuple(moduleProperties, type.items || []), type) :
KindGuard.IsIntersect(type) ? CreateType(FromIntersect(moduleProperties, type.allOf), type) :
KindGuard.IsUnion(type) ? CreateType(FromUnion(moduleProperties, type.anyOf), type) :
KindGuard.IsArray(type) ? CreateType(FromArray(moduleProperties, type.items), type) :
KindGuard.IsAsyncIterator(type) ? CreateType(FromAsyncIterator(moduleProperties, type.items), type) :
KindGuard.IsIterator(type) ? CreateType(FromIterator(moduleProperties, type.items), type) :
KindGuard.IsObject(type) ? CreateType(FromObject(moduleProperties, type.properties), type) :
KindGuard.IsTuple(type)? CreateType(FromTuple(moduleProperties, type.items || []), type) :
KindGuard.IsUnion(type) ? CreateType(FromUnion(moduleProperties, type.anyOf), type) :
type
) as never
}
Expand Down
2 changes: 2 additions & 0 deletions src/type/module/infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { TSchema } from '../schema/index'
import { TArray } from '../array/index'
import { TAsyncIterator } from '../async-iterator/index'
import { TConstructor } from '../constructor/index'
import { TEnum, TEnumRecord } from '../enum/index'
import { TFunction } from '../function/index'
import { TIntersect } from '../intersect/index'
import { TIterator } from '../iterator/index'
Expand Down Expand Up @@ -147,6 +148,7 @@ type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (
Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> :
Type extends TRef<infer Ref extends string> ? TInferRef<ModuleProperties, Ref> :
Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> :
Type extends TEnum<infer _ extends TEnumRecord> ? Static<Type> : // intercept enum before union
Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> :
Static<Type>
)
Expand Down
40 changes: 40 additions & 0 deletions test/static/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Expect } from './assert'
import { Type } from '@sinclair/typebox'

// ------------------------------------------------------------------
// Enum 1
// ------------------------------------------------------------------
{
enum Enum {
A,
B,
}

const T = Type.Module({
T: Type.Object({
value: Type.Enum(Enum),
}),
}).Import('T')

Expect(T).ToStatic<{
value: Enum
}>()
}

// ------------------------------------------------------------------
// Enum 2
// ------------------------------------------------------------------
{
const T = Type.Module({
T: Type.Object({
value: Type.Enum({
x: 1,
y: 2,
}),
}),
}).Import('T')

Expect(T).ToStatic<{
value: 1 | 2
}>()
}
1 change: 1 addition & 0 deletions test/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import './enum'
import './extract'
import './exclude'
import './function'
import './import'
import './indexed'
import './instance-type'
import './intersect'
Expand Down

0 comments on commit cc5f2a3

Please sign in to comment.