Skip to content

Commit

Permalink
Add Construct Type
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed May 3, 2024
1 parent 752f0a6 commit 8cae6cd
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 13 deletions.
23 changes: 10 additions & 13 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, KindGuard, Kind, Static, TSchema } from '@sinclair/typebox'

const C = Type.Call(
[{ x: 1, y: 2 }, 2] as const,
Type.Object({
x: Type.Number(),
y: Type.String(),
}),
)

const P = Type.Parameters(C)

const D = Type.Object({
const T = Type.Object({
getTime: Type.Call([], Type.Number()),
})
const SS = Type.ReturnType(C)

console.log(P)
type T = Static<typeof T>

// inferred: type Date = {
// getTime: () => number;
// }

const C = Type.Construct([], Type.String())
console.log(TypeGuard.IsConstruct(C))
console.log(C)
66 changes: 66 additions & 0 deletions src/type/construct/construct.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'

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

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

// ------------------------------------------------------------------
// TConstruct
// ------------------------------------------------------------------
export interface TConstruct<T extends unknown[] = unknown[], U extends TSchema = TSchema> extends TSchema {
[Kind]: 'Construct'
static: StaticConstruct<T, U, this['params']>
construct: {
parameters: [...T]
returns: U
}
}
/** `[JavaScript]` Creates a Construct type */
export function Construct<T extends unknown[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstruct<T, U> {
return {
...options,
[Kind]: 'Construct',
construct: {
parameters: Clone(parameters),
returns: CloneType(returns),
},
} as never
}
29 changes: 29 additions & 0 deletions src/type/construct/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 './construct'
5 changes: 5 additions & 0 deletions src/type/guard/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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 { TConstruct } from '../construct/index'
import type { TConstructor } from '../constructor/index'
import type { TFunction } from '../function/index'
import type { TInteger } from '../integer/index'
Expand Down Expand Up @@ -99,6 +100,10 @@ export function IsBoolean(value: unknown): value is TBoolean {
export function IsCall(value: unknown): value is TCall {
return IsKindOf(value, 'Call')
}
/** `[Kind-Only]` Returns true if the given value is TConstruct */
export function IsConstruct(value: unknown): value is TConstruct {
return IsKindOf(value, 'Construct')
}
/** `[Kind-Only]` Returns true if the given value is TConstructor */
export function IsConstructor(value: unknown): value is TConstructor {
return IsKindOf(value, 'Constructor')
Expand Down
15 changes: 15 additions & 0 deletions src/type/guard/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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 { TConstruct } from '../construct/index'
import type { TConstructor } from '../constructor/index'
import type { TFunction } from '../function/index'
import type { TInteger } from '../integer/index'
Expand Down Expand Up @@ -231,6 +232,20 @@ export function IsCall(value: unknown): value is TCall {
)
)
}
/** Returns true if the given value is TConstruct */
export function IsConstruct(value: unknown): value is TConstruct {
// prettier-ignore
return (
IsKindOf(value, 'Construct') &&
ValueGuard.HasPropertyKey(value, 'construct') &&
ValueGuard.IsObject(value.construct) && (
ValueGuard.HasPropertyKey(value.construct, 'parameters') &&
ValueGuard.HasPropertyKey(value.construct, 'returns') &&
ValueGuard.IsArray(value.construct.parameters) &&
IsSchema(value.construct.returns)
)
)
}
/** Returns true if the given value is TConstructor */
export function IsConstructor(value: unknown): value is TConstructor {
// prettier-ignore
Expand Down
1 change: 1 addition & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export * from './call/index'
export * from './clone/index'
export * from './composite/index'
export * from './const/index'
export * from './construct/index'
export * from './constructor/index'
export * from './constructor-parameters/index'
export * from './date/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 @@ -31,6 +31,7 @@ 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 { Construct, type TConstruct } from '../construct/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 @@ -65,6 +66,10 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder {
public Call<T extends unknown[], U extends TSchema>(parameters: [...T], returnType: U, options: SchemaOptions = {}): TCall<T, U> {
return Call(parameters, returnType, options)
}
/** `[JavaScript]` Creates a Construct type */
public Construct<T extends unknown[], U extends TSchema>(parameters: [...T], returnType: U, options: SchemaOptions = {}): TConstruct<T, U> {
return Construct(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 @@ -38,6 +38,7 @@ export { Boolean } from '../boolean/index'
export { Call } from '../call/index'
export { Composite } from '../composite/index'
export { Const } from '../const/index'
export { Construct } from '../construct/index'
export { Constructor } from '../constructor/index'
export { ConstructorParameters } from '../constructor-parameters/index'
export { Date } from '../date/index'
Expand Down

0 comments on commit 8cae6cd

Please sign in to comment.