Skip to content

Commit ba9295b

Browse files
authored
feat(eslint-plugin): add consistent-type-imports rule (#2367)
1 parent 12b9c8a commit ba9295b

File tree

6 files changed

+1682
-0
lines changed

6 files changed

+1682
-0
lines changed

Diff for: packages/eslint-plugin/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
106106
| [`@typescript-eslint/class-literal-property-style`](./docs/rules/class-literal-property-style.md) | Ensures that literals on classes are exposed in a consistent style | | :wrench: | |
107107
| [`@typescript-eslint/consistent-type-assertions`](./docs/rules/consistent-type-assertions.md) | Enforces consistent usage of type assertions | | | |
108108
| [`@typescript-eslint/consistent-type-definitions`](./docs/rules/consistent-type-definitions.md) | Consistent with type definition either `interface` or `type` | | :wrench: | |
109+
| [`@typescript-eslint/consistent-type-imports`](./docs/rules/consistent-type-imports.md) | Enforces consistent usage of type imports | | :wrench: | |
109110
| [`@typescript-eslint/explicit-function-return-type`](./docs/rules/explicit-function-return-type.md) | Require explicit return types on functions and class methods | | | |
110111
| [`@typescript-eslint/explicit-member-accessibility`](./docs/rules/explicit-member-accessibility.md) | Require explicit accessibility modifiers on class properties and methods | | :wrench: | |
111112
| [`@typescript-eslint/explicit-module-boundary-types`](./docs/rules/explicit-module-boundary-types.md) | Require explicit return and argument types on exported functions' and classes' public class methods | :heavy_check_mark: | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Enforces consistent usage of type imports (`consistent-type-imports`)
2+
3+
TypeScript 3.8 added support for type-only imports.
4+
Type-only imports allow you to specify that an import can only be used in a type location, allowing certain optimizations within compilers.
5+
6+
## Rule Details
7+
8+
This rule aims to standardize the use of type imports style across the codebase.
9+
10+
## Options
11+
12+
```ts
13+
type Options = {
14+
prefer: 'type-imports' | 'no-type-imports';
15+
disallowTypeAnnotations: boolean;
16+
};
17+
18+
const defaultOptions: Options = {
19+
prefer: 'type-imports',
20+
disallowTypeAnnotations: true,
21+
};
22+
```
23+
24+
### `prefer`
25+
26+
This option defines the expected import kind for type-only imports. Valid values for `prefer` are:
27+
28+
- `type-imports` will enforce that you always use `import type Foo from '...'`. It is default.
29+
- `no-type-imports` will enforce that you always use `import Foo from '...'`.
30+
31+
Examples of **correct** code with `{prefer: 'type-imports'}`, and **incorrect** code with `{prefer: 'no-type-imports'}`.
32+
33+
```ts
34+
import type { Foo } from 'Foo';
35+
import type Bar from 'Bar';
36+
type T = Foo;
37+
const x: Bar = 1;
38+
```
39+
40+
Examples of **incorrect** code with `{prefer: 'type-imports'}`, and **correct** code with `{prefer: 'no-type-imports'}`.
41+
42+
```ts
43+
import { Foo } from 'Foo';
44+
import Bar from 'Bar';
45+
type T = Foo;
46+
const x: Bar = 1;
47+
```
48+
49+
### `disallowTypeAnnotations`
50+
51+
If `true`, type imports in type annotations (`import()`) is not allowed.
52+
Default is `true`.
53+
54+
Examples of **incorrect** code with `{disallowTypeAnnotations: true}`.
55+
56+
```ts
57+
type T = import('Foo').Foo;
58+
const x: import('Bar') = 1;
59+
```
60+
61+
## When Not To Use It
62+
63+
- If you are not using TypeScript 3.8 (or greater), then you will not be able to use this rule, as type-only imports are not allowed.
64+
- If you specifically want to use both import kinds for stylistic reasons, you can disable this rule.

Diff for: packages/eslint-plugin/src/configs/all.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export = {
1818
'@typescript-eslint/comma-spacing': 'error',
1919
'@typescript-eslint/consistent-type-assertions': 'error',
2020
'@typescript-eslint/consistent-type-definitions': 'error',
21+
'@typescript-eslint/consistent-type-imports': 'error',
2122
'default-param-last': 'off',
2223
'@typescript-eslint/default-param-last': 'error',
2324
'dot-notation': 'off',

0 commit comments

Comments
 (0)