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

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


当前回答

这有几个可能的原因。

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的文件也会加深您的理解,但生活中还有更有意义的事情。

其他回答

我遇到过这个问题,因为VSCode自动完成dist/文件夹中的文件。

import { SomeClass } from '../../dist/xxx/someclass' 

要解决这个问题,只需修复导入:

import { SomeClass } from './someclass' 

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

这个配置适合我

"allowJs": true

TL;DR:使用单一回购?检查循环依赖关系!

您可能不小心从依赖于此包的包中导入了类型。

我也得到了这个错误,没有其他答案帮助我。我花了一些时间来确定它,但在浪费了至少一个小时之后,我发现潜在的错误是我的mono repo中包之间的循环依赖。

因此,我们使用yarn工作区,并拥有一个与Zilliqas非常相似的单一回购结构(我们的回购还不是开源的,所以不能链接到它)。

在包A中,我意外地(不知道怎么…)从包B中导入了一个类型,但包B反过来依赖于包A——我在packages/packageB/package.json中明确地声明了:

"dependencies": {
    ...,
    "@mycompany/packageA": "^0.0.0",
    ...
}

这是正确的。但不幸的是,在我的例子中,可能会意外地从包B中导入一个类型,即packages/packageA/_types。但是由于我没有显式地声明(因为依赖是隐式的,不需要的和意外的)这个依赖在packages/packageA/package中。在“依赖”下的Json, typescript编译器(tsc)没有检测到依赖循环,但仍然未能构建…

所以,是的,感谢TypeScript提供的令人敬畏的错误消息…哇,很多不相干的东西。

这有几个可能的原因。

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的文件也会加深您的理解,但生活中还有更有意义的事情。