Skip to content

Commit

Permalink
Propagate This Context Through Check
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed May 4, 2024
1 parent cf09105 commit 645a709
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 112 deletions.
40 changes: 22 additions & 18 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { Type, TypeRegistry, Kind, TSchema, SchemaOptions } from '@sinclair/typebox'
import { Type, Static, 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
// - Propogation of This into nested Constructor / Function revise Compiler implementation.
// - Check.FromThis. Currently checking thisArgs against value for instance
// methods that return 'this'. This is a hack and needs to be revised. Need
// to investigate a Type.This()

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))
const T = Type.Recursive(This => {
return Type.ConstructorCall([], Type.Object({
method: Type.FunctionCall([], This),
}))
})

type T = Static<typeof T>

class Test {
method(): this {
return this
}
}

console.log(Value.Check(T, Foo))
Value.Check(T, Test) // true
5 changes: 2 additions & 3 deletions src/type/function-call/function-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ export interface TFunctionCall<T extends unknown[] = unknown[], U extends TSchem
[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
export function FunctionCall<T extends unknown[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TFunctionCall<T, U> {
return { ...options, [Kind]: 'FunctionCall', functionCall: { parameters, returns: CloneType(returns) } } as never
}
1 change: 0 additions & 1 deletion src/type/guard/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ export function IsFunctionCall(value: unknown): value is TFunctionCall {
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) &&
Expand Down
4 changes: 2 additions & 2 deletions src/type/type/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder {
return BigInt(options)
}
/** `[JavaScript]` Creates a FunctionCall type */
public FunctionCall<T extends unknown[], U extends TSchema>(thisArg: unknown, parameters: [...T], returnType: U, options: SchemaOptions = {}): TFunctionCall<T, U> {
return FunctionCall(thisArg, parameters, returnType, options)
public FunctionCall<T extends unknown[], U extends TSchema>(parameters: [...T], returnType: U, options: SchemaOptions = {}): TFunctionCall<T, U> {
return FunctionCall(parameters, returnType, options)
}
/** `[JavaScript]` Creates a ConstructorCall type */
public ConstructorCall<T extends unknown[], U extends TSchema>(parameters: [...T], returnType: U, options: SchemaOptions = {}): TConstructorCall<T, U> {
Expand Down

0 comments on commit 645a709

Please sign in to comment.