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

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


当前回答

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

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

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

其他回答

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

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

这有几个可能的原因。

In your tsconfig.json: Set outDir to "dist" or the name of another same-level folder. (prefixing with './' is unnecessary). This is where the build files go. Set allowJs to false or delete the line. Note: enabled, allowJs will conflict with the declaration setting/flag. It isn't enabled by default. Include "dist" (or your build folder) in exclude. In your package.json: Set main to "index" or some other chosen name. Don't prefix with the build folder (eg "dist/index"), nor the unnecessary "./". Set types (modern alias of typings) to "index". Adding the extensions (.d.ts or .js) is unnecessary.

虽然你可以有一百万种不同的方法,但是为了简单和加深理解,最好一开始坚持常用的实践——比如使用“dist”,简单的tsconfig。Json和包。Json文件在树的同一级别,以此类推。当然,查找node_modules的文件也会加深您的理解,但生活中还有更有意义的事情。

这个配置适合我

"allowJs": true

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

eg:

\index.ts
\index.tsx

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

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

tsc - traceResolution