Skip to content

Commit

Permalink
Add Call type
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed May 3, 2024
1 parent 93a32b0 commit 58c081a
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 31 deletions.
35 changes: 5 additions & 30 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, Kind, Static, TSchema } from '@sinclair/typebox'

// -----------------------------------------------------------
// Create: Type
// -----------------------------------------------------------
import { Type, TypeGuard, KindGuard, Kind, Static, TSchema } from '@sinclair/typebox'

const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
x: Type.Call([1, 2], Type.Number()),
y: Type.Call([1, 2], Type.Number()),
z: Type.Call([1, 2], Type.Number())
})

type T = Static<typeof T>

console.log(T)

// -----------------------------------------------------------
// Create: Value
// -----------------------------------------------------------

const V = Value.Create(T)

console.log(V)

// -----------------------------------------------------------
// Compile: Type
// -----------------------------------------------------------

const C = TypeCompiler.Compile(T)

console.log(C.Code())

// -----------------------------------------------------------
// Check: Value
// -----------------------------------------------------------

console.log(C.Check(V))
type T = Static<typeof T>
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ License MIT
- [Conditional](#types-conditional)
- [Intrinsic](#types-intrinsic)
- [Transform](#types-transform)
- [Rest](#types-rest)
- [Invocation](#types-invocation)
- [Guard](#types-guard)
- [Unsafe](#types-unsafe)
- [Strict](#types-strict)
Expand Down
66 changes: 66 additions & 0 deletions src/type/call/call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
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.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import type { Ensure } from '../helpers/index'
import { CloneType } from '../clone/type'
import { Clone } from '../clone/value'
import { Kind } from '../symbols/index'

// ------------------------------------------------------------------
// StaticCall
// ------------------------------------------------------------------
type StaticReturnType<U extends TSchema, P extends unknown[]> = Static<U, P>

// prettier-ignore
type StaticCall<T extends unknown[], U extends TSchema, P extends unknown[]> =
Ensure<(...param: [...T]) => StaticReturnType<U, P>>

// ------------------------------------------------------------------
// TCall
// ------------------------------------------------------------------
export interface TCall<T extends unknown[] = unknown[], U extends TSchema = TSchema> extends TSchema {
[Kind]: 'Call'
static: StaticCall<T, U, this['params']>
call: {
parameters: [...T]
returns: U
}
}
/** `[JavaScript]` Creates a Call type */
export function Call<T extends unknown[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TCall<T, U> {
return {
...options,
[Kind]: 'Call',
call: {
parameters :Clone(parameters),
returns: CloneType(returns)
}
} as never
}
29 changes: 29 additions & 0 deletions src/type/call/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
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.
---------------------------------------------------------------------------*/

export * from './call'
5 changes: 5 additions & 0 deletions src/type/guard/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type { TUnion } from '../union/index'
import type { TAny } from '../any/index'
import type { TAsyncIterator } from '../async-iterator/index'
import type { TBigInt } from '../bigint/index'
import type { TCall } from '../call/index'
import type { TConstructor } from '../constructor/index'
import type { TFunction } from '../function/index'
import type { TInteger } from '../integer/index'
Expand Down Expand Up @@ -94,6 +95,10 @@ export function IsBigInt(value: unknown): value is TBigInt {
export function IsBoolean(value: unknown): value is TBoolean {
return IsKindOf(value, 'Boolean')
}
/** `[Kind-Only]` Returns true if the given value is TCall */
export function IsCall(value: unknown): value is TCall {
return IsKindOf(value, 'Call')
}
/** `[Kind-Only]` Returns true if the given value is TConstructor */
export function IsConstructor(value: unknown): value is TConstructor {
return IsKindOf(value, 'Constructor')
Expand Down
16 changes: 16 additions & 0 deletions src/type/guard/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type { TUnion } from '../union/index'
import type { TAny } from '../any/index'
import type { TAsyncIterator } from '../async-iterator/index'
import type { TBigInt } from '../bigint/index'
import type { TCall } from '../call/index'
import type { TConstructor } from '../constructor/index'
import type { TFunction } from '../function/index'
import type { TInteger } from '../integer/index'
Expand Down Expand Up @@ -67,6 +68,7 @@ import type { TVoid } from '../void/index'
import type { TDate } from '../date/index'
import type { TThis } from '../recursive/index'


export class TypeGuardUnknownTypeError extends TypeBoxError {}

const KnownTypes = [
Expand Down Expand Up @@ -216,6 +218,20 @@ export function IsBoolean(value: unknown): value is TBoolean {
IsOptionalString(value.$id)
)
}
/** Returns true if the given value is TCall */
export function IsCall(value: unknown): value is TCall {
// prettier-ignore
return (
IsKindOf(value, 'Call') &&
ValueGuard.HasPropertyKey(value, 'call') &&
ValueGuard.IsObject(value.call) && (
ValueGuard.HasPropertyKey(value.call, 'parameters') &&
ValueGuard.HasPropertyKey(value.call, 'returns') &&
ValueGuard.IsArray(value.call.parameters) &&
IsSchema(value.call.returns)
)
)
}
/** Returns true if the given value is TConstructor */
export function IsConstructor(value: unknown): value is TConstructor {
// prettier-ignore
Expand Down
4 changes: 4 additions & 0 deletions src/type/guard/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

/** Returns true if this value is an object with the given property key */
export function HasPropertyKey<K extends PropertyKey>(value: unknown, key: K): value is Record<PropertyKey, unknown> & { [_ in K]: unknown } {
return IsObject(value) && key in value
}
/** Returns true if this value is an async iterator */
export function IsAsyncIterator(value: unknown): value is AsyncIterableIterator<unknown> {
return IsObject(value) && !IsArray(value) && !IsUint8Array(value) && Symbol.asyncIterator in value
Expand Down
1 change: 1 addition & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export * from './async-iterator/index'
export * from './awaited/index'
export * from './bigint/index'
export * from './boolean/index'
export * from './call/index'
export * from './clone/index'
export * from './composite/index'
export * from './const/index'
Expand Down
5 changes: 5 additions & 0 deletions src/type/type/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { JsonTypeBuilder } from './json'
import { AsyncIterator, type TAsyncIterator } from '../async-iterator/index'
import { Awaited, type TAwaited } from '../awaited/index'
import { BigInt, type TBigInt, type BigIntOptions } from '../bigint/index'
import { Call, type TCall } from '../call/index'
import { Constructor, type TConstructor } from '../constructor/index'
import { ConstructorParameters, type TConstructorParameters } from '../constructor-parameters/index'
import { Date, type TDate, type DateOptions } from '../date/index'
Expand Down Expand Up @@ -60,6 +61,10 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder {
public BigInt(options: BigIntOptions = {}): TBigInt {
return BigInt(options)
}
/** `[JavaScript]` Creates a Call type */
public Call<T extends unknown[], U extends TSchema>(parameters: [...T], returnType: U, options: SchemaOptions = {}): TCall<T, U> {
return Call(parameters, returnType, options)
}
/** `[JavaScript]` Extracts the ConstructorParameters from the given Constructor type */
public ConstructorParameters<T extends TConstructor<TSchema[], TSchema>>(schema: T, options: SchemaOptions = {}): TConstructorParameters<T> {
return ConstructorParameters(schema, options)
Expand Down
1 change: 1 addition & 0 deletions src/type/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export { AsyncIterator } from '../async-iterator/index'
export { Awaited } from '../awaited/index'
export { BigInt } from '../bigint/index'
export { Boolean } from '../boolean/index'
export { Call } from '../call/index'
export { Composite } from '../composite/index'
export { Const } from '../const/index'
export { Constructor } from '../constructor/index'
Expand Down

0 comments on commit 58c081a

Please sign in to comment.