Skip to content

Commit

Permalink
Revision 0.34.6 (#1090)
Browse files Browse the repository at this point in the history
* Add Computed To Type and Kind Guards

* Version

* ChangeLog
  • Loading branch information
sinclairzx81 authored Nov 20, 2024
1 parent 3bd7217 commit 2c5dbab
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions changelog/0.34.0.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### 0.34.0
- [Revision 0.34.6](https://github.com/sinclairzx81/typebox/pull/1090)
- Add Computed To Type and Kind Guards (IsSchema)
- [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)
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.5",
"version": "0.34.6",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
1 change: 1 addition & 0 deletions src/type/guard/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export function IsSchema(value: unknown): value is TSchema {
IsBoolean(value) ||
IsBigInt(value) ||
IsAsyncIterator(value) ||
IsComputed(value) ||
IsConstructor(value) ||
IsDate(value) ||
IsFunction(value) ||
Expand Down
9 changes: 8 additions & 1 deletion src/type/guard/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,13 @@ export function IsBoolean(value: unknown): value is TBoolean {
}
/** Returns true if the given value is TComputed */
export function IsComputed(value: unknown): value is TComputed {
return IsKindOf(value, 'Computed') && IsString(value.target) && ValueGuard.IsArray(value.parameters) && value.parameters.every((schema) => IsSchema(schema))
// prettier-ignore
return (
IsKindOf(value, 'Computed') &&
ValueGuard.IsString(value.target) &&
ValueGuard.IsArray(value.parameters) &&
value.parameters.every((schema) => IsSchema(schema))
)
}
/** Returns true if the given value is TConstructor */
export function IsConstructor(value: unknown): value is TConstructor {
Expand Down Expand Up @@ -606,6 +612,7 @@ export function IsSchema(value: unknown): value is TSchema {
IsBoolean(value) ||
IsBigInt(value) ||
IsAsyncIterator(value) ||
IsComputed(value) ||
IsConstructor(value) ||
IsDate(value) ||
IsFunction(value) ||
Expand Down
4 changes: 2 additions & 2 deletions src/type/module/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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 { Function as FunctionType, type TFunction } from '../function/index'
import { Intersect, type TIntersect, type TIntersectEvaluated } from '../intersect/index'
import { Iterator, type TIterator } from '../iterator/index'
import { KeyOf, type TKeyOf } from '../keyof/index'
Expand Down Expand Up @@ -261,7 +261,7 @@ function FromFunction<ModuleProperties extends TProperties, Parameters extends T
parameters: [...Parameters],
returnType: ReturnType,
): TFromFunction<ModuleProperties, Parameters, ReturnType> {
return Function(FromRest(moduleProperties, parameters as never), FromType(moduleProperties, returnType) as never) as never
return FunctionType(FromRest(moduleProperties, parameters as never), FromType(moduleProperties, returnType) as never) as never
}
// ------------------------------------------------------------------
// Tuple
Expand Down
8 changes: 8 additions & 0 deletions test/runtime/type/guard/kind/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { Type } from '@sinclair/typebox'
import { Assert } from '../../../assert/index'

describe('guard/kind/TComputed', () => {
// ----------------------------------------------------------------
// Schema
// ----------------------------------------------------------------
it('Should guard for Schema', () => {
const T = Type.Partial(Type.Ref('A'))
Assert.IsTrue(KindGuard.IsComputed(T))
Assert.IsTrue(KindGuard.IsSchema(T))
})
// ----------------------------------------------------------------
// Record
// ----------------------------------------------------------------
Expand Down
18 changes: 13 additions & 5 deletions test/runtime/type/guard/type/computed.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import { KindGuard } from '@sinclair/typebox'
import { TypeGuard } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../../assert/index'

describe('guard/type/TComputed', () => {
// ----------------------------------------------------------------
// Schema
// ----------------------------------------------------------------
it('Should guard for Schema', () => {
const T = Type.Partial(Type.Ref('A'))
Assert.IsTrue(TypeGuard.IsComputed(T))
Assert.IsTrue(TypeGuard.IsSchema(T))
})
// ----------------------------------------------------------------
// Record
// ----------------------------------------------------------------
it('Should guard for Record 1', () => {
const T = Type.Record(Type.String(), Type.String())
Assert.IsTrue(KindGuard.IsRecord(T))
Assert.IsTrue(TypeGuard.IsRecord(T))
})
// TRecord<TRecordKey, TRef<...>> is not computed.
it('Should guard for Record 3', () => {
const T = Type.Record(Type.String(), Type.Ref('A'))
Assert.IsTrue(KindGuard.IsRecord(T))
Assert.IsTrue(TypeGuard.IsRecord(T))
})
// TRecord<TRecordKey, TComputed<..., [TRef<...>]> is computed due to interior computed.
it('Should guard for Record 3', () => {
const T = Type.Record(Type.String(), Type.Partial(Type.Ref('A')))
Assert.IsTrue(KindGuard.IsComputed(T))
Assert.IsTrue(TypeGuard.IsComputed(T))
})
// TRecord<TRef<...>, TSchema> is computed as schematics may be transformed to TObject if finite
it('Should guard for Record 4', () => {
const T = Type.Record(Type.Ref('A'), Type.String())
Assert.IsTrue(KindGuard.IsComputed(T))
Assert.IsTrue(TypeGuard.IsComputed(T))
})
})

0 comments on commit 2c5dbab

Please sign in to comment.