我正在尝试使用tsc为一些现有的javascript代码自动生成打字稿声明文件。打字稿编译器给了我一些我不理解的错误(在这种情况下是TS9005)。是否有由tsc生成的所有错误代码的参考列表以及它们在某处的含义的解释?这将是相当方便的。
诊断消息列表可以在 TypeScript 存储库的src/compiler/diagnosticMessages.json
中找到。该文件的结构如下:
{
"Unterminated string literal.": {
"category": "Error",
"code": 1002
},
"Identifier expected.": {
"category": "Error",
"code": 1003
},
"'{0}' expected.": {
"category": "Error",
"code": 1005
},
"A file cannot have a reference to itself.": {
"category": "Error",
"code": 1006
},
// etc...
}
但是,据我所知,没有解释列表。
TS9005 (Declaration emit for this file requires using private name '{0}'. An explicit type annotation may unblock declaration emit.
) 表示 JS 文件正在导出具有非导出类型的内容。例如:
foo.d.ts
interface Foo {
foo: number
}
declare function foo(): Foo
export = foo
bar.js
// @ts-check
module.exports = require('./foo')()
TypeScript 无法为bar.js
创建声明文件,因为导出的类型为Foo
,而不是从foo.d.ts
导出。可以通过为导出添加类型声明来解决此问题:
bar.js
// @ts-check
/** @type {{foo: number}} */
module.exports = require('./foo')()