13 lines
420 B
TypeScript
13 lines
420 B
TypeScript
import type { Static, TSchema, TSchemaOptions, TUnsafe } from 'typebox'
|
|
import { Unsafe } from 'typebox'
|
|
|
|
export function TypeboxRef<T extends TSchema>(t: T, options: TSchemaOptions = {}): TUnsafe<Static<T>> {
|
|
const id = (t as unknown as Record<string, string | undefined>).$id
|
|
|
|
if (!id) {
|
|
throw new Error('missing ID on schema')
|
|
}
|
|
|
|
return Unsafe<Static<T>>({ ...t, $ref: id, $id: undefined, ...options })
|
|
}
|