Revision 0.32.22 (#840)

* Support Optional and Readonly Function Arguments

* Version
This commit is contained in:
sinclairzx81
2024-04-21 01:28:28 +09:00
committed by GitHub
parent bcee7b5a0f
commit 3719b3eaff
6 changed files with 80 additions and 19 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "@sinclair/typebox",
"version": "0.32.21",
"version": "0.32.22",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@sinclair/typebox",
"version": "0.32.21",
"version": "0.32.22",
"license": "MIT",
"devDependencies": {
"@arethetypeswrong/cli": "^0.13.2",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.32.21",
"version": "0.32.22",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
+17 -8
View File
@@ -29,28 +29,37 @@ 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/index'
import type { TReadonly } from '../readonly/index'
import type { TOptional } from '../optional/index'
import { CloneType, CloneRest } from '../clone/type'
import { Kind } from '../symbols/index'
// ------------------------------------------------------------------
// TConstructorStatic
// StaticConstructor
// ------------------------------------------------------------------
type ConstructorStaticReturnType<T extends TSchema, P extends unknown[]> = Static<T, P>
type StaticReturnType<U extends TSchema, P extends unknown[]> = Static<U, P>
// prettier-ignore
type ConstructorStaticParameters<T extends TSchema[], P extends unknown[], Acc extends unknown[] = []> =
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>?] :
[Static<T, P>]
// prettier-ignore
type StaticParameters<T extends TSchema[], P extends unknown[], Acc extends unknown[] = []> = (
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? ConstructorStaticParameters<R, P, [...Acc, Static<L, P>]>
? StaticParameters<R, P, [...Acc, ...StaticParameter<L, P>]>
: Acc
// prettier-ignore
type ConstructorStatic<T extends TSchema[], U extends TSchema, P extends unknown[]> = (
Ensure<new (...param: ConstructorStaticParameters<T, P>) => ConstructorStaticReturnType<U, P>>
)
// prettier-ignore
type StaticConstructor<T extends TSchema[], U extends TSchema, P extends unknown[]> =
Ensure<new (...params: StaticParameters<T, P>) => StaticReturnType<U, P>>
// ------------------------------------------------------------------
// TConstructor
// ------------------------------------------------------------------
export interface TConstructor<T extends TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema {
[Kind]: 'Constructor'
static: ConstructorStatic<T, U, this['params']>
static: StaticConstructor<T, U, this['params']>
type: 'Constructor'
parameters: T
returns: U
+18 -8
View File
@@ -29,28 +29,38 @@ 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/index'
import type { TReadonly } from '../readonly/index'
import type { TOptional } from '../optional/index'
import { CloneType, CloneRest } from '../clone/type'
import { Kind } from '../symbols/index'
// ------------------------------------------------------------------
// FunctionStatic
// StaticFunction
// ------------------------------------------------------------------
type FunctionStaticReturnType<T extends TSchema, P extends unknown[]> = Static<T, P>
type StaticReturnType<U extends TSchema, P extends unknown[]> = Static<U, P>
// prettier-ignore
type FunctionStaticParameters<T extends TSchema[], P extends unknown[], Acc extends unknown[] = []> =
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>?] :
[Static<T, P>]
// prettier-ignore
type StaticParameters<T extends TSchema[], P extends unknown[], Acc extends unknown[] = []> = (
T extends [infer L extends TSchema, ...infer R extends TSchema[]]
? FunctionStaticParameters<R, P, [...Acc, Static<L, P>]>
? StaticParameters<R, P, [...Acc, ...StaticParameter<L, P>]>
: Acc
// prettier-ignore
type FunctionStatic<T extends TSchema[], U extends TSchema, P extends unknown[]> = (
Ensure<(...param: FunctionStaticParameters<T, P>) => FunctionStaticReturnType<U, P>>
)
// prettier-ignore
type StaticFunction<T extends TSchema[], U extends TSchema, P extends unknown[]> =
Ensure<(...params: StaticParameters<T, P>) => StaticReturnType<U, P>>
// ------------------------------------------------------------------
// TFunction
// ------------------------------------------------------------------
export interface TFunction<T extends TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema {
[Kind]: 'Function'
static: FunctionStatic<T, U, this['params']>
static: StaticFunction<T, U, this['params']>
type: 'Function'
parameters: T
returns: U
+20
View File
@@ -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())
+22
View File
@@ -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())