Skip to content
← Back to rules

import/no-default-export Restriction

作用

禁止模块具有默认导出。这有助于你的编辑器 提供更好的自动导入功能,因为与默认导出相比,具名导出提供了 更明确、更可预测的导入方式。

这为什么不好?

默认导出可能会导致混淆,因为导入值的名称 会根据其导入方式而变化。这会使重构和 自动导入变得不那么可靠。

示例

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

javascript
export default 'bar';

const foo = 'foo';
export { foo as default }

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

javascript
export const foo = "foo";
export const bar = "bar";

如何使用

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

json
{
  "plugins": ["import"],
  "rules": {
    "import/no-default-export": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["import"],
  rules: {
    "import/no-default-export": "error",
  },
});
bash
oxlint --deny import/no-default-export --import-plugin

版本

此规则在 v0.2.14 中添加。

参考资料