Defining a Type from TypeBox and preserve tags #850
-
Hi 👋 , Based on this example:
Is there a way to conserve the tags from the TypeBox Would like to preserve the information defined on the schema like the Thank you! 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
@gonzauy Hello! I don't know of a way to map schema options to JsDoc comments in the type system, but you can apply JsDoc comments manually and have them come through on the static type. import { Type, Static } from '@sinclair/typebox'
export const SchemaTest = Type.Object(
{
/**
* The name of the user
*/
name: Type.String({ description: 'The name of the user' }),
/**
* The age of the user, must be at least 18
* @minimum 18
* @maximum 100
*/
age: Type.Number({
minimum: 18,
maximum: 100,
description: 'The age of the user, must be at least 18',
}),
},
);
export type MyType = Static<typeof SchemaTest>;
function test(value: MyType) {
value.name // hover
value.age // hover
} Unfortunately, this is a bit of duplication, but should largely work. I would be super keen to explore comment mapping should it become possible in the language, but sadly not possible today (afaik) Hope this helps |
Beta Was this translation helpful? Give feedback.
-
@sinclairzx81 , thank you for the answer! |
Beta Was this translation helpful? Give feedback.
-
@gonzauy I just published https://www.npmjs.com/package/@coderspirit/nominal-typebox . For now it only preserves "brands", but I'll be iterating on it to also preserve arbitrary properties (it's based on As a side note, there is already one "property" that can be already preserved:
|
Beta Was this translation helpful? Give feedback.
@gonzauy Hello!
I don't know of a way to map schema options to JsDoc comments in the type system, but you can apply JsDoc comments manually and have them come through on the static type.
TypeScript Link Here