You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeDoc中无需重写方法,能否覆写子类继承的父类方法文档注释?

在TypeDoc中为子类继承的方法替换文档注释(无需重写方法)

问题描述

能否在TypeDoc中为子类从父类继承的方法添加文档注释,且不用在子类里重写该方法的实现?我想在不编写重写代码的前提下,替换掉继承来的文档注释。

示例代码:

class Parent<T>
{
    readonly #tProp: T;

    public constructor(tArg: T)
    {
        this.#tProp = tArg;
    }

    /** Getter for the tProp */
    public tProp(): T { return this.#tProp; }
}

/*
 * 是否可以无需重写tProp getter的实现,就为从Parent类继承的tProp getter添加文档注释来替换原注释?
 */
class Child extends Parent<string>
{
    public constructor(stringProp: string)
    {
        super(stringProp);
    }
}

解决方案

可以实现,不需要重写方法的具体逻辑,只需在子类中用JSDoc注释声明该继承成员,就能覆盖父类的文档注释。

方法1:直接覆盖文档

在子类中针对目标成员添加自定义JSDoc注释,并用类型断言(!:)声明其类型,无需编写方法体:

class Child extends Parent<string>
{
    public constructor(stringProp: string)
    {
        super(stringProp);
    }

    /**
     * 获取当前实例的字符串属性
     * @returns 字符串类型的属性值
     */
    public tProp!: string;
}

方法2:继承父类文档并补充内容

如果想保留父类的部分文档,再添加子类专属说明,可以使用@inheritDoc标签指定父类成员,再追加自定义注释:

class Child extends Parent<string>
{
    public constructor(stringProp: string)
    {
        super(stringProp);
    }

    /**
     * @inheritDoc Parent#tProp
     * 该属性在子类中固定为字符串类型
     */
    public tProp!: string;
}

说明

TypeDoc会识别这种声明式的注释,将其作为子类成员的文档,替代或补充父类的原始注释,完全不需要重写方法的实现逻辑。


内容的提问来源于stack exchange,提问作者Snap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 10:00:19