useExportType
Summary
Section titled “Summary”- Rule available since:
v1.5.0 - Diagnostic Category:
lint/style/useExportType - This rule is recommended, meaning it is enabled by default.
- This rule has a safe fix.
- The default severity of this rule is warning.
- Sources:
- Inspired from
@typescript-eslint/consistent-type-exports
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "style": { "useExportType": "error" } } }}Description
Section titled “Description”Promotes the use of export type for types.
TypeScript allows specifying a type keyword on an export to indicate that the export doesn’t exist at runtime.
This allows compilers to safely drop exports of types without looking for their definition.
The rule ensures that all exports used only as a type use a type-only export.
It also groups inline type exports into a grouped export type.
If you use the TypeScript Compiler (TSC) to compile your code into JavaScript,
then you can disable this rule, as TSC can remove exports only used as types.
However, for consistency and compatibility with other compilers, you may want to enable this rule.
In that case, it’s recommended to enable TSC’s verbatimModuleSyntax.
This configuration ensures that TSC preserves exports not marked with the type keyword.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”interface I {}export { I };code-block.ts:2:8 lint/style/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ All exports are only types.
1 │ interface I {}
> 2 │ export { I };
│ ^^^^^^
3 │
ℹ Using export type allows compilers to safely drop exports of types without looking for their definition.
ℹ Safe fix: Use export type.
2 │ export·type·{·I·};
│ +++++
type T = number;export { T };code-block.ts:2:8 lint/style/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ All exports are only types.
1 │ type T = number;
> 2 │ export { T };
│ ^^^^^^
3 │
ℹ Using export type allows compilers to safely drop exports of types without looking for their definition.
ℹ Safe fix: Use export type.
2 │ export·type·{·T·};
│ +++++
import type { T } from "./mod.js";export { T };code-block.ts:2:8 lint/style/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ All exports are only types.
1 │ import type { T } from ”./mod.js”;
> 2 │ export { T };
│ ^^^^^^
3 │
ℹ Using export type allows compilers to safely drop exports of types without looking for their definition.
ℹ Safe fix: Use export type.
2 │ export·type·{·T·};
│ +++++
export { type X, type Y };code-block.ts:1:8 lint/style/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ All exports are only types.
> 1 │ export { type X, type Y };
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using export type allows compilers to safely drop exports of types without looking for their definition.
ℹ Safe fix: Use export type.
1 │ - export·{·type·X,·type·Y·};
1 │ + export·type·{·X,·Y·};
2 2 │
class C {}function f() {}export { C, f };This rules checks only the identifiers that are defined in a file. It doesn’t warn against a type exported as a value in a re-export clause such as:
export { TypeA } from "./mod.ts"Options
Section titled “Options”The style option allows enforcing a style for exporting types.
The option supports three values:
inlineType: always useexport { type T }instead ofexport type { T }separatedType: always useexport type { T }instead ofexport { type T }auto: useexport type { T }orexport { type T, V }when values are exported alongside types (default)
{ "linter": { "rules": { "style": { "useExportType": { "options": { "style": "inlineType" } } } } }}import type { A } from "./mod.ts";export { A };code-block.ts:2:8 lint/style/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Some exports are only types.
1 │ import type { A } from ”./mod.ts”;
> 2 │ export { A };
│ ^^^^^^
3 │
ℹ This export is a type.
1 │ import type { A } from ”./mod.ts”;
> 2 │ export { A };
│ ^
3 │
ℹ Using export type allows compilers to safely drop exports of types without looking for their definition.
ℹ Safe fix: Add inline type keywords.
2 │ export·{·type·A·};
│ +++++
import { A } from "./mod.ts";export type { A };code-block.ts:2:8 lint/style/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Some exports are only types.
1 │ import { A } from ”./mod.ts”;
> 2 │ export type { A };
│ ^^^^^^^^^^^
3 │
ℹ This export is a type.
1 │ import { A } from ”./mod.ts”;
> 2 │ export type { A };
│ ^
3 │
ℹ Using export type allows compilers to safely drop exports of types without looking for their definition.
ℹ Safe fix: Add inline type keywords.
1 1 │ import { A } from ”./mod.ts”;
2 │ - export·type·{·A·};
2 │ + export·{·type·A·};
3 3 │
{ "linter": { "rules": { "style": { "useExportType": { "options": { "style": "separatedType" } } } } }}import type { A } from "./mod.ts";export { A };code-block.ts:2:8 lint/style/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ All exports are only types.
1 │ import type { A } from ”./mod.ts”;
> 2 │ export { A };
│ ^^^^^^
3 │
ℹ Using export type allows compilers to safely drop exports of types without looking for their definition.
ℹ Safe fix: Use export type.
2 │ export·type·{·A·};
│ +++++
import { A, B } from "./mod.ts";export { type A, B };code-block.ts:2:8 lint/style/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Separate type exports from other exports.
1 │ import { A, B } from ”./mod.ts”;
> 2 │ export { type A, B };
│ ^^^^^^^^^^^^^^
3 │
ℹ Using export type allows compilers to safely drop exports of types without looking for their definition.
ℹ Safe fix: Extract types into a new export.
1 1 │ import { A, B } from ”./mod.ts”;
2 │ - export·{·type·A,·B·};
2 │ + export·type·{·A,·};
3 │ + export·{·B·};
3 4 │
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.