UNPKG

2.01 kBMarkdownView Raw
1# `consistent-type-imports`
2
3Enforces consistent usage of type imports.
4
5TypeScript 3.8 added support for type-only imports.
6Type-only imports allow you to specify that an import can only be used in a type location, allowing certain optimizations within compilers.
7
8## Rule Details
9
10This rule aims to standardize the use of type imports style across the codebase.
11
12## Options
13
14```ts
15type Options = {
16 prefer: 'type-imports' | 'no-type-imports';
17 disallowTypeAnnotations: boolean;
18};
19
20const defaultOptions: Options = {
21 prefer: 'type-imports',
22 disallowTypeAnnotations: true,
23};
24```
25
26### `prefer`
27
28This option defines the expected import kind for type-only imports. Valid values for `prefer` are:
29
30- `type-imports` will enforce that you always use `import type Foo from '...'` except referenced by metadata of decorators. It is default.
31- `no-type-imports` will enforce that you always use `import Foo from '...'`.
32
33Examples of **correct** code with `{prefer: 'type-imports'}`, and **incorrect** code with `{prefer: 'no-type-imports'}`.
34
35```ts
36import type { Foo } from 'Foo';
37import type Bar from 'Bar';
38type T = Foo;
39const x: Bar = 1;
40```
41
42Examples of **incorrect** code with `{prefer: 'type-imports'}`, and **correct** code with `{prefer: 'no-type-imports'}`.
43
44```ts
45import { Foo } from 'Foo';
46import Bar from 'Bar';
47type T = Foo;
48const x: Bar = 1;
49```
50
51### `disallowTypeAnnotations`
52
53If `true`, type imports in type annotations (`import()`) is not allowed.
54Default is `true`.
55
56Examples of **incorrect** code with `{disallowTypeAnnotations: true}`.
57
58```ts
59type T = import('Foo').Foo;
60const x: import('Bar') = 1;
61```
62
63## When Not To Use It
64
65- 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.
66- If you specifically want to use both import kinds for stylistic reasons, you can disable this rule.
67
68## Attributes
69
70- Configs:
71 - [ ] ✅ Recommended
72 - [ ] 🔒 Strict
73- [x] 🔧 Fixable
74- [ ] 💭 Requires type information