Using Typescript types/interfaces as input for Typebox Types #317
-
Hello, I'd like to make a Typebox type based on a Typescript type or an interface. Is it possible? For example, I have the following Typescript type:
I want to have something like:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 20 replies
-
@sherzod-sync Hi, Unfortunately this isn't possible in TypeScript as all type information is erased on compilation. Because type information is erased, there is nothing available to compute the However, there are alternative solutions to this, but each requires custom compiler transformations / plugins. These kinds of solutions usually take the TypeScript Compiler API as a dependency and use it to intercept the compilation to read type information available gathered during a compilation pass. This type information can then be emitted to JavaScript (to be available at runtime) or used to build reflection lookups (which can also be used at runtime) TypeBox doesn't offer this functionality by default, so you will need to type things with the Codegenhttps://github.com/sinclairzx81/typebox/blob/master/codegen/typebox.ts Exampleimport { TypeBoxCodegen } from './path/to/typebox.ts'
const code = TypeBoxCodegen.Generate(`
interface Vector {
x: number,
y: number,
z: number
}
type Vectors = Vector[]
`)
console.log(code)
// outputs
// import { Type, Static } from '@sinclair/typebox'
// type Vector = Static<typeof Vector>
// const Vector = Type.Object({
// x: Type.Number(),
// y: Type.Number(),
// z: Type.Number()
// })
// type Vectors = Static<typeof Vectors>
// const Vectors = Type.Array(Vector) Hope this helps |
Beta Was this translation helpful? Give feedback.
-
@xddq Hi. The code generation modules are not actually part of the published TypeBox package so can't be directly imported with
There hasn't been many changes to the codegen modules in 0.26.0, but I have renamed the modules to make it clear the The current codegens are provided for reference.
|
Beta Was this translation helpful? Give feedback.
-
@xddq Heya. The workbench is just a utility project for the time being (or supplementary development tooling built to find transformation issues (or at least have a way to reason about them)). I've moved the codegen modules over to the workbench for development, but am still syncing them back to this repository.
Sure! would be great to collaborate on some of this. This codegen stuff has been sitting there for a while, and I think it highlights some of the more complex disparities between TS and TB. Would be good to get another set of eyes across it (and your indexer update would be welcome there too). Just drop an issue / PR on the
I'd hold off on deprecating. The workbench is not really a CLI project, it's intended to be more analogous to the TSP. I do think there's value in offering CLI tooling (particularly toolchain integration) so your project is absolutely worth keeping. |
Beta Was this translation helpful? Give feedback.
-
+1 on this. I need to use external json schema definitions(from OCI) and integrate them into an existing typebox project. |
Beta Was this translation helpful? Give feedback.
@sherzod-sync Hi,
Unfortunately this isn't possible in TypeScript as all type information is erased on compilation. Because type information is erased, there is nothing available to compute the
Type.Array(Type.Number())
at runtime.However, there are alternative solutions to this, but each requires custom compiler transformations / plugins. These kinds of solutions usually take the TypeScript Compiler API as a dependency and use it to intercept the compilation to read type information available gathered during a compilation pass. This type information can then be emitted to JavaScript (to be available at runtime) or used to build reflection lookups (which can also be used at runtime)
Type…