首页 > 解决方案 > 在哪里可以找到 tsc 错误列表?

问题描述

我正在尝试使用 tsc 为一些现有的 javascript 代码自动生成打字稿声明文件。打字稿编译器给了我一些我不理解的错误(在这种情况下为 TS9005)。是否有 tsc 生成的所有错误代码的参考列表以及它们在某处的含义的解释?会比较方便。

标签: typescript

解决方案


诊断消息列表可以src/compiler/diagnosticMessages.json在 TypeScript 存储库中找到。该文件的结构如下:

{
    "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')()

推荐阅读