Typescript error “Cannot write file xxx because it would overwrite input file

出现这个问题基本是因为开启了allowJs。

因为allowJs即允许Typescript编译器去编译js。而编译之后的输出文件也就是xxx.js与源文件是一样的。

所以就会报出“会覆盖输入文件”这样的错误。

事实上,我们日常开发使用第三方打包工具比如webpack。编译输出是由ts-loader负责的,所以不必关心每个TS文件的输出。

此时可以设置noEmits为true

No Emit -
Do not emit compiler output files like JavaScript source code, source-maps or declarations.
This makes room for another tool like Babel, or swc to handle converting the TypeScript file to a file which can run inside a JavaScript environment.
You can then use TypeScript as a tool for providing editor integration, and as a source code type-checker.

此外还可以指定输出目录,来避免冲突。

更多具体信息,请参考 https://github.com/kulshekhar/ts-jest/issues/1471

Here is a possible JSON schema for the TypeScript compiler's configuration file: ```json { "$schema": "http://json-schema.org/draft-07/schema#", "title": "TypeScript Compiler Configuration", "type": "object", "properties": { "compilerOptions": { "type": "object", "properties": { "allowJs": { "type": "boolean" }, "checkJs": { "type": "boolean" }, "noEmit": { "type": "boolean" }, "noEmitOnError": { "type": "boolean" }, "strict": { "type": "boolean" }, "target": { "type": ["string", "number"], "enum": ["ES3", "ES5", "ES6", "ES2015", "ES2016", "ES2017", "ES2018", "ES2019", "ES2020", "ES2021", "ESNext"] }, "module": { "type": ["string", "number"], "enum": ["CommonJS", "AMD", "System", "UMD", "ES6", "ES2015", "ES2020", "ESNext"] }, "outFile": { "type": "string" }, "outDir": { "type": "string" }, "rootDir": { "type": "string" }, "baseUrl": { "type": "string" }, "paths": { "type": "object", "additionalProperties": { "type": "array", "items": { "type": "string" } } }, "typeRoots": { "type": "array", "items": { "type": "string" } }, "types": { "type": "array", "items": { "type": "string" } }, "lib": { "type": "array", "items": { "type": "string" } }, "experimentalDecorators": { "type": "boolean" }, "emitDecoratorMetadata": { "type": "boolean" } }, "additionalProperties": false }, "include": { "type": "array", "items": { "type": "string" } }, "exclude": { "type": "array", "items": { "type": "string" } } }, "required": ["compilerOptions"], "additionalProperties": false } ``` This schema defines an object with three properties: `compilerOptions`, `include`, and `exclude`. The `compilerOptions` property is itself an object with various properties for configuring the TypeScript compiler, such as `allowJs`, `checkJs`, `noEmit`, `strict`, etc. The other two properties, `include` and `exclude`, are arrays of file patterns to include or exclude from compilation. The schema includes type validations and some additional constraints, such as allowed values for `target` and `module`.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值