Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
---

### Version Updates
- [Revision 1.4.2](https://github.com/sinclairzx81/typebox/pull/1719)
- Ensure Pick and Omit Retain Property Modifiers
- [Revision 1.4.1](https://github.com/sinclairzx81/typebox/pull/1717)
- Faster FNV1A-64 Hashing [#1713](https://github.com/sinclairzx81/typebox/pull/1713)

Expand Down
73 changes: 3 additions & 70 deletions src/type/engine/evaluate/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,82 +31,15 @@ THE SOFTWARE.

import { Guard } from '../../../guard/index.ts'

import { type TReadonly, IsReadonly } from '../../types/_readonly.ts'
import { type TOptional, IsOptional } from '../../types/_optional.ts'

import { type TSchema } from '../../types/schema.ts'
import { type TProperties } from '../../types/properties.ts'
import { type TObject, Object, IsObject } from '../../types/object.ts'
import { type TNever, Never } from '../../types/never.ts'
import { type TTuple, IsTuple } from '../../types/tuple.ts'

import { type TAddReadonly, AddReadonly } from '../../action/_add_readonly.ts'
import { type TAddOptional, AddOptional } from '../../action/_add_optional.ts'
import { type TRemoveReadonly, RemoveReadonly } from '../../action/_remove_readonly.ts'
import { type TRemoveOptional, RemoveOptional } from '../../action/_remove_optional.ts'

import { type TTupleElementsToProperties, TupleElementsToProperties } from '../tuple/to_object.ts'
import { type TEvaluateIntersect, EvaluateIntersect } from './evaluate.ts'
import { type TEvaluatePropertyIntersect, EvaluatePropertyIntersect } from './property.ts'

// ------------------------------------------------------------------
// CanComposite (note: TWith<T, ...> requires infer here. review)
// ------------------------------------------------------------------
/** Returns true if the type is a valid operand to Composite. */
export type TCanComposite<Type extends TSchema> = (
Type extends TObject<infer _ extends TProperties> ? true :
Type extends TTuple<infer _ extends TSchema[]> ? true :
false
)
/** Returns true if the type is a valid operand to Composite. */
export function CanComposite<Type extends TSchema>(type: Type): TCanComposite<Type> {
return (IsObject(type) || IsTuple(type)) as never
}
// ----------------------------------------------------------------------------
// IsReadonlyProperty
// ----------------------------------------------------------------------------
type TIsReadonlyProperty<Left extends TSchema, Right extends TSchema> = (
Left extends TReadonly<Left> ? Right extends TReadonly<Right> ? true : false : false
)
function IsReadonlyProperty<Left extends TSchema, Right extends TSchema>(left: Left, right: Right): TIsReadonlyProperty<Left, Right> {
return (IsReadonly(left) ? IsReadonly(right) ? true : false : false) as never
}
// ----------------------------------------------------------------------------
// IsOptionalProperty
// ----------------------------------------------------------------------------
type TIsOptionalProperty<Left extends TSchema, Right extends TSchema> = (
Left extends TOptional<Left> ? Right extends TOptional<Right> ? true : false : false
)
function IsOptionalProperty<Left extends TSchema, Right extends TSchema>(left: Left, right: Right): TIsOptionalProperty<Left, Right> {
return (IsOptional(left) ? IsOptional(right) ? true : false : false) as never
}
// ----------------------------------------------------------------------------
// CompositeProperty
// ----------------------------------------------------------------------------
type TCompositeProperty<Left extends TSchema, Right extends TSchema,
IsReadonly extends boolean = TIsReadonlyProperty<Left, Right>,
IsOptional extends boolean = TIsOptionalProperty<Left, Right>,
Evaluated extends TSchema = TEvaluateIntersect<[Left, Right]>,
// Modifiers need to be discarded and re-applied
Property extends TSchema = TRemoveReadonly<TRemoveOptional<Evaluated>>
> = (
[IsReadonly, IsOptional] extends [true, true] ? TAddReadonly<TAddOptional<Property>> :
[IsReadonly, IsOptional] extends [true, false] ? TAddReadonly<Property> :
[IsReadonly, IsOptional] extends [false, true] ? TAddOptional<Property> :
Property
)
function CompositeProperty<Left extends TSchema, Right extends TSchema>(left: Left, right: Right): TCompositeProperty<Left, Right> {
const isReadonly = IsReadonlyProperty(left, right)
const isOptional = IsOptionalProperty(left, right)
const evaluated = EvaluateIntersect([left, right]) as TSchema
// Modifiers need to be discarded and re-applied
const property = RemoveReadonly(RemoveOptional(evaluated))
return (
isReadonly && isOptional ? AddReadonly(AddOptional(property)) :
isReadonly && !isOptional ? AddReadonly(property) :
!isReadonly && isOptional ? AddOptional(property) :
property
) as never
}
// ----------------------------------------------------------------------------
// CompositePropertyKey
//
Expand All @@ -119,14 +52,14 @@ function CompositeProperty<Left extends TSchema, Right extends TSchema>(left: Le
type TCompositePropertyKey<Left extends TProperties, Right extends TProperties, Key extends PropertyKey,
Result extends TSchema = (
Key extends keyof Left
? Key extends keyof Right ? TCompositeProperty<Left[Key], Right[Key]> : Left[Key]
? Key extends keyof Right ? TEvaluatePropertyIntersect<Left[Key], Right[Key]> : Left[Key]
: Key extends keyof Right ? Right[Key] : TNever
)
> = Result
function CompositePropertyKey<Left extends TProperties, Right extends TProperties, Key extends PropertyKey>(left: Left, right: Right, key: Key): TCompositePropertyKey<Left, Right, Key> {
return (
key in left
? key in right ? CompositeProperty(left[key], right[key]) : left[key]
? key in right ? EvaluatePropertyIntersect(left[key], right[key]) : left[key]
: key in right ? right[key] : Never()
) as never
}
Expand Down
1 change: 1 addition & 0 deletions src/type/engine/evaluate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export * from './evaluate.ts'
export * from './flatten.ts'
export * from './instantiate.ts'
export * from './narrow.ts'
export * from './property.ts'
16 changes: 15 additions & 1 deletion src/type/engine/evaluate/narrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ THE SOFTWARE.

import { Guard } from '../../../guard/index.ts'
import { type TSchema } from '../../types/schema.ts'
import { type TProperties } from '../../types/properties.ts'
import { type TObject, IsObject } from '../../types/object.ts'
import { type TAny, IsAny } from '../../types/any.ts'
import { type TNever, Never, IsNever } from '../../types/never.ts'
import { type TTuple, IsTuple } from '../../types/tuple.ts'
import { type TUnknown, IsUnknown } from '../../types/unknown.ts'
import { type TCompare, type TCompareResult, Compare, CompareResultLeftInside, CompareResultRightInside, CompareResultEqual } from './compare.ts'
import { type TComposite, type TCanComposite, Composite, CanComposite } from './composite.ts'
import { type TComposite, Composite } from './composite.ts'

// ------------------------------------------------------------------
// NarrowCompareRule
Expand All @@ -57,6 +60,17 @@ function NarrowCompareRule<Left extends TSchema, Right extends TSchema>(left: Le
) as never
}
// ------------------------------------------------------------------
// CanComposite
// ------------------------------------------------------------------
type TCanComposite<Type extends TSchema> = (
Type extends TObject<infer _ extends TProperties> ? true :
Type extends TTuple<infer _ extends TSchema[]> ? true :
false
)
function CanComposite<Type extends TSchema>(type: Type): TCanComposite<Type> {
return (IsObject(type) || IsTuple(type)) as never
}
// ------------------------------------------------------------------
// NarrowCompositeRule
// ------------------------------------------------------------------
type TNarrowCompositeRule<Left extends TSchema, Right extends TSchema,
Expand Down
99 changes: 99 additions & 0 deletions src/type/engine/evaluate/property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*--------------------------------------------------------------------------

TypeBox

The MIT License (MIT)

Copyright (c) 2017-2026 Haydn Paterson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

---------------------------------------------------------------------------*/

// deno-fmt-ignore-file

import { type TSchema } from '../../types/schema.ts'
import { type TOptional, IsOptional } from '../../types/_optional.ts'
import { type TReadonly, IsReadonly } from '../../types/_readonly.ts'
import { type TAddOptionalAction, AddOptionalAction } from '../optional/instantiate_add.ts'
import { type TAddReadonlyAction, AddReadonlyAction } from '../readonly/instantiate_add.ts'
import { type TRemoveOptionalAction, RemoveOptionalAction } from '../optional/instantiate_remove.ts'
import { type TRemoveReadonlyAction, RemoveReadonlyAction } from '../readonly/instantiate_remove.ts'
import { type TEvaluateIntersect, EvaluateIntersect, type TEvaluateUnion, EvaluateUnion } from './evaluate.ts'

// ------------------------------------------------------------------
// EvaluatePropertyIntersect
//
// Evaluates Left and Right as an intersection. It retains Optional
// and Readonly only when both operands have them.
// ------------------------------------------------------------------
export type TEvaluatePropertyIntersect<Left extends TSchema, Right extends TSchema,
IsReadonly extends boolean = Left extends TReadonly ? Right extends TReadonly ? true : false : false,
IsOptional extends boolean = Left extends TOptional ? Right extends TOptional ? true : false : false,
Evaluated extends TSchema = TEvaluateIntersect<[Left, Right]>,
Property extends TSchema = TRemoveReadonlyAction<TRemoveOptionalAction<Evaluated>>
> = (
[IsReadonly, IsOptional] extends [true, true] ? TAddReadonlyAction<TAddOptionalAction<Property>> :
[IsReadonly, IsOptional] extends [true, false] ? TAddReadonlyAction<Property> :
[IsReadonly, IsOptional] extends [false, true] ? TAddOptionalAction<Property> :
Property
)
export function EvaluatePropertyIntersect<Left extends TSchema, Right extends TSchema>(left: Left, right: Right): TEvaluatePropertyIntersect<Left, Right> {
const isReadonly = IsReadonly(left) && IsReadonly(right)
const isOptional = IsOptional(left) && IsOptional(right)
const evaluated = EvaluateIntersect([left, right]) as TSchema
const property = RemoveReadonlyAction(RemoveOptionalAction(evaluated, {}), {})
return (
isReadonly && isOptional ? AddReadonlyAction(AddOptionalAction(property, {}), {}) :
isReadonly ? AddReadonlyAction(property, {}) :
isOptional ? AddOptionalAction(property, {}) :
property
) as never
}
// ------------------------------------------------------------------
// EvaluatePropertyUnion
//
// Evaluates Left and Right as a union after stripping modifiers.
// It reapplies Optional and Readonly if either operand has them.
// ------------------------------------------------------------------
export type TEvaluatePropertyUnion<Left extends TSchema, Right extends TSchema,
IsReadonly extends boolean = Left extends TReadonly ? true : Right extends TReadonly ? true : false,
IsOptional extends boolean = Left extends TOptional ? true : Right extends TOptional ? true : false,
LeftProperty extends TSchema = TRemoveReadonlyAction<TRemoveOptionalAction<Left>>,
RightProperty extends TSchema = TRemoveReadonlyAction<TRemoveOptionalAction<Right>>,
Property extends TSchema = TEvaluateUnion<[LeftProperty, RightProperty]>
> = (
[IsReadonly, IsOptional] extends [true, true] ? TAddReadonlyAction<TAddOptionalAction<Property>> :
[IsReadonly, IsOptional] extends [true, false] ? TAddReadonlyAction<Property> :
[IsReadonly, IsOptional] extends [false, true] ? TAddOptionalAction<Property> :
Property
)
export function EvaluatePropertyUnion<Left extends TSchema, Right extends TSchema>(left: Left, right: Right): TEvaluatePropertyUnion<Left, Right> {
const isReadonly = IsReadonly(left) || IsReadonly(right)
const isOptional = IsOptional(left) || IsOptional(right)
const leftProperty = RemoveReadonlyAction(RemoveOptionalAction(left, {}), {})
const rightProperty = RemoveReadonlyAction(RemoveOptionalAction(right, {}), {})
const property = EvaluateUnion([leftProperty, rightProperty])
return (
isReadonly && isOptional ? AddReadonlyAction(AddOptionalAction(property, {}), {}) :
isReadonly ? AddReadonlyAction(property, {}) :
isOptional ? AddOptionalAction(property, {}) :
property
) as never
}
6 changes: 3 additions & 3 deletions src/type/engine/object/from_intersect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { Guard } from '../../../guard/index.ts'
import { type TSchema } from '../../types/schema.ts'
import { type TProperties } from '../../types/properties.ts'
import { type TFromType, FromType } from './from_type.ts'
import { type TEvaluateIntersect, EvaluateIntersect } from '../evaluate/evaluate.ts'
import { type TEvaluatePropertyIntersect, EvaluatePropertyIntersect } from '../evaluate/property.ts'

// ------------------------------------------------------------------
// CollapseProperties
Expand All @@ -46,7 +46,7 @@ type TCollapseIntersectProperties<Left extends TProperties, Right extends TPrope

LeftProperties extends TProperties = { [Key in LeftKeys]: Left[Key] },
RightProperties extends TProperties = { [Key in RightKeys]: Right[Key] },
SharedProperties extends TProperties = { [Key in SharedKeys]: TEvaluateIntersect<[Left[Key], Right[Key]]> },
SharedProperties extends TProperties = { [Key in SharedKeys]: TEvaluatePropertyIntersect<Left[Key], Right[Key]> },

Unique extends TProperties = Memory.TAssign<LeftProperties, RightProperties>,
Shared extends TProperties = Memory.TAssign<Unique, SharedProperties>
Expand All @@ -59,7 +59,7 @@ function CollapseIntersectProperties<Left extends TProperties, Right extends TPr

const leftProperties = leftKeys.reduce((result, key) => ({ ...result, [key]: left[key] }), {})
const rightProperties = rightKeys.reduce((result, key) => ({ ...result, [key]: right[key] }), {})
const sharedProperties = sharedKeys.reduce((result, key) => ({ ...result, [key]: EvaluateIntersect([left[key], right[key]]) }), {})
const sharedProperties = sharedKeys.reduce((result, key) => ({ ...result, [key]: EvaluatePropertyIntersect(left[key], right[key]) }), {})

const unique = Memory.Assign(leftProperties, rightProperties)
const shared = Memory.Assign(unique, sharedProperties)
Expand Down
6 changes: 3 additions & 3 deletions src/type/engine/object/from_union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { Guard } from '../../../guard/index.ts'
import { type TUnreachable, Unreachable } from '../../../system/unreachable/index.ts'
import { type TSchema } from '../../types/schema.ts'
import { type TProperties } from '../../types/properties.ts'
import { type TEvaluateUnion, EvaluateUnion } from '../evaluate/evaluate.ts'
import { type TEvaluatePropertyUnion, EvaluatePropertyUnion } from '../evaluate/property.ts'
import { type TFromType, FromType } from './from_type.ts'

// ------------------------------------------------------------------
Expand All @@ -41,15 +41,15 @@ import { type TFromType, FromType } from './from_type.ts'
type TCollapseUnionProperties<Left extends TProperties, Right extends TProperties,
SharedKeys extends PropertyKey = keyof Left & keyof Right,
Result extends TProperties = {
[Key in SharedKeys]: TEvaluateUnion<[Left[Key], Right[Key]]>
[Key in SharedKeys]: TEvaluatePropertyUnion<Left[Key], Right[Key]>
}
> = Result
function CollapseUnionProperties<Left extends TProperties, Right extends TProperties>
(left: Left, right: Right):
TCollapseUnionProperties<Left, Right> {
const sharedKeys = Guard.Keys(left).filter((key) => key in right)
const result = sharedKeys.reduce((result, key) => {
return { ...result, [key]: EvaluateUnion([left[key], right[key]]) }
return { ...result, [key]: EvaluatePropertyUnion(left[key], right[key]) }
}, {}) as never
return result as never
}
Expand Down
2 changes: 1 addition & 1 deletion tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as Test from './task/test/index.ts'
import * as Spec from './task/spec/index.ts'
import * as Task from './task/task.ts'

const Version = '1.4.1'
const Version = '1.4.2'

// ------------------------------------------------------------------
// BuildTo
Expand Down
Loading
Loading