Skip to main content

consistent-type-exports

Enforce consistent usage of type exports.

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复

💭

该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。

TypeScript 允许在导出上指定 type 关键字,以指示导出仅存在于类型系统中,而不存在于运行时。这允许转译器在不知道依赖类型的情况下删除导出。

¥TypeScript allows specifying a type keyword on exports to indicate that the export exists only in the type system, not at runtime. This allows transpilers to drop exports without knowing the types of the dependencies.

请参阅 博客 > 一致类型导出和导入:原因和方法 以了解更多详细信息。

¥See Blog > Consistent Type Exports and Imports: Why and How for more details.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/consistent-type-exports": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

interface ButtonProps {
onClick: () => void;
}

class Button implements ButtonProps {
onClick = () => console.log('button!');
}

export { Button, ButtonProps };
Open in Playground

选项

该规则接受以下选项:

type Options = [
{
/** Whether the rule will autofix "mixed" export cases using TS inline type specifiers. */
fixMixedExportsWithInlineTypeSpecifier?: boolean;
},
];

const defaultOptions: Options = [
{ fixMixedExportsWithInlineTypeSpecifier: false },
];

¥Options

fixMixedExportsWithInlineTypeSpecifier

Whether the rule will autofix "mixed" export cases using TS inline type specifiers. Default: false.

如果你使用的 TypeScript 版本低于 4.5,那么你将无法使用此选项。

¥If you are using a TypeScript version less than 4.5, then you will not be able to use this option.

例如以下代码:

¥For example the following code:

const x = 1;
type T = number;

export { x, T };

使用 {fixMixedExportsWithInlineTypeSpecifier: true} 将修复为:

¥With {fixMixedExportsWithInlineTypeSpecifier: true} will be fixed to:

const x = 1;
type T = number;

export { x, type T };

使用 {fixMixedExportsWithInlineTypeSpecifier: false} 将修复为:

¥With {fixMixedExportsWithInlineTypeSpecifier: false} will be fixed to:

const x = 1;
type T = number;

export type { T };
export { x };

何时不使用它

¥When Not To Use It

如果你使用 --isolatedModules,如果未使用 export type 重新导出类型,则编译器会出错。此规则通常禁止为空的类(没有构造函数或字段)。

¥If you use --isolatedModules the compiler would error if a type is not re-exported using export type. This rule may be less useful in those cases.

如果你出于风格原因特别想要使用两种导出类型,或者不希望强制使用一种风格而不是另一种风格,则可以避免此规则。

¥If you specifically want to use both export kinds for stylistic reasons, or don't wish to enforce one style over the other, you can avoid this rule.

但是,请记住,不一致的风格可能会损害项目的可读性。我们建议为此规则选择一个最适合你的项目的选项。

¥However, keep in mind that inconsistent style can harm readability in a project. We recommend picking a single option for this rule that works best for your project.


Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

'## 资源'