consistent-type-exports
Enforce consistent usage of type exports.
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
This rule requires type information to run.
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.
module.exports = {
"rules": {
"@typescript-eslint/consistent-type-exports": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
interface ButtonProps {
onClick: () => void;
}
class Button implements ButtonProps {
onClick = () => console.log('button!');
}
export { Button, ButtonProps };
Open in Playgroundinterface ButtonProps {
onClick: () => void;
}
class Button implements ButtonProps {
onClick = () => console.log('button!');
}
export { Button };
export type { ButtonProps };
Open in PlaygroundOptions
This rule accepts the following options:
type Options = [
{
fixMixedExportsWithInlineTypeSpecifier?: boolean;
},
];
const defaultOptions: Options = [
{ fixMixedExportsWithInlineTypeSpecifier: false },
];
fixMixedExportsWithInlineTypeSpecifier
When this is set to true, the rule will autofix "mixed" export cases using TS 4.5's "inline type specifier". 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 };
With {fixMixedExportsWithInlineTypeSpecifier: true}
will be fixed to:
const x = 1;
type T = number;
export { x, type T };
With {fixMixedExportsWithInlineTypeSpecifier: false}
will be fixed to:
const x = 1;
type T = number;
export type { T };
export { x };
When Not To Use It
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 degredations after enabling type checked rules.