-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[import/no-unused-modules] False positive while importing TS interfaces #1924
Comments
I agree, |
I think it is probably working under some conditions depending on loading order like Flow type imports but I still don't know why type import loading order is different than regular ones. I personally use regular imports to import interfaces and other types so I don't have this issue. |
It’s a best practice to only ever use import type for type values, so anything we can do to make that easier, we should do. |
I had a quick look at the code and it looks like type imports are ignored because of this line in This was introduced in #1494 so that
|
@cherryblossom000 that sounds like a great idea. Want to make a PR? |
@ljharb I'm working on it right now. However, I realised the solution I proposed above wouldn't work as the export maps are cached. I'm thinking of having a if (supportedTypes.has(specifier.type)) {
importedSpecifiers.add({name: specifier.type, isType})
}
if (specifier.type === 'ImportSpecifier') {
importedSpecifiers.add({name: specifier.imported.name, isType})
} |
That also seems reasonable. |
This is a bit more complicated than I expected. All these tests: fail if this test is removed or moved to after the other tests: In other words, the tests for files that are only imported as types only succeed when the file importing them is tested first.
This seems to be because of these lines in
For example, if we had these files:
export interface A {}
import type {A} from './a' If However, if > npx eslint a.ts b.ts
a.ts
1:1 error exported declaration 'A' not used within other modules import/no-unused-modules
✖ 1 problem (1 error, 0 warnings)
> npx eslint b.ts a.ts # no output |
The use of
import/no-unused-modules
flags raises a false error: exported declaration 'XYZ' not used within other modules when usingimport type { XYZ } from "file.ts"
notation. 🐛Source Code:
Other notes:
I could use
import { Foo } from "./file1.ts"
but this coding style conflicts with the @typescript-eslint/consistent-type-imports rule and I get a lint error:Will be really great if
import/no-unused-modules
can be upgraded to handleimport type ...
syntax. 🎉Attn. #1819
cc @nicolashenry @ljharb
The text was updated successfully, but these errors were encountered: