typescript-eslint: Bug: In v8, no-unused-vars says "defined but only used as type" on import with no escape hatch
Before You File a Bug Report Please Confirm You Have Done The Following…
- I have tried restarting my IDE and the issue persists.
- I have updated to the latest version of the packages.
- I have searched for related issues and found none that matched my issue.
- I have read the FAQ and my problem is not listed.
Issue Description
With code like:
import { SomeType } from "./someModule";
export const value = 1234 as typeof SomeType;
This code is flagged as having an unused variable in v8 with a new message, something like:
/home/jabaile/work/TypeScript/src/harness/client.ts
53:5 warning 'PatternMatchKind' is defined but only used as a type. Allowed unused vars must match /^(_+$|_[^_])/u @typescript-eslint/no-unused-vars
The TS repo canonically does not use type-only imports (we cannot use verbatimModuleSyntax), so it’s not desirable to make this a type-only import. But, there’s no way to opt out of this new detection for imports.
Reproduction Repository Link
https://github.com/jakebailey/TypeScript
Repro Steps
- Clone https://github.com/jakebailey/TypeScript/tree/eslint-9
npm cinpx eslint src/harness/client.ts
/home/jabaile/work/TypeScript/src/harness/client.ts
53:5 warning 'PatternMatchKind' is defined but only used as a type. Allowed unused vars must match /^(_+$|_[^_])/u @typescript-eslint/no-unused-vars
Versions
| package | version |
|---|---|
@typescript-eslint/eslint-plugin |
8.0.0-alpha.59 |
@typescript-eslint/parser |
8.0.0-alpha.59 |
@typescript-eslint/scope-manager |
8.0.0-alpha.59 |
@typescript-eslint/typescript-estree |
8.0.0-alpha.59 |
@typescript-eslint/type-utils |
8.0.0-alpha.59 |
@typescript-eslint/utils |
8.0.0-alpha.59 |
TypeScript |
5.4.4 |
ESLint |
9.8.0 |
node |
22.5.1 |
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 7
- Comments: 15 (9 by maintainers)
I have a related use case that in my code base causes the “warning ‘z$foo’ is assigned a value but only used as a type @typescript-eslint/no-unused-vars” error when for example using the zod library. I get this error every time a zod definition it already used to infer the type but (not yet) to parse. I my code baseI get this error a few hundred times and it would be most helpful to have an option that exclude this specific use case of unused variable.
playground
This is an oversight – #9330 should not have applied to imports! Imports are “dual value/type” variables in the scope analysis and so they should not be reported on by the
typeofcheck.That doesn’t really seem like a good way to ignore the problem… If we then go to use that import (it’s an enum), we’d need to rename it back.
To me,
no-unused-varis overstepping; this is already enforced byconsistent-type-importsorverbatimModuleSyntaxas switching it totype SomeTypewould also fix the problem (Playground Link).Cool, thanks. That looks like a separate issue from this one, so I’d ask a separate issue filed out with a good reproduction case & explanation of that situation. Thanks!