使用 JSDoc 在 ES6 类上设置索引签名

发布于 2025-01-10 03:35:21 字数 364 浏览 3 评论 0原文

在 Typescript 中,您只需这样做:

class Test {
  [key: string]: whatever
}

这允许您像这样访问计算属性名称...

class Test {
  getProp(key) {
    return this[key]
  }
}

...而无需接收 Element 隐式具有“any”类型,因为类型“Test”没有索引签名。< /code>

我不知道如何使用 JSDoc 来完成相同的任务。有人有这样的运气吗?

In Typescript, you'd just do this:

class Test {
  [key: string]: whatever
}

Which allows you to access computed property names like so...

class Test {
  getProp(key) {
    return this[key]
  }
}

... without receiving Element implicitly has an 'any' type because type 'Test' has no index signature.

I can't figure out how to accomplish the equivalent with JSDoc. Anyone had any luck with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2025-01-17 03:35:21

对于单个方法,可以使用 @this 标记来执行此操作:

class Test {
    /**
     * @this {{[k: string]: string}}
     * @param {string} key
     */
    getProp(key) {
        return this[key]
    }
}

尚不支持对整个类执行此操作。目前存在 TS 4.7 中的开放功能请求 (TypeScript#48096) .1 里程碑。然而,鉴于 Needs Proposal 标签,有人需要提出一个提议的 JSDoc 标签 + 语法,否则它可能最终会被推迟到该里程碑。

For a single method, it's possible to do this with the @this tag:

class Test {
    /**
     * @this {{[k: string]: string}}
     * @param {string} key
     */
    getProp(key) {
        return this[key]
    }
}

Doing it for the entire class isn't yet supported. There is an open feature request (TypeScript#48096) which is currently in the TS 4.7.1 milestone. However, given the Needs Proposal label, somebody needs to come up with a proposed JSDoc tag + syntax, or it will likely end up being pushed off that milestone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文