TypeScript: [bug] tsc error: cannot write file 'main.d.ts' because it would overwrite input file

Excuse me for writing my question here but i guess its a bug.

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"
      ]
    }

TypeScript Version: 1.8.10

This issue solved when:

  1. Exclude index.ts in tsconfig
  2. Run tsc index.ts
  3. Turn declaration off in tsconfig
  4. Rename indextx to foo.ts!

I have same issue when i rename index.ts to foo.ts and 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 index 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 and excuse me again for asking my question here.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

for future references tsc --traceResolution should show you what happened. here is what i got running it on your project:

======== Resolving module '../' from 'c:/test/8468/Node.js-Telegram-Bot-API/examples/test-server.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
Loading module as file / folder, candidate module location 'c:/test/8468/Node.js-Telegram-Bot-API'.
File 'c:/test/8468/Node.js-Telegram-Bot-API.ts' does not exist.
File 'c:/test/8468/Node.js-Telegram-Bot-API.tsx' does not exist.
File 'c:/test/8468/Node.js-Telegram-Bot-API.d.ts' does not exist.
Found 'package.json' at 'c:/test/8468/Node.js-Telegram-Bot-API/package.json'.
'package.json' has 'typings' field 'index.d.ts' that references 'c:/test/8468/Node.js-Telegram-Bot-API/index.d.ts'.
File 'c:/test/8468/Node.js-Telegram-Bot-API/index.d.ts' exist - use it as a name resolution result.
======== Module name '../' was successfully resolved to 'c:/test/8468/Node.js-Telegram-Bot-API/index.d.ts'. ========

I don’t think you can import a folder at once.

you can, it shoudl 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 defintion 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.t.

solutions

  1. make your output go to a diffrent location, e.g. \lib\index.d.ts
  2. import index instead of the folder as @zhengbli suggested erlier

If you mean to import from the index.ts, you can do import * as test from 'index'. I don’t think you can import a folder at once.

nope. arab 😃 egyptien to be specific.

The error means that the compiler is trying to write on of its outputs (in this case index.d.ts because you are using --declaration on an input file index.ts or index.tsx presumably) that happens to be an input that was passed to it. the result is a possible data loss (i.e. if index.d.ts is a file that you wrote by hand, and did not mean for it to be overwritten with the one that is emitted from the compiler.

When you use tsconfig.json, it sucks in all the files in the containing folder and its subfolders.

consider using --out or --outDir and excluding the output file or folder in your tsconfig.json.