[英]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:此问题在以下情况下解决:
index.ts
in tsconfig
index.ts
在tsconfig
tsc index.ts
tsc index.ts
declaration
off in tsconfig
tsconfig
关闭declaration
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
to将
package.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:关键是在
outDir
和exclude
键中包含相同的目录:
{
"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#L1见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.它应该加载您的
index.ts
、 index.d.ts
或package.json
“ index.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.