typescript-eslint: Bug: [consistent-type-imports] Using with isolatedModules and emitDecoratorMetadata
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.
Playground Link
Repro Code
import type { Key } from 'react';
declare function Decorator( propertyName?: string ): ParameterDecorator;
export default class Test {
async func(
@Decorator() param: Key
) {
return param;
}
}
ESLint Config
{
"parser" : "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
},
"plugins" : [ "@typescript-eslint" ],
"rules" : {
"@typescript-eslint/consistent-type-imports": [ "warn", { "prefer": "type-imports" } ]
}
}
tsconfig
{
"compilerOptions": {
"module" : "esnext",
"isolatedModules" : true,
"emitDecoratorMetadata" : true,
"experimentalDecorators": true
}
}
Expected Result
No errors showing for both the decorator and the type import
Actual Result
if you use import type, you get
ESLint: Type import "Key" is used by decorator metadata.(@typescript-eslint/consistent-type-imports)
while if you use just import, you get
TS1272: A type referenced in a decorated signature must be imported with 'import type' or a namespace import when 'isolatedModules' and 'emitDecoratorMetadata' are enabled.
Not sure if there’s a better way to do this since isolatedModules must be set
Additional Info
This doesn’t need complex setup, literally any type import will throw an error for this so I’m not sure if this needs reproduction repository
Versions
| package | version |
|---|---|
@typescript-eslint/eslint-plugin |
5.33.0 |
@typescript-eslint/parser |
5.33.0 |
TypeScript |
4.7.4 |
ESLint |
8.21.0 |
node |
16.5.0 |
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 22
- Comments: 20 (6 by maintainers)
I can reproduce this using NestJS
This error is only thrown when I use typescript ^4.8, with 4.7 I do not get that error Using also @typescript-eslint 5.35 and eslint 8.22
https://github.com/typescript-eslint/typescript-eslint/issues/5468#issuecomment-1278274384 was marked as off topic, but I’m re-posting because it is a valid short term solution if you’re blocked. From the initial example, instead of this:
Change it to this:
The lint rule will pass, and TypeScript will also be happy with isolatedModules + emitDecoratorMetadata
Unfortunately auto-fixing still won’t work, so it’s not a great long term solution. If you are converting an existing codebase, you can:
emitDecoratorMetadataoff"fixStyle": "inline-type-imports"on this ruleemitDecoratorMetadataback onYou can also make a tsconfig.eslint.json that is used just by your eslint config to do something similar:
@jiby-aurum it’s technically possible - yes - however it’s not something we do due to complexity. Configuring the tooling is already complicated enough for most users, and conditionally using type information in a rule based on a config makes it that one step more complicated.
That aside - TBH I don’t think it’s a good idea for us to even attempt to make this work in this case. Simply put - because we don’t want to be in the position of re-implementing TS’s logic.
Unfortunately as we’ve seen with this issue TS can change their logic from version to version. This would mean that in order for us to properly support every version of TS, we would need to inspect the TS version and fork to the appropriate logic. That’s truly awful for maintenance. Additionally TS’s logic can change based on the presence of other compiler options. So combined with versioning, we could need to maintain forks within forks to properly support it.
For us as volunteer maintainers, it’s just not worth the maintenance burden - regression testing old TS versions isn’t something we’ve been able to setup yet, so there’s no way for us to verify that the rule would work for past and future TS versions.
This is why I say that the preferred solution to the problem is for the rule to just ignore any imported name that is used in a location that might appear in decorator metadata. That means that everyone can continue freely using the rule in every codebase and we won’t ever suggest or autofix changes that will introduce type errors.
@bradzacher completely makes sense, was not aware of the complexity involved. For me personally your preferred solution perfectly gets the job done.
Thanks for the explanation 👍
What’s also important here, but might have been overlooked, is that the TypeScript behaviour changes with the
moduleResolution.Even with TS 5.2, with
isolatedModulesandemitDecoratorMetadatayou can still do a “full” import and not have TS complain, as long as you’re using an outdatedmoduleResolutionsetting:Even for a type of a decorated variable, TS will be fine with a “full” import – exactly how the
consistent-type-importsrule tells us to do, based on theemitDecoratorMetadata. After all, that’s the reason for having thisemitDecoratorMetadatasetting forconsistent-type-imports– if you see that the type is eventually decorated, import the whole thing.But the moment you change the
moduleResolutionto something modern, likeNode16(NodeNext), then TypeScript starts telling you that you have to change the “full” import into a lighter “type” import. And then it’s mutually exclusive to what the ESLint rule is trying to do.So after all it’s a change in TS’s behaviour, because the ESLint rule’s behaviour with
emitDecoratorMetadataused to be correct (and still is, in outdated TS projects).I have a feeling that simply turning
emitDecoratorMetadataoff (just like the previous comment suggests https://github.com/typescript-eslint/typescript-eslint/issues/5468#issuecomment-1596222250) is the only way to go (unless we want to do additional tsconfig parsing of moduleResolution).~When you use SWC the rule actually behaves correctly, because it is still the case that importing the whole object is necessary for SWC to correctly emit decorator metadata. Otherwise it’s just decorating the variables with
design:paramtypesset to an empty array.~~But if I’m using SWC, then my moduleResolution is not set to NodeNext, but to Bundler.~
~…however I thought that by changing the moduleResolution to NodeNext I will trigger the issue in this other project, but I cannot. It’s some kind of a heisenbug that I cannot put my finger on.~
~The most relevant issue to this in the TS repo I was able to find is this: https://github.com/microsoft/TypeScript/issues/54717~
~But I wasn’t able to find any data that explicitly states that the behaviour of
import typevsimporthas changed for Node16.~EDIT: I got most of the SWC part completely wrong. The 1272 error happens when all of these are met:
emitDecoratorMetadataisolatedModulesmoduleResolutionset toNode16,NodeNextorBundlerSo yeah, if you’re:
tscthen you’re probably better off with a separate
tsconfig.jsonfor ESLint withemitDecoratorMetadataset to false, so that the auto-fix supported by TypeScript does the job properly and does not clash with the ESLint exception to the rule.Or… not using this ESLint rule at all?
@bradzacher https://github.com/HaoboZ/consistent-type-imports-error