Files
typebox/test/static/recursive.ts
glm-5.1 bd758c2342 Fork from @sinclair/typebox 0.34.49 as @alkdev/typebox
- Rename package from @sinclair/typebox to @alkdev/typebox
- Update author, repository, and homepage to alkdev
- Remove GitHub workflows, .vscode config, and branding assets
- Update all source, test, example, changelog, and task imports
- Update tsconfig.json path mappings
- Clean up readme header (remove upstream badges/branding)
2026-04-23 13:22:31 +00:00

101 lines
1.9 KiB
TypeScript

import { Static, Type } from '@alkdev/typebox'
import { Expect } from './assert'
{
// identity
const R = Type.Recursive((Node) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(Node),
}),
)
type T = Static<typeof R>
Expect(R).ToStatic<{ id: string; nodes: T[] }>()
}
{
// keyof
const R = Type.Recursive((Node) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(Node),
}),
)
const T = Type.KeyOf(R)
Expect(T).ToStatic<'id' | 'nodes'>()
}
{
// partial
const R = Type.Recursive((Node) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(Node),
}),
)
const T = Type.Partial(R)
Expect(T).ToStatic<{
id?: string | undefined
nodes?: Static<typeof T>[] | undefined
}>()
}
{
// required
const R = Type.Recursive((Node) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(Node),
}),
)
const P = Type.Partial(R)
const T = Type.Required(P)
Expect(T).ToStatic<{
id: string
nodes: Static<typeof T>[]
}>()
}
{
// pick
const R = Type.Recursive((Node) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(Node),
}),
)
const T = Type.Pick(R, ['id'])
Expect(T).ToStatic<{
id: string
}>()
}
{
// omit
const R = Type.Recursive((Node) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(Node),
}),
)
const T = Type.Omit(R, ['id'])
Expect(T).ToStatic<{
nodes: Static<typeof T>[]
}>()
}
// prettier-ignore
{
// issue: https://github.com/sinclairzx81/typebox/issues/336
type JSONValue =
| string
| number
| null
| boolean
| { [x: string]: JSONValue }
| JSONValue[]
const R = Type.Recursive((Node) => Type.Union([
Type.Null(),
Type.String(),
Type.Number(),
Type.Boolean(),
Type.Record(Type.String(), Node),
Type.Array(Node)
]))
Expect(R).ToStatic<JSONValue>()
}