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

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


当前回答

在我的例子中,我只是把"module": "commonjs"改为"module": "esnext",它就修复了它。

其他回答

我也有同样的问题,这是因为排除选项。如果指定了exclude选项,则还必须添加输出目录。

我有这样的排除选项"exclude": ["resources/js/**/*", "node_modules"]和输出目录设置为outDir:"./build"。

添加构建到排除选项修复了这个问题。

排除:[“资源/ js / * * / *”、“node_modules”,“构建”)

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

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

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

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

import { SomeClass } from './someclass' 

当你在lerna包项目或monorepo项目中,它可能工作:


首先,你的项目是这样的

packages\
  core\
    tsconfig.json
    package.json 
  common\
    tsconfig.json
    package.json
  tsconfig.base.json

你package.json

  ...
  "main": "./lib/index.js",
  "types": "./lib/index.d.ts",
  "files": [
    "lib"
  ],
  ...

你tsconfig.json

{
  "extends": "../tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./lib",
  },
}

您将发现无论如何更改tsconfig。Json文件,它不起作用


这就是我的解

先删除你的outDir:"lib",或者使用命令rd lib /s /q,然后运行tsc,就可以了

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