Skip to content

Commit

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

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

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({
getTime: Type.Call([], Type.Number()),
})
const SS = Type.ReturnType(C)

type T = Static<typeof T>
console.log(P)
6 changes: 3 additions & 3 deletions src/type/call/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export function Call<T extends unknown[], U extends TSchema>(parameters: [...T],
...options,
[Kind]: 'Call',
call: {
parameters :Clone(parameters),
returns: CloneType(returns)
}
parameters: Clone(parameters),
returns: CloneType(returns),
},
} as never
}
2 changes: 1 addition & 1 deletion src/type/call/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './call'
export * from './call'
1 change: 0 additions & 1 deletion src/type/guard/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ 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
42 changes: 35 additions & 7 deletions src/type/parameters/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,45 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import { type TConst, Const } from '../const'
import { type TTuple, Tuple } from '../tuple/index'
import type { TFunction } from '../function/index'
import type { Ensure } from '../helpers/index'
import { Tuple, type TTuple } from '../tuple/index'
import type { TCall } from '../call/index'
import { CloneRest } from '../clone/type'
import { Clone } from '../clone/value'

// ------------------------------------------------------------------
// Parameters
// TypeGuard
// ------------------------------------------------------------------
export type TParameters<T extends TFunction> = Ensure<TTuple<T['parameters']>>
import { IsCall, IsFunction } from '../guard/kind'

/** `[JavaScript]` Extracts the Parameters from the given Function type */
export function Parameters<T extends TFunction<TSchema[], TSchema>>(schema: T, options: SchemaOptions = {}): TParameters<T> {
return Tuple(CloneRest(schema.parameters), { ...options })
// ------------------------------------------------------------------
// CallParameters
// ------------------------------------------------------------------
// prettier-ignore
type TCallParameters<T extends unknown[], Acc extends TSchema[] = []> =
T extends [infer L extends unknown, ...infer R extends unknown[]]
? TCallParameters<R, [...Acc, TConst<L>]>
: Acc
// prettier-ignore
function CallParameters<T extends unknown[]>(parameters: T): TCallParameters<T> {
return parameters.map(parameter => Const(parameter)) as never
}
// ------------------------------------------------------------------
// Parameters
// ------------------------------------------------------------------
// prettier-ignore
export type TParameters<T extends TSchema> = (
T extends TCall<infer S extends unknown[]> ? TTuple<TCallParameters<S>> :
T extends TFunction<infer S> ? TTuple<S> :
TTuple<[]>
)
/** `[JavaScript]` Extracts the Parameters from the given Function or Call type */
export function Parameters<T extends TSchema>(schema: T, options: SchemaOptions = {}): TParameters<T> {
// prettier-ignore
return (
IsCall(schema) ? Tuple(CallParameters(Clone(schema.call.parameters)), options) :
IsFunction(schema) ? Tuple(CloneRest(schema.parameters), options) :
Tuple([])
) as never
}
33 changes: 26 additions & 7 deletions src/type/return-type/return-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,32 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { SchemaOptions } from '../schema/index'
import type { TFunction } from '../function/index'
import type { TSchema, SchemaOptions } from '../schema/index'
import { type TFunction } from '../function/index'
import { type TCall } from '../call/index'
import { type TNever, Never } from '../never/index'
import { CloneType } from '../clone/type'

export type TReturnType<T extends TFunction> = T['returns']

/** `[JavaScript]` Extracts the ReturnType from the given Function type */
export function ReturnType<T extends TFunction<any[], any>>(schema: T, options: SchemaOptions = {}): TReturnType<T> {
return CloneType(schema.returns, options)
// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
import { IsFunction, IsCall } from '../guard/kind'

// ------------------------------------------------------------------
// ReturnType
// ------------------------------------------------------------------
// prettier-ignore
export type TReturnType<T extends TSchema> =
T extends TFunction<infer _, infer S> ? S :
T extends TCall<infer _, infer S> ? S :
TNever

/** `[JavaScript]` Extracts the ReturnType from the given Function or Call type */
export function ReturnType<T extends TSchema>(schema: T, options: SchemaOptions = {}): TReturnType<T> {
// prettier-ignore
return CloneType((
IsFunction(schema) ? schema.returns :
IsCall(schema) ? schema.call.returns :
Never()
), options) as never
}
6 changes: 3 additions & 3 deletions src/type/type/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder {
return Iterator(items, options)
}
/** `[JavaScript]` Extracts the Parameters from the given Function type */
public Parameters<T extends TFunction<TSchema[], TSchema>>(schema: T, options: SchemaOptions = {}): TParameters<T> {
public Parameters<T extends TSchema>(schema: T, options: SchemaOptions = {}): TParameters<T> {
return Parameters(schema, options)
}
/** `[JavaScript]` Creates a Promise type */
Expand All @@ -105,8 +105,8 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder {
public RegExp(unresolved: string | RegExp, options: RegExpOptions = {}) {
return RegExp(unresolved as any, options)
}
/** `[JavaScript]` Extracts the ReturnType from the given Function type */
public ReturnType<T extends TFunction<any[], any>>(schema: T, options: SchemaOptions = {}): TReturnType<T> {
/** `[JavaScript]` Extracts the ReturnType from the given Function or Call type */
public ReturnType<T extends TSchema>(schema: T, options: SchemaOptions = {}): TReturnType<T> {
return ReturnType(schema, options)
}
/** `[JavaScript]` Creates a Symbol type */
Expand Down

0 comments on commit 752f0a6

Please sign in to comment.