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

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


当前回答

我也有同样的问题。在我的情况下,这是因为我在一个模块中有两个同名的文件: index.ts index.tsx。

我重新命名了其中一个,问题得到了解决。

其他回答

如果你在一个大的代码或monorepo中工作,有时只是重新启动你的编辑器就足够了,或者如果你使用vscode重新启动typescript语言服务器。

问题的根源可能是两个文件生成了相同的模块。因此,如果在同一个文件夹中有两个名称相同但扩展名不同的文件,则会导致此错误。

eg:

\index.ts
\index.tsx

解决方案是将其中一个文件名更改为其他名称。

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

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

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

在我的实例中,我使用了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!
    ]
}

来自另一个答案的allowJs选项让我想到,也许我的配置不允许在项目中使用JavaScript文件。

因此,我没有像人们推荐的那样使用outDir,而是将有问题的.js重命名为.ts。

当然,这是一个样板项目,该文件是整个(TypeScript)项目中唯一的JavaScript文件。