{@inheritDoc}
标签类型:内联标签
TSDoc 标准化: 扩展
语法
{@inheritDoc DECLARATION_REFERENCE}
@inheritDoc
标签会自动从另一个声明复制文档内容。 当两个声明具有相同行为时,这将避免重复,因此应具有相同的文档。
内联标签内容是 TSDoc 声明引用,它指定要从中复制的“目标声明”。 目标可以是任何任意声明;它不需要来自基类或实现的接口。 目标声明不是可选的;如果省略它,将报告 ae-unresolved-inheritdoc-base 错误。 目标声明本身可能包含另一个 @inheritDoc
标签;但是,如果链接形成一个循环,则将报告 ae-cyclic-inherit-doc 错误。
用法示例
import { Serializer } from 'example-library';
/**
* An interface describing a widget.
* @public
*/
export interface IWidget {
/**
* Draws the widget on the display surface.
* @param x - the X position of the widget
* @param y - the Y position of the widget
*/
public draw(x: number, y: number): void;
}
/** @public */
export class Button implements IWidget {
/** {@inheritDoc IWidget.draw} */
public draw(x: number, y: number): void {
. . .
}
/**
* {@inheritDoc example-library#Serializer.writeFile}
* @deprecated Use {@link example-library#Serializer.writeFile} instead.
*/
public save(): void {
. . .
}
}
在上面的示例中,Button.draw()
实现 IWidget.draw()
接口契约,因此我们可以使用 {@inheritDoc IWidget.draw}
来避免复制粘贴整个注释块。 注意,Button.save()
从 Serializer.writeFile()
继承其文档,后者具有不同的名称并且从不同的包导入;尽管没有“继承”关系,我们仍然可以从该声明复制文档。
复制的内容
@inheritDoc
标签不会复制整个注释主体。 只有以下组件会被复制
- 摘要部分
@remarks
块@params
块@typeParam
块@returns
块
其他标签,如 @defaultValue
或 @example
不会被复制,需要在 @inheritDoc
标签后显式包含。