Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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.

like image 624
Ali.MD Avatar asked May 03 '16 20:05

Ali.MD


3 Answers

Perahaps a better approach is to exclude the target directory from your build. The key is including the same directory in both the outDir and exclude keys:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": true,
        "experimentalDecorators": true,
        "outDir": "target",
        "declaration": true
    },
    "exclude": [
        "node_modules",
        "target"
    ]
}
like image 113
Burt_Harris Avatar answered Oct 21 '22 10:10

Burt_Harris


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

It should load your index.ts, index.d.ts, or package.json "typings" field.
And that's 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/package.json#L9
so import * as test from '../' will load package.json, load typings field and then load index.d.ts and it causes the problem.

Two solutions

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

  • Move output to a different folder
    e.g. \lib\index.d.ts.

In both solutions you should load the target file and not the folder. Since you're not loading the root folder anymore the problem will be solved.

like image 25
Ali.MD Avatar answered Oct 21 '22 10:10

Ali.MD


Many thanks to everyone else for their answers, for me however the problem was a lot simpler (and stupider). I had let WebStorm select the best import statement for me and overlooked that it included a file from the "dist" directory (this is what I have my output set to.

Once that cause was found the error made much more sense as it was indeed trying to write to the output file that was being used as it's input.

like image 2
Paul Carroll Avatar answered Oct 21 '22 11:10

Paul Carroll