Skip to content
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

Rule suggestion: Prefer "import type" #1786

Closed
ghost opened this issue Mar 23, 2020 · 1 comment
Closed

Rule suggestion: Prefer "import type" #1786

ghost opened this issue Mar 23, 2020 · 1 comment
Labels
awaiting response Issues waiting for a reply from the OP or another party package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@ghost
Copy link

ghost commented Mar 23, 2020

Mostly separating this out from #1436 so it can have a dedicated issue.

TypeScript 3.8 adds an import type syntax which allows you to import type definitions only, for ease of transpilation via tools which don't have type checking like babel. For transpilers, these definitions are ignored and removed on transpilation.

The newer compiler also has an importsNotUsedAsValues compiler option to clarify whether you want to enforce the use of import type syntax, with the three options:

  1. remove: Don't transpile import type definitions, not loading any stateful changes (default)
  2. preserve: Transpile import type definitions, loading any stateful changes
  3. error: Error when plain imports are for types only, requiring a separation of stateful/value imports and type imports

That latter option looks like a good candidate for a rule in typescript-eslint. Although the compiler will warn on these errors, an eslint rule can also have an applicable fix as well. This is also not a possible rule for eslint-plugin-import, as it requires type checking.

This rule, when enabled, will match the behaviour of the error variant of the importsNotUsedAsValues flag, providing an applicable fix.

Example code:

import { Type, Class, CONST } from 'module';

function doThing(value: Type): void {
    if (value instanceof Class) {
        doAnotherThing(value);
    } else if (value === CONST) {
        doDifferentThing(value)
    } else {
        doUsualThing(value);
    }
}

Suggested fix:

-import { Type, Class, CONST } from 'module';
+import type { Type } from 'module';
+import { Class, CONST } from 'module';
@ghost ghost added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Mar 23, 2020
@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for maintainers to take a look labels Mar 23, 2020
@bradzacher
Copy link
Member

Thanks for the suggestion!

This would be better as work in the TS language service.
The TS language service has the ability to tell the IDE about fixes are available for a specific TS error.

For example, I added a new TS error for invalid JSX characters, and added a fixer to the language service here: microsoft/TypeScript#36636

If we were to implement this in the plugin, we would have to duplicate TS's detection logic.
Additionally, this would be implementing a rule which duplicates an existing TS error - which is something that we explicitly avoid doing in this plugin.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

1 participant