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

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


当前回答

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

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

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

import { SomeClass } from './someclass' 

其他回答

使用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

我也有同样的问题。在我的例子中,这是选项:allowJs: true的结果。

所以我基本上要去掉这一行来消除错误。 我在您的代码中没有看到它,但它可能在这里对您有帮助。

这个配置适合我

"allowJs": true

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

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

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