在Visual Studio 2015 Update 3中的Typescript 2.2.1项目中,我在错误列表中得到了数百个错误,例如:

不能写入文件'C:/{{my-project}}/node_modules/buffer-shims/index.js',因为它会覆盖输入文件。

它一直都是这样的。它实际上并没有阻止构建,并且一切都可以正常工作,但是错误列表会分散注意力,并且很难在发生“真正的”错误时定位它们。

这是我的tsconfig。json文件

{
  "compileOnSave": true,
  "compilerOptions": {
    "baseUrl": ".",
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "ES5",
    "forceConsistentCasingInFileNames": true,
    "strictNullChecks": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,

    "typeRoots": [],
    "types": [] //Explicitly specify an empty array so that the TS2 @types modules are not acquired since we aren't ready for them yet.
  },
  "exclude": ["node_modules"]
}

我怎样才能消除这些错误呢?


当前回答

这个问题似乎已经通过更新到Typescript 2.3.x解决了

此外,使用Visual Studio 2017也是一个很大的改进。不过,我强烈建议您进行这两种更新。

其他回答

我遇到同样的问题,我尝试了以上的解决方案。没有一个对我有效。 然后删除构建文件夹在我的case - lib和重新运行构建cmd为我工作。

在我的实例中,我使用了outDir选项,但没有从输入中排除目标目录:

// Bad
{
    "compileOnSave": true,
    "compilerOptions": {
        "outDir": "./built",
        "allowJs": true,
        "target": "es5",
        "allowUnreachableCode": false,
        "noImplicitReturns": true,
        "noImplicitAny": true,
        "typeRoots": [ "./typings" ],
        "outFile": "./built/combined.js"
    },
    "include": [
        "./**/*"
    ],
    "exclude": [
        "./plugins/**/*",
        "./typings/**/*"
    ]
}

我们所要做的就是排除outDir中的文件:

// Good
{
    "compileOnSave": true,
    "compilerOptions": {
        "outDir": "./built",
        "allowJs": true,
        "target": "es5",
        "allowUnreachableCode": false,
        "noImplicitReturns": true,
        "noImplicitAny": true,
        "typeRoots": [ "./typings" ],
        "outFile": "./built/combined.js"
    },
    "include": [
        "./**/*"
    ],
    "exclude": [
        "./plugins/**/*",
        "./typings/**/*",
        "./built/**/*" // This is what fixed it!
    ]
}

在我的情况下,由于开发一个库和应用程序在同一时间…

将一个从库中导入的文件从app移动到库中,会导致现在在库中的文件从它自己的dist文件夹中导入东西。

有趣的是…这实际上是最好的重构。它保持了对文件的正确引用:)

我通过从我的tsconfig中删除“declaration”:true来解决这个问题。json文件。不过现在我没有声明了,所以没用。

我也遇到了错误:无法写入文件'xx\xxx.js',因为它会覆盖输入文件。

我发现:

如果未指定outDir, .js文件将在生成它们的.ts文件的同一目录中生成。

为了避免意外覆盖源文件,编译器将显示此错误。

您需要指定outDir,或者设置noEmit: true,以避免生成类似JavaScript源代码的编译器输出文件。