在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文件夹中导入东西。

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

其他回答

outDir集。

"outDir": "./",

这个提示是,如果您不设置outDir,那么输出将直接放在输入文件的旁边。allowJs之后,JavaScript文件也会被编译。然后,编译后的JavaScript文件将覆盖源文件。这只是在提醒你。

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

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

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

我在一个相当大的单仓库中开发时遇到了这些错误,其中各种包依赖于对其他TypeScript包的引用。虽然错误不会影响构建或运行时,但当打开tsconfig时,它们仍然存在于VS Code问题面板中。json文件。

上面的一些答案确实帮助我减少了错误的数量,但直到我重新启动VS Code TypeScript服务器,所有的错误似乎都神奇地消失了。

在VS Code中:

Shift + command + P打开Mac上的命令托盘。开始输入“TypeScript”,并导航到“TypeScript: Restart TS Server”。按回车键

如果幸运的话,已修复的错误应该会自动消失。

这个配置适合我

"allowJs": true

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