Skip to content

Commit

Permalink
Support Optional and Readonly Function Arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 20, 2024
1 parent 1de3543 commit c46dfbf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 10 deletions.
36 changes: 32 additions & 4 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,38 @@ import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, Kind, Static, TSchema } from '@sinclair/typebox'

const F = Type.Constructor([Type.Readonly(Type.Array(Type.String()))], Type.Number())
// -----------------------------------------------------------
// Create: Type
// -----------------------------------------------------------

console.log(F)
const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
})

type X = Static<typeof F>
type T = Static<typeof T>

type A = (x: readonly number[]) => void
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))
6 changes: 3 additions & 3 deletions src/type/constructor/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ THE SOFTWARE.
import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import type { Ensure } from '../helpers/index'
import type { TReadonlyOptional } from '../readonly-optional'
import type { TReadonly } from '../readonly'
import type { TReadonlyOptional } from '../readonly-optional/index'
import type { TReadonly } from '../readonly/index'
import type { TOptional } from '../optional/index'
import { CloneType, CloneRest } from '../clone/type'
import { Kind } from '../symbols/index'
Expand All @@ -43,7 +43,7 @@ type StaticReturnType<U extends TSchema, P extends unknown[]> = Static<U, P>
type StaticParameter<T extends TSchema, P extends unknown[]> =
T extends TReadonlyOptional<T> ? [Readonly<Static<T, P>>?] :
T extends TReadonly<T> ? [Readonly<Static<T, P>>] :
T extends TOptional<T> ? [Static<T, P>] :
T extends TOptional<T> ? [Static<T, P>?] :
[Static<T, P>]
// prettier-ignore
type StaticParameters<T extends TSchema[], P extends unknown[], Acc extends unknown[] = []> = (
Expand Down
6 changes: 3 additions & 3 deletions src/type/function/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ THE SOFTWARE.
import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import type { Ensure } from '../helpers/index'
import type { TReadonlyOptional } from '../readonly-optional'
import type { TReadonly } from '../readonly'
import type { TReadonlyOptional } from '../readonly-optional/index'
import type { TReadonly } from '../readonly/index'
import type { TOptional } from '../optional/index'
import { CloneType, CloneRest } from '../clone/type'
import { Kind } from '../symbols/index'
Expand All @@ -43,7 +43,7 @@ type StaticReturnType<U extends TSchema, P extends unknown[]> = Static<U, P>
type StaticParameter<T extends TSchema, P extends unknown[]> =
T extends TReadonlyOptional<T> ? [Readonly<Static<T, P>>?] :
T extends TReadonly<T> ? [Readonly<Static<T, P>>] :
T extends TOptional<T> ? [Static<T, P>] :
T extends TOptional<T> ? [Static<T, P>?] :
[Static<T, P>]
// prettier-ignore
type StaticParameters<T extends TSchema[], P extends unknown[], Acc extends unknown[] = []> = (
Expand Down
20 changes: 20 additions & 0 deletions test/static/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ import { Type } from '@sinclair/typebox'
}))
Expect(T).ToStatic<new (param_0: number, param_1: string) => { method: new (param_0: number, param_1: string) => boolean }>()
}
{
// readonly-optional
const T = Type.Constructor([Type.ReadonlyOptional(Type.Array(Type.Number()))], Type.Number())
Expect(T).ToStaticDecode<new (param_0?: readonly number[]) => number>()
}
{
// readonly
const T = Type.Constructor([Type.Readonly(Type.Array(Type.Number()))], Type.Number())
Expect(T).ToStaticDecode<new (param_0: readonly number[]) => number>()
}
{
// optional 1
const T = Type.Constructor([Type.Optional(Type.Number())], Type.Number())
Expect(T).ToStaticDecode<new (param_0?: number) => number>()
}
{
// optional 2
const T = Type.Constructor([Type.Number(), Type.Optional(Type.Number())], Type.Number())
Expect(T).ToStaticDecode<new (param_0: number, param_1?: number) => number>()
}
{
// decode 2
const S = Type.Transform(Type.Integer())
Expand Down
22 changes: 22 additions & 0 deletions test/static/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ import { Type } from '@sinclair/typebox'
}))
Expect(T).ToStatic<(param_0: number, param_1: string) => { method: (param_0: number, param_1: string) => boolean }>()
}
{
// readonly-optional
const T = Type.Function([Type.ReadonlyOptional(Type.Array(Type.Number()))], Type.Number())
Expect(T).ToStaticDecode<(param_0?: readonly number[]) => number>()
}
{
// readonly
const T = Type.Function([Type.Readonly(Type.Array(Type.Number()))], Type.Number())
Expect(T).ToStaticDecode<(param_0: readonly number[]) => number>()
}
{
// optional 1
const T = Type.Function([Type.Optional(Type.Number())], Type.Number())
Expect(T).ToStaticDecode<(param_0?: number) => number>()
}
{
// optional 2
const T = Type.Function([Type.Number(), Type.Optional(Type.Number())], Type.Number())
Expect(T).ToStaticDecode<(param_0: number, param_1?: number) => number>()
}
const F = Type.Constructor([Type.Readonly(Type.Array(Type.String()))], Type.Number())

{
// decode 2
const S = Type.Transform(Type.Integer())
Expand Down

0 comments on commit c46dfbf

Please sign in to comment.