TypeScript error: Unable to write file 'index.d.ts' because it will overwrite the input file

I have a problem running tsc

 error TS5055: Cannot write file 'index.d.ts' because it would overwrite input file. 

my tsconfig.json :

 { "compilerOptions": { "target": "es6", "module": "commonjs", "moduleResolution": "node", "declaration": true, "newLine": "LF", "preserveConstEnums": true, "pretty": true, "experimentalDecorators": true }, "exclude": [ "node_modules", "typings" ] } 

This issue is resolved if:

  • exclude index.ts in tsconfig
  • run tsc index.ts
  • Turn off declaration in tsconfig
  • rename index to another name!

I have the same problem when changing typescript of the main file in package.json
for example: rename index.ts in foo.ts

change package.json to

 "typescript": { "main": "foo.ts" } 

Tsc error:

 error TS5055: Cannot write file 'index.d.ts' because it would overwrite input file. 

file contents without mater, any code contents have the same problem!

What can I do to fix this?

Source code: https://github.com/AliMD/Node.js-Telegram-Bot-API/tree/v0.0.2-0
Thank you in advance.

+5
source share
4 answers

In your example folder, you wrote import * as test from '../'
see https://github.com/AliMD/Node.js-Telegram-Bot-API/blob/v0.0.2/examples/test-server.ts#L1

Your print field index.ts , index.d.ts or package.json should be loaded.
And that is the problem. Your package.json really says that index.d.ts is the definition file for this package, see https://github.com/AliMD/Node.js-Telegram-Bot-API/blob/v0.0.2/package. json # L9
so import * as test from '../' load package.json , load the input field, and then load index.d.ts , and this will cause a problem.

Two solutions

  • Import index file instead of root folder (recommended approach)
    for example import * as test from '../index' ; or,

  • Move output to another folder
    , eg. \lib\index.d.ts .

In both solutions, you should upload the target file, not the folder. Since you are no longer loading the root folder, the problem will be resolved.

+4
source

Perahaps's best approach is to exclude the target directory from your assembly. The key includes the same directory in the outDir and exclude keys:

 { "compilerOptions": { "module": "commonjs", "target": "es6", "noImplicitAny": false, "sourceMap": true, "experimentalDecorators": true, "outDir": "target", "declaration": true }, "exclude": [ "node_modules", "target" ] } 
+1
source

Many thanks to everyone for their answers, for me, however, the problem was much simpler (and dumber). I let WebStorm choose the best import statement for me and overlooked that it includes a file from the "dist" directory (this is what I configured the output for.

As soon as this reason was found, the error acquired much more meaning, because it really tried to write to the output file that was used during the input.

+1
source

I had the same problem and it was very easy to fix. I deleted node_modules and then deleted the types folder.

in package.json I have this in

 scripts: { ... "tsc": "tsc", "typings": "typings" ... } 

then executed the commands:

npm install
npm run tsc
npm run typings install

0
source

Source: https://habr.com/ru/post/1265389/


All Articles