Ali.MD Ali.MD - 2 months ago 54
TypeScript Question

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

I have issue when run

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 solved when:


  1. exclude
    index.ts
    in
    tsconfig

  2. run
    tsc index.ts

  3. Turn
    declaration
    off in
    tsconfig

  4. rename
    index
    to another name!



I have same issue when change typescript main file in package.json

for example: rename index.ts to 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.


content of file no mater, any code content has same issue!

What can i do for fix it ?

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

Thank you in advance.

Answer

In you example folder you write import * as test from "../";
see https://github.com/AliMD/Node.js-Telegram-Bot-API/blob/v0.0.2-0/examples/test-server.ts#L1

It should load you index.ts, index.d.ts, or package.json "typings" field.
And this is the issue. your package.json does say 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-0/package.json#L9
so import * as test from "../"; will load package.json, load typings field and then load index.d.ts and it make the problem.

Solution

  1. You should import index file instead root folder (recommended)

    import * as test from "../index";

  2. Make your output go to a diffrent location, e.g. \lib\index.d.ts

in both solution you should load target file instead folder and because you don`t load root folder anymore, the problem will be solved.