
Description
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:
remove
: Don't transpileimport type
definitions, not loading any stateful changes (default)preserve
: Transpileimport type
definitions, loading any stateful changeserror
: Error when plainimport
s 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';