Skip to content
← Back to rules

typescript/no-useless-empty-export Correctness

This rule is turned on by default.
🛠️ An auto-fix is available for this rule.

它的作用

禁止在模块文件中不会改变任何内容的空导出。

为什么这有问题?

空的 export {} 语句在 TypeScript 代码中有时很有用, 可以把原本会被当作脚本文件的文件变成模块文件。 根据 TypeScript Handbook Modules 页面

在 TypeScript 中,就像在 ECMAScript 2015 中一样,任何包含 顶层 import 或 export 的文件都被视为模块。相反,不包含任何顶层 import 或 export 声明的文件会被当作 脚本处理,其内容可在全局作用域中使用(因此模块也可以使用)。

然而,如果文件中已经有其他顶层 import 或 export 语句,export {} 语句就不会起任何作用。

此规则会报告在已经使用 ES 模块的文件中没有任何作用的 export {}

示例

以下是此规则的错误代码示例:

ts
export const value = "Hello, world!";
export {};

以下是此规则的正确代码示例:

ts
export const value = "Hello, world!";

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/no-useless-empty-export": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/no-useless-empty-export": "error",
  },
});
bash
oxlint --deny typescript/no-useless-empty-export

版本

此规则在 v0.4.4 中添加。

参考资料