no-useless-empty-export
Disallow empty exports that don't change anything in a module file.
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
在 TypeScript 代码中,空的 export {}
语句有时很有用,可以将原本是脚本文件的文件转换为模块文件。根据 TypeScript 手册模块页面:
¥An empty export {}
statement is sometimes useful in TypeScript code to turn a file that would otherwise be a script file into a module file.
Per the TypeScript Handbook Modules page:
在 TypeScript 中,就像在 ECMAScript 2015 中一样,任何包含顶层导入或导出的文件都被视为模块。相反,没有任何顶层导入或导出声明的文件被视为其内容在全局作用域内可用的脚本(因此也适用于模块)。
¥In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).
但是,如果文件中有任何其他顶层导入或导出语句,则 export {}
语句不执行任何操作。
¥However, an export {}
statement does nothing if there are any other top-level import or export statements in a file.
此规则报告 export {}
的任何使用情况,包括用 替换的修复程序。
¥This rule reports an export {}
that doesn't do anything in a file already using ES modules.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-useless-empty-export": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-useless-empty-export": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
export const value = 'Hello, world!';
export {};
Open in Playgroundimport 'some-other-module';
export {};
Open in Playgroundexport const value = 'Hello, world!';
Open in Playgroundimport 'some-other-module';
Open in Playground选项
该规则不可配置。
何时不使用它
¥When Not To Use It
如果你不介意文件底部有一个空的 export {}
,则可能不需要此规则。
¥If you don't mind an empty export {}
at the bottom of files, you likely don't need this rule.
'## 资源'