-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): add consistent-type-imports
rule
#2367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bradzacher
merged 2 commits into
typescript-eslint:v4
from
ota-meshi:consistent-type-import
Aug 16, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/eslint-plugin/docs/rules/consistent-type-imports.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Enforces consistent usage of type imports (`consistent-type-imports`) | ||
|
||
TypeScript 3.8 added support for type-only imports. | ||
Type-only imports allow you to specify that an import can only be used in a type location, allowing certain optimizations within compilers. | ||
|
||
## Rule Details | ||
|
||
This rule aims to standardize the use of type imports style across the codebase. | ||
|
||
## Options | ||
|
||
```ts | ||
type Options = { | ||
prefer: 'type-imports' | 'no-type-imports'; | ||
disallowTypeAnnotations: boolean; | ||
}; | ||
|
||
const defaultOptions: Options = { | ||
prefer: 'type-imports', | ||
disallowTypeAnnotations: true, | ||
}; | ||
``` | ||
|
||
### `prefer` | ||
|
||
This option defines the expected import kind for type-only imports. Valid values for `prefer` are: | ||
|
||
- `type-imports` will enforce that you always use `import type Foo from '...'`. It is default. | ||
- `no-type-imports` will enforce that you always use `import Foo from '...'`. | ||
|
||
bradzacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Examples of **correct** code with `{prefer: 'type-imports'}`, and **incorrect** code with `{prefer: 'no-type-imports'}`. | ||
|
||
```ts | ||
import type { Foo } from 'Foo'; | ||
import type Bar from 'Bar'; | ||
type T = Foo; | ||
const x: Bar = 1; | ||
``` | ||
|
||
Examples of **incorrect** code with `{prefer: 'type-imports'}`, and **correct** code with `{prefer: 'no-type-imports'}`. | ||
|
||
```ts | ||
import { Foo } from 'Foo'; | ||
import Bar from 'Bar'; | ||
type T = Foo; | ||
const x: Bar = 1; | ||
``` | ||
|
||
### `disallowTypeAnnotations` | ||
|
||
If `true`, type imports in type annotations (`import()`) is not allowed. | ||
Default is `true`. | ||
|
||
bradzacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Examples of **incorrect** code with `{disallowTypeAnnotations: true}`. | ||
|
||
```ts | ||
type T = import('Foo').Foo; | ||
const x: import('Bar') = 1; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
- 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. | ||
- If you specifically want to use both import kinds for stylistic reasons, you can disable this rule. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.