0

在 TypeScript 项目中,我有一个foo.d.ts文件和一个带有 JSDoc 注释的foo.js文件。输出类型声明时,我可以强制 TypeScript 使用 中的类型声明foo.d.ts,而不是 中的 JSDoc 注释吗?foo.js

我的问题是 JDSoc 注释有错误,我不能轻易修复它们,因为文件是由外部工具 ( protobuf.js ) 生成的。TypeScript 然后抱怨

生成/foo.js:1:1 - 错误 TS9005:为此文件发出的声明需要使用私有名称“Foo”。显式类型注释可以解除阻止声明发出。

1 导出常量 Foo = (() => {

即使foo.d.ts完全没问题。确实,手动删除 JSDoc 注释可以修复错误,但我更愿意避免在构建过程中执行该步骤。

https://github.com/microsoft/TypeScript/issues/23708#issuecomment-385020854似乎声称foo.d.ts确实应该优先于foo.js

编译器总是在 .js 文件之前选择 .d.ts 文件。因此,如果您有包的声明文件,编译器将不会查看 .js 文件,也不会查看该文件中的 JSDoc 注释。它只会查看 .d.ts 文件。

虽然我希望在导入类型声明时这是真的,但在输出类型声明时它似乎不是真的,就像我正在做的那样。

更新:在我的真实项目中,我确实找到了一种在构建过程中删除评论的简单方法,但与 MWE 不同,它不能修复错误。但是,如果我只是.js在 TypeScript 运行时删除文件,然后手动将副本放入 build/.

MWE

foo.js

export const Foo = (() => {

    // Deleting the next comment fixes the error.
    /**
     * @constructor
     */
    function Foo(properties) {}

    return Foo;
})();

foo.d.ts

export class Foo {
    constructor(properties: any);
}

tsconfig.json

{
  "compilerOptions": {
    "rootDir": "generated",
    "outDir": "build",
    "target": "es2019",
    "module": "es2015",
    "moduleResolution": "node",
    "strict": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    /* Changing "allowJs" to false fixes the error, but then foo.js is not copied to build/ like I want */
    "allowJs": true,
    "checkJs": false,
    /* Changing "declaration" to false fixes the error, but I need built .d.ts files */
    "declaration": true
  },
  "include": ["generated"]
}

打字稿版本:4.4.2

4

0 回答 0