mirror of
https://github.com/zoriya/typebox.git
synced 2026-05-27 16:43:30 +00:00
Revision 0.26.0 (#346)
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { Static, TSchema } from '@sinclair/typebox'
|
||||
|
||||
export function Expect<T extends TSchema>(schema: T) {
|
||||
export function Expect<T extends TSchema, S = Static<T>>(schema: T) {
|
||||
return {
|
||||
ToInfer: <U extends Static<T>>() => {},
|
||||
ToBe: <U extends T>() => {},
|
||||
ToInfer: <U extends S>() => {},
|
||||
ToBe: <U extends S>() => {},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
import { Expect } from './assert'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
Expect(Type.BigInt()).ToInfer<bigint>()
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Expect } from './assert'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
{
|
||||
const A = Type.Object({
|
||||
A: Type.String(),
|
||||
B: Type.String(),
|
||||
})
|
||||
const B = Type.Object({
|
||||
A: Type.Number(),
|
||||
B: Type.Number(),
|
||||
})
|
||||
const T = Type.Composite([A, B])
|
||||
|
||||
Expect(T).ToInfer<{
|
||||
A: string | number
|
||||
B: string | number
|
||||
}>()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Expect } from './assert'
|
||||
|
||||
{
|
||||
const T = Type.Exclude(Type.String(), Type.String())
|
||||
Expect(T).ToBe<never>()
|
||||
}
|
||||
{
|
||||
const T = Type.Exclude(Type.String(), Type.Number())
|
||||
Expect(T).ToBe<string>()
|
||||
}
|
||||
{
|
||||
const T = Type.Exclude(Type.Union([Type.Number(), Type.String(), Type.Boolean()]), Type.Number())
|
||||
Expect(T).ToBe<boolean | string>()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { Expect } from './assert'
|
||||
|
||||
{
|
||||
const T = Type.Extract(Type.String(), Type.String())
|
||||
Expect(T).ToBe<string>()
|
||||
}
|
||||
{
|
||||
const T = Type.Extract(Type.String(), Type.Number())
|
||||
Expect(T).ToBe<never>()
|
||||
}
|
||||
{
|
||||
const T = Type.Extract(Type.Union([Type.Number(), Type.String(), Type.Boolean()]), Type.Number())
|
||||
Expect(T).ToBe<number>()
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
import './any'
|
||||
import './array'
|
||||
import './bigint'
|
||||
import './boolean'
|
||||
import './composite'
|
||||
import './date'
|
||||
import './constructor-parameters'
|
||||
import './constructor'
|
||||
import './emum'
|
||||
import './extract'
|
||||
import './exclude'
|
||||
import './function'
|
||||
import './instance-type'
|
||||
import './intersect'
|
||||
@@ -13,6 +17,7 @@ import './literal'
|
||||
import './modifier'
|
||||
import './namespace'
|
||||
import './never'
|
||||
import './not'
|
||||
import './null'
|
||||
import './number'
|
||||
import './object'
|
||||
@@ -31,6 +36,7 @@ import './required'
|
||||
import './return-type'
|
||||
import './strict'
|
||||
import './string'
|
||||
import './symbol'
|
||||
import './tuple'
|
||||
import './union'
|
||||
import './unknown'
|
||||
|
||||
+15
-12
@@ -1,5 +1,5 @@
|
||||
import { Expect } from './assert'
|
||||
import { Type, TOptional, TString } from '@sinclair/typebox'
|
||||
import { Type, TOptional, TString, Static } from '@sinclair/typebox'
|
||||
|
||||
{
|
||||
const A = Type.Object({
|
||||
@@ -38,14 +38,17 @@ import { Type, TOptional, TString } from '@sinclair/typebox'
|
||||
|
||||
// https://github.com/sinclairzx81/typebox/issues/113
|
||||
// https://github.com/sinclairzx81/typebox/issues/187
|
||||
// {
|
||||
// const A = Type.Object({ A: Type.String() })
|
||||
// const B = Type.Object({ B: Type.String() })
|
||||
// const C = Type.Object({ C: Type.String() })
|
||||
// const T = Type.Intersect([A, Type.Union([B, C])])
|
||||
// type T = Static<typeof T>
|
||||
// const _0: T = { A: '', B: '' }
|
||||
// const _1: T = { A: '', C: '' }
|
||||
// const _3: T = { A: '', B: '', C: '' }
|
||||
// Expect(T).ToBe<{ A: string } & ({ B: string, } | { C: string })>()
|
||||
// }
|
||||
{
|
||||
const A = Type.Object({ A: Type.String() })
|
||||
const B = Type.Object({ B: Type.String() })
|
||||
const C = Type.Object({ C: Type.String() })
|
||||
const T = Type.Intersect([A, Type.Union([B, C])])
|
||||
type T = Static<typeof T>
|
||||
const _0: T = { A: '', B: '' }
|
||||
const _1: T = { A: '', C: '' }
|
||||
const _3: T = { A: '', B: '', C: '' }
|
||||
// invert equivelence (expect true both cases)
|
||||
type T1 = T extends { A: string } & ({ B: string } | { C: string }) ? true : false
|
||||
type T2 = { A: string } & ({ B: string } | { C: string }) extends T ? true : false
|
||||
Expect(T).ToBe<{ A: string } & ({ B: string } | { C: string })>() // solved!
|
||||
}
|
||||
|
||||
@@ -55,3 +55,27 @@ import { Type } from '@sinclair/typebox'
|
||||
)
|
||||
Expect(T).ToInfer<'C'>()
|
||||
}
|
||||
{
|
||||
{
|
||||
const A = Type.Object({ type: Type.Literal('A') })
|
||||
const B = Type.Object({ type: Type.Literal('B') })
|
||||
const C = Type.Object({ type: Type.Literal('C') })
|
||||
const Union = Type.Union([A, B, C])
|
||||
const Extended = Type.Object({
|
||||
x: Type.Number(),
|
||||
y: Type.Number(),
|
||||
z: Type.Number(),
|
||||
})
|
||||
const T = Type.Intersect([Union, Extended])
|
||||
|
||||
const K1 = Type.KeyOf(T)
|
||||
|
||||
Expect(K1).ToInfer<'type' | 'x' | 'y' | 'z'>()
|
||||
|
||||
const P = Type.Omit(T, ['type', 'x'])
|
||||
|
||||
const K2 = Type.KeyOf(P)
|
||||
|
||||
Expect(K2).ToInfer<'y' | 'z'>()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Expect } from './assert'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
{
|
||||
const T = Type.Not(Type.Number(), Type.String())
|
||||
Expect(T).ToInfer<string>()
|
||||
}
|
||||
@@ -54,3 +54,51 @@ import { Type, Static } from '@sinclair/typebox'
|
||||
C: string
|
||||
}>()
|
||||
}
|
||||
{
|
||||
const A = Type.Object({ type: Type.Literal('A') })
|
||||
const B = Type.Object({ type: Type.Literal('B') })
|
||||
const C = Type.Object({ type: Type.Literal('C') })
|
||||
const Union = Type.Union([A, B, C])
|
||||
const Extended = Type.Object({
|
||||
x: Type.Number(),
|
||||
y: Type.Number(),
|
||||
z: Type.Number(),
|
||||
})
|
||||
const T = Type.Intersect([Union, Extended])
|
||||
|
||||
Expect(T).ToInfer<
|
||||
(
|
||||
| {
|
||||
type: 'A'
|
||||
}
|
||||
| {
|
||||
type: 'B'
|
||||
}
|
||||
| {
|
||||
type: 'C'
|
||||
}
|
||||
) & {
|
||||
x: number
|
||||
y: number
|
||||
z: number
|
||||
}
|
||||
>()
|
||||
|
||||
const P = Type.Omit(T, ['type', 'x'])
|
||||
|
||||
Expect(P).ToInfer<
|
||||
({} | {} | {}) & {
|
||||
y: number
|
||||
z: number
|
||||
}
|
||||
>()
|
||||
|
||||
const O = Type.Partial(P)
|
||||
|
||||
Expect(O).ToInfer<
|
||||
({} | {} | {}) & {
|
||||
y?: number | undefined
|
||||
z?: number | undefined
|
||||
}
|
||||
>()
|
||||
}
|
||||
|
||||
@@ -17,3 +17,56 @@ import { Type, Static } from '@sinclair/typebox'
|
||||
C?: string
|
||||
}>()
|
||||
}
|
||||
{
|
||||
{
|
||||
const A = Type.Object({ type: Type.Literal('A') })
|
||||
const B = Type.Object({ type: Type.Literal('B') })
|
||||
const C = Type.Object({ type: Type.Literal('C') })
|
||||
const Union = Type.Union([A, B, C])
|
||||
const Extended = Type.Object({
|
||||
x: Type.Number(),
|
||||
y: Type.Number(),
|
||||
z: Type.Number(),
|
||||
})
|
||||
|
||||
const T = Type.Intersect([Union, Extended])
|
||||
|
||||
Expect(T).ToInfer<
|
||||
(
|
||||
| {
|
||||
type: 'A'
|
||||
}
|
||||
| {
|
||||
type: 'B'
|
||||
}
|
||||
| {
|
||||
type: 'C'
|
||||
}
|
||||
) & {
|
||||
x: number
|
||||
y: number
|
||||
z: number
|
||||
}
|
||||
>()
|
||||
|
||||
const P = Type.Partial(T)
|
||||
|
||||
Expect(P).ToInfer<
|
||||
(
|
||||
| {
|
||||
type?: 'A' | undefined
|
||||
}
|
||||
| {
|
||||
type?: 'B' | undefined
|
||||
}
|
||||
| {
|
||||
type?: 'C' | undefined
|
||||
}
|
||||
) & {
|
||||
x?: number | undefined
|
||||
y?: number | undefined
|
||||
z?: number | undefined
|
||||
}
|
||||
>()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,3 +57,73 @@ import { Type, Static } from '@sinclair/typebox'
|
||||
B: string
|
||||
}>()
|
||||
}
|
||||
{
|
||||
const A = Type.Object({ type: Type.Literal('A') })
|
||||
const B = Type.Object({ type: Type.Literal('B') })
|
||||
const C = Type.Object({ type: Type.Literal('C') })
|
||||
const Union = Type.Union([A, B, C])
|
||||
const Extended = Type.Object({
|
||||
x: Type.Number(),
|
||||
y: Type.Number(),
|
||||
z: Type.Number(),
|
||||
})
|
||||
const T = Type.Intersect([Union, Extended])
|
||||
|
||||
Expect(T).ToInfer<
|
||||
(
|
||||
| {
|
||||
type: 'A'
|
||||
}
|
||||
| {
|
||||
type: 'B'
|
||||
}
|
||||
| {
|
||||
type: 'C'
|
||||
}
|
||||
) & {
|
||||
x: number
|
||||
y: number
|
||||
z: number
|
||||
}
|
||||
>()
|
||||
|
||||
const K = Type.KeyOf(T)
|
||||
|
||||
Expect(K).ToInfer<'type' | 'x' | 'y' | 'z'>()
|
||||
|
||||
const P = Type.Pick(T, ['type', 'x'])
|
||||
|
||||
Expect(P).ToInfer<
|
||||
(
|
||||
| {
|
||||
type: 'A'
|
||||
}
|
||||
| {
|
||||
type: 'B'
|
||||
}
|
||||
| {
|
||||
type: 'C'
|
||||
}
|
||||
) & {
|
||||
x: number
|
||||
}
|
||||
>()
|
||||
|
||||
const O = Type.Partial(P)
|
||||
|
||||
Expect(O).ToInfer<
|
||||
(
|
||||
| {
|
||||
type?: 'A' | undefined
|
||||
}
|
||||
| {
|
||||
type?: 'B' | undefined
|
||||
}
|
||||
| {
|
||||
type?: 'C' | undefined
|
||||
}
|
||||
) & {
|
||||
x?: number | undefined
|
||||
}
|
||||
>()
|
||||
}
|
||||
|
||||
@@ -20,3 +20,57 @@ Expect(Type.RegEx(/foo/)).ToInfer<string>()
|
||||
C: string
|
||||
}>()
|
||||
}
|
||||
{
|
||||
{
|
||||
const A = Type.Object({ type: Type.Literal('A') })
|
||||
const B = Type.Object({ type: Type.Literal('B') })
|
||||
const C = Type.Object({ type: Type.Literal('C') })
|
||||
const Union = Type.Union([A, B, C])
|
||||
const Extended = Type.Object({
|
||||
x: Type.Number(),
|
||||
y: Type.Number(),
|
||||
z: Type.Number(),
|
||||
})
|
||||
const T = Type.Intersect([Union, Extended])
|
||||
|
||||
const P = Type.Partial(T)
|
||||
|
||||
Expect(P).ToInfer<
|
||||
(
|
||||
| {
|
||||
type?: 'A' | undefined
|
||||
}
|
||||
| {
|
||||
type?: 'B' | undefined
|
||||
}
|
||||
| {
|
||||
type?: 'C' | undefined
|
||||
}
|
||||
) & {
|
||||
x?: number | undefined
|
||||
y?: number | undefined
|
||||
z?: number | undefined
|
||||
}
|
||||
>()
|
||||
|
||||
const R = Type.Required(P)
|
||||
|
||||
Expect(R).ToInfer<
|
||||
(
|
||||
| {
|
||||
type: 'A'
|
||||
}
|
||||
| {
|
||||
type: 'B'
|
||||
}
|
||||
| {
|
||||
type: 'C'
|
||||
}
|
||||
) & {
|
||||
x: number
|
||||
y: number
|
||||
z: number
|
||||
}
|
||||
>()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
import { Expect } from './assert'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
Expect(Type.Symbol()).ToInfer<symbol>()
|
||||
Reference in New Issue
Block a user