【问题标题】:How to document a JS class in Typescript with JSDoc如何使用 JSDoc 在 Typescript 中记录 JS 类
【发布时间】:2019-02-03 07:07:35
【问题描述】:

我有一个代码库,希望慢慢迁移到 Typescript。这意味着我使用 Node 中的 util.inherits 以非 ES6 方式创建类,并且希望使用 JSDoc 类型注释而不是此时转换为 Typescript。

但是我在输入类时遇到问题:

var util = require("util");

function Base() {
}

/**
 * @constructor
 * @param {string} arg
 */
function Thing(arg) {
    Thing.super_.call(this);

    this.x = arg;
}

util.inherits(Thing, Base);

var thing = new Thing("test");

运行 Typescript 时会给出以下输出:

$ tsc --noEmit --allowJs --checkJs .\test.js
test.js:11:15 - error TS2339: Property 'super_' does not exist on type 'typeof Thing'.

11         Thing.super_.call(this);
                 ~~~~~~

有没有办法使用 JSDoc 记录 inherits 创建的 super_ 属性?

【问题讨论】:

    标签: javascript typescript jsdoc


    【解决方案1】:

    这似乎有效:

    /** @type {typeof Base} */
    Thing.super_;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      • 2022-01-03
      相关资源
      最近更新 更多