Skip to content

Commit

Permalink
Add ConstructorCall and FunctionCall
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed May 4, 2024
1 parent 93a32b0 commit cf09105
Show file tree
Hide file tree
Showing 22 changed files with 435 additions and 65 deletions.
69 changes: 29 additions & 40 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
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
// -----------------------------------------------------------

const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: 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))
import { Type, TypeRegistry, Kind, TSchema, SchemaOptions } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

// Tests:
// - Check
// - Create
// - Transform.Has
// Todo:
// - Propogation of This into nested Constructor / Function calls

export class Foo {
constructor(public a: number) {
console.log('Inside Consructor', a)
}
method(a: number) {
console.log('Inside Method', a, this)
}
}
// prettier-ignore
const T = Type.ConstructorCall([1], Type.Object({
method: Type.FunctionCall(null, [2], Type.Number())
}))

const C = Value.Create(T)

const X = new C(1)
console.log(X.method(12))

console.log(Value.Check(T, Foo))
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.32.27",
"version": "0.32.28",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
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/constructor-call/constructor-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'

// ------------------------------------------------------------------
// StaticConstructorCall
// ------------------------------------------------------------------
type StaticInstanceType<U extends TSchema, P extends unknown[]> = Static<U, P>

// prettier-ignore
type StaticConstructorCall<T extends unknown[], U extends TSchema, P extends unknown[]> = (
Ensure<new (...param: [...T]) => StaticInstanceType<U, P>>
)
// ------------------------------------------------------------------
// TConstructorCall
// ------------------------------------------------------------------
export interface TConstructorCall<T extends unknown[] = unknown[], U extends TSchema = TSchema> extends TSchema {
[Kind]: 'ConstructorCall'
static: StaticConstructorCall<T, U, this['params']>
constructorCall: {
parameters: [...T]
returns: U
}
}
/** `[JavaScript]` Creates a ConstructorCall type */
export function ConstructorCall<T extends unknown[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstructorCall<T, U> {
return {
...options,
[Kind]: 'ConstructorCall',
constructorCall: {
parameters: Clone(parameters),
returns: CloneType(returns),
},
} as never
}
29 changes: 29 additions & 0 deletions src/type/constructor-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 './constructor-call'
59 changes: 59 additions & 0 deletions src/type/function-call/function-call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*--------------------------------------------------------------------------
@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 { Kind } from '../symbols/index'

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

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

// ------------------------------------------------------------------
// TFunctionCall
// ------------------------------------------------------------------
export interface TFunctionCall<T extends unknown[] = unknown[], U extends TSchema = TSchema> extends TSchema {
[Kind]: 'Call'
static: StaticFunctionCall<T, U, this['params']>
functionCall: {
thisArg: unknown
parameters: [...T]
returns: U
}
}
/** `[JavaScript]` Creates a FunctionCall type */
export function FunctionCall<T extends unknown[], U extends TSchema>(thisArg: unknown, parameters: [...T], returns: U, options?: SchemaOptions): TFunctionCall<T, U> {
return { ...options, [Kind]: 'FunctionCall', functionCall: { thisArg, parameters, returns: CloneType(returns) } } as never
}
29 changes: 29 additions & 0 deletions src/type/function-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 './function-call'
10 changes: 10 additions & 0 deletions src/type/guard/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ import type { TAny } from '../any/index'
import type { TAsyncIterator } from '../async-iterator/index'
import type { TBigInt } from '../bigint/index'
import type { TConstructor } from '../constructor/index'
import type { TConstructorCall } from '../constructor-call/index'
import type { TFunction } from '../function/index'
import type { TFunctionCall } from '../function-call/index'
import type { TInteger } from '../integer/index'
import type { TIntersect } from '../intersect/index'
import type { TIterator } from '../iterator/index'
Expand Down Expand Up @@ -94,10 +96,18 @@ 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 TFunctionCall */
export function IsFunctionCall(value: unknown): value is TFunctionCall {
return IsKindOf(value, 'FunctionCall')
}
/** `[Kind-Only]` Returns true if the given value is TConstructor */
export function IsConstructor(value: unknown): value is TConstructor {
return IsKindOf(value, 'Constructor')
}
/** `[Kind-Only]` Returns true if the given value is TConstructorCall */
export function IsConstructorCall(value: unknown): value is TConstructorCall {
return IsKindOf(value, 'ConstructorCall')
}
/** `[Kind-Only]` Returns true if the given value is TDate */
export function IsDate(value: unknown): value is TDate {
return IsKindOf(value, 'Date')
Expand Down
31 changes: 31 additions & 0 deletions src/type/guard/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import type { TAny } from '../any/index'
import type { TAsyncIterator } from '../async-iterator/index'
import type { TBigInt } from '../bigint/index'
import type { TConstructor } from '../constructor/index'
import type { TConstructorCall } from '../constructor-call/index'
import type { TFunction } from '../function/index'
import type { TFunctionCall } from '../function-call/index'
import type { TInteger } from '../integer/index'
import type { TIntersect } from '../intersect/index'
import type { TIterator } from '../iterator/index'
Expand Down Expand Up @@ -228,6 +230,20 @@ export function IsConstructor(value: unknown): value is TConstructor {
IsSchema(value.returns)
)
}
/** Returns true if the given value is TConstructorCall */
export function IsConstructorCall(value: unknown): value is TConstructorCall {
// prettier-ignore
return (
IsKindOf(value, 'ConstructorCall') &&
ValueGuard.HasPropertyKey(value, 'constructorCall') &&
ValueGuard.IsObject(value.constructorCall) && (
ValueGuard.HasPropertyKey(value.constructorCall, 'parameters') &&
ValueGuard.HasPropertyKey(value.constructorCall, 'returns') &&
ValueGuard.IsArray(value.constructorCall.parameters) &&
IsSchema(value.constructorCall.returns)
)
)
}
/** Returns true if the given value is TDate */
export function IsDate(value: unknown): value is TDate {
return (
Expand All @@ -253,6 +269,21 @@ export function IsFunction(value: unknown): value is TFunction {
IsSchema(value.returns)
)
}
/** Returns true if the given value is TFunctionCall */
export function IsFunctionCall(value: unknown): value is TFunctionCall {
// prettier-ignore
return (
IsKindOf(value, 'FunctionCall') &&
ValueGuard.HasPropertyKey(value, 'functionCall') &&
ValueGuard.IsObject(value.functionCall) && (
ValueGuard.HasPropertyKey(value.functionCall, 'thisArg') &&
ValueGuard.HasPropertyKey(value.functionCall, 'parameters') &&
ValueGuard.HasPropertyKey(value.functionCall, 'returns') &&
ValueGuard.IsArray(value.functionCall.parameters) &&
IsSchema(value.functionCall.returns)
)
)
}
/** Returns true if the given value is TInteger */
export function IsInteger(value: unknown): value is TInteger {
return (
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
2 changes: 2 additions & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export * from './clone/index'
export * from './composite/index'
export * from './const/index'
export * from './constructor/index'
export * from './constructor-call/index'
export * from './constructor-parameters/index'
export * from './date/index'
export * from './deref/index'
Expand All @@ -46,6 +47,7 @@ export * from './exclude/index'
export * from './extends/index'
export * from './extract/index'
export * from './function/index'
export * from './function-call/index'
export * from './guard/index'
export * from './helpers/index'
export * from './indexed/index'
Expand Down

0 comments on commit cf09105

Please sign in to comment.