【问题标题】:Using TypeScript types from JS在 JS 中使用 TypeScript 类型
【发布时间】:2017-11-11 22:32:03
【问题描述】:

我在 Typescript 中启用了 checkJS 和 allowJS 并创建了一个简单的 fooInterface.d.ts 文件:

declare interface FooInterface {
  x();
} 

我正在尝试针对此接口对 JS 文件进行类型检查:

/**
 * @implements {FooInterface}
 */
class Bar {
  y() {}
}

但是我无法让它在运行 tsc 时引发错误。

也试过简单写declare class,也没用。

如何在 JSDoc+JS 中重用 .d.ts 文件中的类型?

【问题讨论】:

  • 我认为@implements 还没有工作,但在跟踪它时存在问题。试试@extends
  • 在哪里可以找到问题?我试图在问题跟踪器中搜索这个没有结果。 @type + declare class 无效,@extends + declare class 无效:/

标签: javascript typescript jsdoc


【解决方案1】:

已在 TypeScript 中请求支持 JSDoc @implements 标签,并由 https://github.com/Microsoft/TypeScript/issues/17498 跟踪

@extends 标签将在 TypeScript 的下一个版本 (2.7) 中得到支持,并且已经在夜间构建 (typescript@next) 中可用,我可能会补充说,它非常稳定。

您可以使用您选择的包管理器进行夜间构建。例如

使用 npm

$ npm install typescript@next

使用jspm

$ jspm install typescript@next

使用纱线

$ yarn add typescript@next

【讨论】:

    猜你喜欢
    • 2021-08-30
    • 2020-08-07
    • 2019-08-22
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2021-10-17
    • 2019-02-03
    相关资源
    最近更新 更多