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
}
}
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.
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"
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With