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

πŸ› noUnusedImports does not detect used types #557

Closed
1 task done
EvHaus opened this issue Oct 20, 2023 · 6 comments Β· Fixed by #566
Closed
1 task done

πŸ› noUnusedImports does not detect used types #557

EvHaus opened this issue Oct 20, 2023 · 6 comments Β· Fixed by #566
Assignees
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug

Comments

@EvHaus
Copy link

EvHaus commented Oct 20, 2023

Environment information

Incompatible Biome Server: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

β„Ή Rage discovered this running server using an incompatible version of Biome.

Server:
  Version:                      1.2.2

What happened?

When the nursery/noUnusedImports rule is used in a file where a type is imported, it will detect that import as unused (even though it's actually used).

import type NextAuth from 'next-auth/next';
const a: typeof NextAuth = {}

Expected result

Because the import is actually used, it should not be flagged for removal.

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@Conaclos
Copy link
Member

This is intentional. typeof can only be applied on a value, not a type. Are you sure that your code is correct? How is defined NextAuth?

@netner-d
Copy link

netner-d commented Oct 20, 2023

@Conaclos typeof can be applied to a variable imported as a type

@netner-d
Copy link

I have the same issue on my side

import type * as Config from './objects/config'; // -> Config is reported as unused
type ConfigCheck<T extends CheckObject<Omit<typeof Config, never>> = Omit<typeof Config, never>> =
  T;

@Conaclos
Copy link
Member

Conaclos commented Oct 20, 2023

In my view, Config should be imported as a value:

import * as Config from './objects/config'; // -> Config is reported as unused
type ConfigCheck<T extends CheckObject<Omit<typeof Config, never>> = Omit<typeof Config, never>> =
  T;

Taking some time to investigate, it seems that TypeScript allows the use of a type in typeof in only one case: if the type is imported. This could avoid conflicts with verbatimModuleSyntax mode. I have to investigate more to be sure about that.

@Conaclos Conaclos added S-Bug-confirmed Status: report has been confirmed as a valid bug A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Oct 20, 2023
@Conaclos Conaclos self-assigned this Oct 20, 2023
@EvHaus
Copy link
Author

EvHaus commented Oct 21, 2023

Maybe we need a rule for detecting when you're unnecessarily using typeof on an imported type :)

Although, TypeScript yells at me if I try to remove it:

import type NextAuth from 'next-auth/next';

// 'NextAuth' refers to a value, but is being used as a type here. Did you mean 'typeof NextAuth'?
const a: NextAuth = {}

This works though:

import NextAuth from 'next-auth/next';
const a: typeof NextAuth = {}

@Conaclos
Copy link
Member

Conaclos commented Oct 21, 2023

Yes this conflicts with verbatimModuleSyntax.
I shipped a fix on main which allows using imported type in a typeof.
You can test on the playground that the fix solves your issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants