Skip to content

Commit

Permalink
Revision 0.34.5 (#1088)
Browse files Browse the repository at this point in the history
* Record Types are not TCompute for TRef Value Type

* Apply Options to Import Target

* ChangeLog

* Version
  • Loading branch information
sinclairzx81 authored Nov 20, 2024
1 parent cc5f2a3 commit 3bd7217
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 93 deletions.
3 changes: 2 additions & 1 deletion changelog/0.34.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.34.0

- [Revision 0.34.5](https://github.com/sinclairzx81/typebox/pull/1088)
- Record Types no longer TCompute for TRef Value Type (Modules)
- [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)
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.4",
"version": "0.34.5",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
20 changes: 15 additions & 5 deletions src/type/module/infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { TIntersect } from '../intersect/index'
import { TIterator } from '../iterator/index'
import { TObject, TProperties } from '../object/index'
import { TOptional } from '../optional/index'
import { TRecord } from '../record/index'
import { TReadonly } from '../readonly/index'
import { TRef } from '../ref/index'
import { TTuple } from '../tuple/index'
Expand Down Expand Up @@ -83,16 +84,16 @@ type TInferIterator<ModuleProperties extends TProperties, Type extends TSchema>
// ------------------------------------------------------------------
// prettier-ignore
type TInferIntersect<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown = unknown> = (
Types extends [infer L extends TSchema, ...infer R extends TSchema[]]
? TInferIntersect<ModuleProperties, R, Result & TInfer<ModuleProperties, L>>
Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]]
? TInferIntersect<ModuleProperties, Right, Result & TInfer<ModuleProperties, Left>>
: Result
)
// ------------------------------------------------------------------
// Object
// ------------------------------------------------------------------
type ReadonlyOptionalPropertyKeys<Properties extends TProperties> = { [K in keyof Properties]: Properties[K] extends TReadonly<TSchema> ? (Properties[K] extends TOptional<Properties[K]> ? K : never) : never }[keyof Properties]
type ReadonlyPropertyKeys<Source extends TProperties> = { [K in keyof Source]: Source[K] extends TReadonly<TSchema> ? (Source[K] extends TOptional<Source[K]> ? never : K) : never }[keyof Source]
type OptionalPropertyKeys<Source extends TProperties> = { [K in keyof Source]: Source[K] extends TOptional<TSchema> ? (Source[K] extends TReadonly<Source[K]> ? never : K) : never }[keyof Source]
type ReadonlyOptionalPropertyKeys<Properties extends TProperties> = { [Key in keyof Properties]: Properties[Key] extends TReadonly<TSchema> ? (Properties[Key] extends TOptional<Properties[Key]> ? Key : never) : never }[keyof Properties]
type ReadonlyPropertyKeys<Source extends TProperties> = { [Key in keyof Source]: Source[Key] extends TReadonly<TSchema> ? (Source[Key] extends TOptional<Source[Key]> ? never : Key) : never }[keyof Source]
type OptionalPropertyKeys<Source extends TProperties> = { [Key in keyof Source]: Source[Key] extends TOptional<TSchema> ? (Source[Key] extends TReadonly<Source[Key]> ? never : Key) : never }[keyof Source]
type RequiredPropertyKeys<Source extends TProperties> = keyof Omit<Source, ReadonlyOptionalPropertyKeys<Source> | ReadonlyPropertyKeys<Source> | OptionalPropertyKeys<Source>>
// prettier-ignore
type InferPropertiesWithModifiers<Properties extends TProperties, Source extends Record<keyof any, unknown>> = Evaluate<(
Expand All @@ -119,6 +120,14 @@ type TInferTuple<ModuleProperties extends TProperties, Types extends TSchema[] ,
: Result
)
// ------------------------------------------------------------------
// Record
// ------------------------------------------------------------------
// prettier-ignore
type TInferRecord<ModuleProperties extends TProperties, Key extends TSchema, Type extends TSchema,
InferredKey extends PropertyKey = TInfer<ModuleProperties, Key> extends infer Key extends PropertyKey ? Key : never,
InferedType extends unknown = TInfer<ModuleProperties, Type>,
> = Record<InferredKey, InferedType>
// ------------------------------------------------------------------
// Ref
// ------------------------------------------------------------------
// prettier-ignore
Expand Down Expand Up @@ -146,6 +155,7 @@ type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (
Type extends TIntersect<infer Types extends TSchema[]> ? TInferIntersect<ModuleProperties, Types> :
Type extends TIterator<infer Type extends TSchema> ? TInferIterator<ModuleProperties, Type> :
Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> :
Type extends TRecord<infer Key extends TSchema, infer Type extends TSchema> ? TInferRecord<ModuleProperties, Key, Type> :
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
Expand Down
3 changes: 2 additions & 1 deletion src/type/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export class TModule<ModuleProperties extends TProperties, ComputedModulePropert
}
/** `[Json]` Imports a Type by Key. */
public Import<Key extends keyof ComputedModuleProperties>(key: Key, options?: SchemaOptions): TImport<ComputedModuleProperties, Key> {
return CreateType({ [Kind]: 'Import', $defs: this.$defs, $ref: key }, options) as never
const $defs = { ...this.$defs, [key]: CreateType(this.$defs[key], options) }
return CreateType({ [Kind]: 'Import', $defs, $ref: key }) as never
}
// prettier-ignore
private WithIdentifiers($defs: ComputedModuleProperties): ComputedModuleProperties {
Expand Down
Loading

0 comments on commit 3bd7217

Please sign in to comment.