Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a storm of "error TS5055" messages on every TypeScript Compile in VS Code

I am trying to convert a JavaScript based express project to TypeScript using VS Code 1.6.1. I can now add new files in TypeScript and compile them to JavaScript. However, every time I compile a TypeScript file, I get a long list of errors - one per existing (legacy) JavaScript file - saying:

error TS5055: cannot write file XYZ.js because it would overwrite input file

How do I stop this?

My tsconfig looks like this:

{
    "compilerOptions": {
        "module": "commonjs",
        "sourceMap": true,
        "watch": true,
        "allowJs": true
    }
}
like image 837
mark andrew Avatar asked Nov 07 '16 08:11

mark andrew


2 Answers

This problem occurred because the TypeScript compiler tries to transpile everything, including files that are already JavaScript, in which case it understandably complains that it will overwrite its input. I found no combination of "include" and "exclude" statements which could stop this behaviour.

The solution is to add a new field, "outDir", to the compilerOptions, so it looks like this

{
    "compilerOptions": {
        "module": "commonjs",
        "sourceMap": true,
        "watch": true,
        "allowJs": true,
        "outDir": "generated"
    }
}

This will cause tsc to copy ALL ts and js in your project to the specified folder. There will be no more of those pesky TS5055 errors.

If the import references in your project are all relative, this should be fine. Otherwise, for example, if your Node.js project is using a "public" folder at the top level, you might have to modify path references in your code accordingly.

like image 171
mark andrew Avatar answered Sep 16 '22 15:09

mark andrew


The error is happening also when the d.ts are generated tsc take all files it found in the output directory and sub-directories by default.

Assuming your output is in the out directory, you have to exclude it in the tsconfig.json:

"compilerOptions": {
  // All the options you want...

  // Redirect output structure to the directory
  "outDir": "out"

  // Exclude here the directories you want ts do not compile...
  "exclude": [
    "out"
  ]
}
like image 25
Shim-Sao Avatar answered Sep 20 '22 15:09

Shim-Sao