在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"]
}

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


当前回答

使用tsc——build——explainFiles来跟踪.d。Ts被包含在您的构建中!

e.g.

dist/index.d.ts
  Imported via '..' from file 'mocks/mock-requests.ts'

在这种情况下,我应该导入'..'以外的东西…

看到https://www.typescriptlang.org/tsconfig explainFiles

其他回答

在我的情况下,这是因为我不小心包含了一个类从dist目录:

import {Entities} from "../../dist";

刚刚删除了这条线,现在一切都好了。

添加"outDir": "。/dist”到tsconfig中的compilerOptions。json为我工作时,我得到这个错误。我很确定这只是Visual Studio Code TypeScript扩展输出这个错误。我使用ts-loader与Webpack,而不是tsc编译器直接,所以我不必指定outDir,因为Webpack配置控制,但如果这使VS Code扩展高兴,这是好的。

正如GitHub问题中所建议的那样,理解这个问题的最佳选择是使用tracerresolution标志启动tsc命令

tsc - traceResolution

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

我发现:

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

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

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

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