9

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.

10
  • I excluded index.ts in tsconfig temporary to solve this issue and run tsc index.ts separately when index.ts changed! :( Commented May 3, 2016 at 20:26
  • maybe you have a reference to index.d.ts in your code somewhere, then when you compile, the compiler thinks that index.d.ts is part of the source and therefor throw that error. just a thought. Commented May 3, 2016 at 21:21
  • What happens if you compile with out the "declaration": true? Commented May 3, 2016 at 21:42
  • what do you mean ? Commented May 4, 2016 at 21:37
  • 5
    you don't get points on comments, but you're right, I don't know the answer, but I'm asking questions/making suggestions in order to try and help you solve or find workaround your problem until it's solved. You're welcome. And the outDir can be related to the issue as it separates the source from the output, and then it shouldn't complain about writing on a source file, maybe, just a thought, but maybe it won't work, damn, won't get my precious points. Commented May 4, 2016 at 23:07

6 Answers 6

16

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"
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

8

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.

3 Comments

Links broken, you better to copy that code snippet that use URLs
@Al-Mothafar Thank you, i updated the links, as you see i wrote the codes and also link the source for more information
Worth noting that the same applies if you import from “./“ and happen to be at the top level, which is helpful when searching for this pattern of use
7

In my case I was missing "include": ["./src/**/*.ts"] in tsconfig.json even though I had set the following:

    "compilerOptions": {
        "outDir": "./build",
        "rootDir": "./src"
    },

Typescript was probably confused as to which files it needed to include and probably thought my file that was built in ./build/index.d.ts was an input file.

Comments

4

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.

1 Comment

I had similar problem, VSCode was doing auto imports, and I overlooked one of them was from dist directory.
1

Adding include to my configuration solved the problem for me.

  {
    "compilerOptions": {
        /* ... */
    },
    "include": ["src"],
    "exclude": ["**/*.test.*"]
  }

As said in the doc, exclude specifies an array of filenames or patterns that should be skipped when resolving include.

I deduce that exclude can't work if include hasn't been defined.

Comments

-1

I had this very same problem, and it was super simple to fix. I deleted node_modules, then deleting the typings folder.

in package.json I have these in

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

then ran the commands:

npm install
npm run tsc
npm run typings install

1 Comment

Not sure why this was downvoted. But in the last 6 years, this error has occurred for numerous reasons. This answer helped out with my issue, and it is used as a possible answer that could be tried without ruining your solution. Just because it didn't work for you doesn't mean it's not a possible solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.