Replies: 1 comment 1 reply
-
@rolanday Hiya, The jsdoc comment is applied to the top-level type alias only (string, null, union) but not interior type constituents. You can however extract the interior types into their own type aliases and apply comments to those. These can be combined into the finalized type. For example /**
* @description Cluster Unique Identifier
* @pattern '^[a-z0-9]{16}$'
*/
export type Cuid = string;
/**
* @description Nullable Cluster Unique Identifier
*/
export type NullableCuid = Cuid | null
export interface Request {
/**
* @description Optional and Nullable Cluster Unique Identifier
*/
value?: NullableCuid
} Produces import { Type, Static, TypeClone } from '@sinclair/typebox'
export type Cuid = Static<typeof Cuid>
export const Cuid = Type.String({
description: 'Cluster Unique Identifier',
pattern: '^[a-z0-9]{16}$'
})
export type NullableCuid = Static<typeof NullableCuid>
export const NullableCuid = Type.Union([Cuid, Type.Null()], {
description: 'Nullable Cluster Unique Identifier'
})
export type Request = Static<typeof Request>
export const Request = Type.Object({
value: Type.Optional(
TypeClone.Type(NullableCuid, {
description: 'Optional and Nullable Cluster Unique Identifier'
})
)
}) Btw, if you're using the workbench to automate type generation, you may be interested in https://github.com/sinclairzx81/typebox-codegen which provides a API that can be integrated into toolchains (it's what the workbench is using). The workbench was written as a debugger for the codegen, so both give the same output. Hope this helps |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Probably hoping for a lot here, but is it possible to apply codegen attribute targeting a specific union member.
For example, this ...
... generates this ...
The generated output is arguably correct -- expected even -- but what I want, is ...
Seems like a hard problem, and if cannot be solved for the general case 1) is it possible to handle were non-ambiguous (e.g., in above example, since
string | null
, pattern can only apply to string case), or, 2) can TypeBox enforce the rule check with pattern applied toType.Union
?Thanks!!
Beta Was this translation helpful? Give feedback.
All reactions