Skip to content

Commit

Permalink
feat(eslint-plugin): make the requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Aug 11, 2020
1 parent fe60024 commit bb913f0
Show file tree
Hide file tree
Showing 3 changed files with 1,338 additions and 115 deletions.
56 changes: 34 additions & 22 deletions packages/eslint-plugin/docs/rules/consistent-type-imports.md
@@ -1,33 +1,19 @@
# 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.

```ts
import type { Foo } from './foo';
let foo: Foo;
```

```ts
import { Foo } from './foo';
let foo: Foo;
```

```ts
let foo: import('foo').Foo;
```

## Options

```ts
type Options =
| 'type-imports'
| 'no-type-imports'
| {
prefer: 'type-imports' | 'no-type-imports';
disallowTypeAnnotations: boolean;
};
type Options = {
prefer: 'type-imports' | 'no-type-imports';
disallowTypeAnnotations: boolean;
};

const defaultOptions: Options = {
prefer: 'type-imports',
Expand All @@ -42,11 +28,37 @@ This option defines the expected import kind for type-only imports. Valid values
- `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 '...'`.

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`.

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 specifically want to use both import kinds for stylistic reasons, you can disable this rule.
- 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.

0 comments on commit bb913f0

Please sign in to comment.