简体   繁体   English

TypeScript 错误:无法写入文件“index.d.ts”,因为它会覆盖输入文件

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

I have issue when run tsc运行tsc时出现问题

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

my tsconfig.json :我的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排除index.tstsconfig
  2. run tsc index.ts运行tsc index.ts
  3. Turn declaration off in tsconfigtsconfig关闭declaration
  4. rename index to another name!index重命名为另一个名称!

I have same issue when change typescript main file in package.json在 package.json 中更改打字稿主文件时我遇到了同样的问题
for example: rename index.ts to foo.ts例如:将 index.ts 重命名为 foo.ts

change package.json topackage.json更改为

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

tsc error: tsc 错误:

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源代码:https ://github.com/AliMD/Node.js-Telegram-Bot-API/tree/v0.0.2-0
Thank you in advance.提前谢谢你。

Perahaps a better approach is to exclude the target directory from your build.也许更好的方法是从构建中排除target目录。 The key is including the same directory in both the outDir and exclude keys:关键是在outDirexclude键中包含相同的目录:

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

In your example folder you wrote import * as test from '../'在您的示例文件夹中,您编写了import * as test from '../'
see https://github.com/AliMD/Node.js-Telegram-Bot-API/blob/v0.0.2/examples/test-server.ts#L1https://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.它应该加载您的index.tsindex.d.tspackage.jsonindex.d.ts ” 字段。
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你的package.json确实说index.d.ts是这个包的定义文件,见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.所以import * as test from '../'将加载package.json ,加载index.d.ts字段,然后加载index.d.ts并导致问题。

Two solutions两种解决方案

  • Import index file instead of root folder (recommended approach)导入索引文件而不是根文件夹(推荐方法)
    eg import * as test from '../index' ;例如import * as test from '../index' or,或者,

  • Move output to a different folder将输出移动到不同的文件夹
    eg \\lib\\index.d.ts .例如\\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.由于您不再加载根文件夹,因此问题将得到解决。

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.我让 WebStorm 为我选择了最好的导入语句,但忽略了它包含来自“dist”目录的文件(这是我将输出设置为的文件。

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.一旦发现该原因,错误就更有意义了,因为它确实试图写入用作输入的输出文件。

I had this very same problem, and it was super simple to fix.我遇到了同样的问题,解决起来非常简单。 I deleted node_modules, then deleting the typings folder.我删除了 node_modules,然后删除了typings 文件夹。

in package.json I have these in在 package.json 我有这些

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

then ran the commands:然后运行命令:

npm install
npm run tsc
npm run typings install

In my case I was missing "include": ["./src/**/*.ts"] in tsconfig.json even though I had set the following:在我的情况下,即使我设置了以下内容,我在 tsconfig.json 中也缺少"include": ["./src/**/*.ts"]

    "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. Typescript 可能对它需要包含哪些文件感到困惑,并且可能认为我在./build/index.d.ts中构建的文件是一个输入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法写入文件“index.d.ts”,因为它会覆盖输入文件 - 但它被(?)排除在外 - Cannot write file 'index.d.ts' because it would overwrite input file - but it is(?) excluded 错误TS5055中的错误:无法写入文件“…”,因为它将使用allowJS和outDir选项覆盖输入文件 - ERROR in error TS5055: Cannot write file '…' because it would overwrite input file with allowJS and outDir option 打字稿错误“无法写入文件...因为它会覆盖输入文件。” - Typescript error "Cannot write file ... because it would overwrite input file." VSCode中的TypeScript错误:“无法写入文件'x',因为它将覆盖输入文件。” - TypeScript errors in VSCode: “Cannot write file 'x' because it would overwrite input file.” 如何拆分大型的typescript index.d.ts文件 - How to split large typescript index.d.ts file @ types / jest index.d.ts文件返回错误 - @types/jest index.d.ts file returning error index.d.ts 文件未发布到 NPM - index.d.ts file not published to NPM TypeScript输入文件node.d.ts与index.d.ts - TypeScript typings file node.d.ts versus index.d.ts TypeScript 找不到带有 index.d.ts 的节点模块 - TypeScript cannot find node module with index.d.ts 无法使用打字稿中的index.d.ts扩展Process接口 - cannot extend Process interface with index.d.ts in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM