Skip to content

Commit 54af135

Browse files
committed
Introduce preservedTypeAnnotationTags option
Resolves #3020 Total time spent: 32 minutes
1 parent bd7888a commit 54af135

File tree

19 files changed

+110
-22
lines changed

19 files changed

+110
-22
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ title: Changelog
44

55
## Unreleased
66

7+
### Features
8+
9+
- Introduced the `preservedTypeAnnotationTags` option to specify tags whose type annotations should
10+
be copied to the output documentation, #3020.
11+
API: Introduced `typeAnnotation` on `CommentTag`
12+
713
## v0.28.13 (2025-09-14)
814

915
### Features

site/options/comments.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ Note that `@deprecated` is a block tag, not a modifier tag, so should not be spe
190190
## excludeTags
191191

192192
```bash
193-
$ typedoc --excludeTags apidefine
193+
$ typedoc --excludeTags @apidefine
194194
```
195195

196196
Specify tags that should be removed from doc comments when parsing.
@@ -199,14 +199,26 @@ Useful if your project uses [apiDoc](https://apidocjs.com/) for documenting REST
199199
## notRenderedTags
200200

201201
```bash
202-
$ typedoc --notRenderedTags beta
202+
$ typedoc --notRenderedTags @beta
203203
```
204204

205205
Specify tags which should be preserved in the doc comments, but not rendered
206206
when creating output. This is intended to support tags which carry some meaning
207207
about how to render a member or instructions for TypeDoc to do something after a
208208
package has been deserialized from JSON in packages mode.
209209

210+
## preservedTypeAnnotationTags
211+
212+
```json
213+
// typedoc.json
214+
{
215+
"preservedTypeAnnotationTags": ["@fires"]
216+
}
217+
```
218+
219+
Specify block tags whose type annotations should be preserved by TypeDoc's parser,
220+
leading to their content being included in the rendered documentation.
221+
210222
## externalSymbolLinkMappings
211223

212224
```json

site/options/package-options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ at the root level. The following tables indicate where an option should be set.
121121
| [`cascadedModifierTags`](comments.md#cascadedmodifiertags) | Package | |
122122
| [`excludeTags`](comments.md#excludetags) | Package | |
123123
| [`notRenderedTags`](comments.md#notrenderedtags) | Root | |
124+
| [`preservedTypeAnnotationTags`](comments.md#preservedtypeannotationtags) | Package | |
124125
| [`externalSymbolLinkMappings`](comments.md#externalsymbollinkmappings) | Both | Unresolved links are checked both when converting and when merging projects |
125126

126127
## Organization Options

src/lib/converter/comments/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface CommentParserConfig {
1919
blockTags: Set<string>;
2020
inlineTags: Set<string>;
2121
modifierTags: Set<string>;
22+
preservedTypeAnnotationTags: Set<string>;
2223
jsDocCompatibility: JsDocCompatibility;
2324
suppressCommentWarningsInDeclarationFiles: boolean;
2425
useTsLinkResolution: boolean;

src/lib/converter/comments/parser.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,22 @@ function blockTag(
380380
let content: CommentDisplayPart[];
381381
if (tagName === "@example") {
382382
return exampleBlock(comment, lexer, config, i18n, warning, files);
383-
} else if (
383+
}
384+
385+
let typeAnnotation: string | undefined;
386+
if (
387+
!lexer.done() &&
388+
config.preservedTypeAnnotationTags.has(tagName)
389+
) {
390+
if (lexer.peek().kind === TokenSyntaxKind.Text && /^\s+$/.test(lexer.peek().text)) {
391+
lexer.take();
392+
}
393+
if (lexer.peek().kind === TokenSyntaxKind.TypeAnnotation) {
394+
typeAnnotation = lexer.take().text;
395+
}
396+
}
397+
398+
if (
384399
["@default", "@defaultValue"].includes(tagName) &&
385400
config.jsDocCompatibility.defaultTag
386401
) {
@@ -396,7 +411,11 @@ function blockTag(
396411
content = blockContent(comment, lexer, config, i18n, warning, files);
397412
}
398413

399-
return new CommentTag(tagName as TagString, content);
414+
const tag = new CommentTag(tagName as TagString, content);
415+
if (typeAnnotation) {
416+
tag.typeAnnotation = typeAnnotation;
417+
}
418+
return tag;
400419
}
401420

402421
/**

src/lib/converter/converter.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -785,12 +785,9 @@ export class Converter extends AbstractComponent<Application, ConverterEvents> {
785785
private _buildCommentParserConfig() {
786786
this._config = {
787787
blockTags: new Set(this.application.options.getValue("blockTags")),
788-
inlineTags: new Set(
789-
this.application.options.getValue("inlineTags"),
790-
),
791-
modifierTags: new Set(
792-
this.application.options.getValue("modifierTags"),
793-
),
788+
inlineTags: new Set(this.application.options.getValue("inlineTags")),
789+
modifierTags: new Set(this.application.options.getValue("modifierTags")),
790+
preservedTypeAnnotationTags: new Set(this.application.options.getValue("preservedTypeAnnotationTags")),
794791
jsDocCompatibility: this.application.options.getValue("jsDocCompatibility"),
795792
suppressCommentWarningsInDeclarationFiles: this.application.options.getValue(
796793
"suppressCommentWarningsInDeclarationFiles",

src/lib/internationalization/locales/en.cts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ export = {
316316
help_excludeTags: "Remove the listed block/modifier tags from doc comments",
317317
help_notRenderedTags: "Tags which will be preserved in doc comments, but not rendered when creating output",
318318
help_cascadedModifierTags: "Modifier tags which should be copied to all children of the parent reflection",
319+
help_preservedTypeAnnotationTags: "Block tags whose type annotations should be preserved in the output.",
319320
help_readme:
320321
"Path to the readme file that should be displayed on the index page. Pass `none` to disable the index page and start the documentation on the globals page",
321322
help_basePath: "Specifies a path which links may be resolved relative to.",

src/lib/models/Comment.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ export class CommentTag {
8888
*/
8989
name?: string;
9090

91+
/**
92+
* Optional type annotation associated with this tag. TypeDoc will remove type annotations unless explicitly
93+
* requested by the user with the `preservedTypeAnnotationTags` option.
94+
*/
95+
typeAnnotation?: string;
96+
9197
/**
9298
* The actual body text of this tag.
9399
*/
@@ -130,6 +136,9 @@ export class CommentTag {
130136
if (this.name) {
131137
tag.name = this.name;
132138
}
139+
if (this.typeAnnotation) {
140+
tag.typeAnnotation = this.typeAnnotation;
141+
}
133142
return tag;
134143
}
135144

@@ -138,12 +147,14 @@ export class CommentTag {
138147
tag: this.tag,
139148
name: this.name,
140149
content: Comment.serializeDisplayParts(this.content),
150+
typeAnnotation: this.typeAnnotation,
141151
};
142152
}
143153

144154
fromObject(de: Deserializer, obj: JSONOutput.CommentTag) {
145155
// tag already set by Comment.fromObject
146156
this.name = obj.name;
157+
this.typeAnnotation = obj.typeAnnotation;
147158
this.content = Comment.deserializeDisplayParts(de, obj.content);
148159
}
149160
}

src/lib/output/themes/default/partials/comment.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export function commentTags(context: DefaultThemeRenderContext, props: Reflectio
8383
{name}
8484
{anchorIcon(context, anchor)}
8585
</h4>
86+
{item.typeAnnotation && <span class="tsd-type-annotation">{item.typeAnnotation}</span>}
8687
<JSX.Raw html={context.markdown(item.content)} />
8788
</div>
8889
</>

src/lib/serialization/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export interface Comment extends Partial<S<M.Comment, "blockTags" | "label">> {
369369
}
370370

371371
/** @category Comments */
372-
export interface CommentTag extends S<M.CommentTag, "tag" | "name"> {
372+
export interface CommentTag extends S<M.CommentTag, "tag" | "name" | "typeAnnotation"> {
373373
content: CommentDisplayPart[];
374374
}
375375

0 commit comments

Comments
 (0)