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

https://typescript-eslint.io/play/#ts=4.7.4&sourceType=module&code=JYWwDg9gTgLgBDAnmApnA3nA0ixcC+cAZlBCHAORQoCGAxjBQNwBQLAJinQDY3XEBXAHYNgEIXAAiXaDRjQAFGFKpYiAHI0QKAPwAuOAGcYUYEIDmASgMAFPlpQwUUaXVnyorFigAekWHCcRDQC3PA8NIaGcAAqKMYYbHBwkYgigiIKLMnJAAKu7oqWcGD2IAY4iNlwxejVydQwAlASpVBarMn4bN1AA&eslintrc=N4KABGBECmAeAu0B2ATAzpAXGA2uCUADgDYCuA5gJZKYAC8AnodGgMYBOlh8AtC8dXgB6dtFYB7ALaTkKaCkj4AugBp8kQgEN2aaOyxR6TFhy69+goVp17IaiBu272AeW6VxSDNlAEi7cQArMXgDSAA6IXg2TwAzSnJwwLRPRQgAX3siMiovAzw-SCNmNk5uPjQBJFDlLMh2UmIWA18CIsYS03KLaqEJL0o0RGqeDugeSklCcXZo-PxCgHdtJDsFgla-B0JRWNtsSDGJqZm59Yz1pXx0kHSgA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEk8sAJgK4YVTolHoAeekANOBqRkJWKgCGedAwCyjFhzZ5MTdIJrt4yPABF0cTFNiZZ6PBIZSJy1eqG0eiLMnjoiF1PsPHMZSipqQgC+IMFAA

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)

Most upvoted comments

I can reproduce this using NestJS

image image

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:

import type { Key } from 'react';

Change it to this:

import { type Key } from 'react';

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:

  1. Turn emitDecoratorMetadata off
  2. Set "fixStyle": "inline-type-imports" on this rule
  3. Run lint fix
  4. Turn emitDecoratorMetadata back on

#5468 (comment) 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:

import type { Key } from 'react';

Change it to this:

import { type Key } from 'react';

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:

1. Turn `emitDecoratorMetadata` off

2. Set `"fixStyle": "inline-type-imports"` on this rule

3. Run lint fix

4. Turn `emitDecoratorMetadata` back on

You can also make a tsconfig.eslint.json that is used just by your eslint config to do something similar:

{
  // eslint uses this file to workaround https://github.com/typescript-eslint/typescript-eslint/issues/5468
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "emitDecoratorMetadata": false
  },
}

@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 isolatedModules and emitDecoratorMetadata you can still do a “full” import and not have TS complain, as long as you’re using an outdated moduleResolution setting:

  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "isolatedModules": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
}

Even for a type of a decorated variable, TS will be fine with a “full” import – exactly how the consistent-type-imports rule tells us to do, based on the emitDecoratorMetadata. After all, that’s the reason for having this emitDecoratorMetadata setting for consistent-type-imports – if you see that the type is eventually decorated, import the whole thing.

But the moment you change the moduleResolution to something modern, like Node16 (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 emitDecoratorMetadata used to be correct (and still is, in outdated TS projects).

I have a feeling that simply turning emitDecoratorMetadata off (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:paramtypes set 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 type vs import has changed for Node16.~


EDIT: I got most of the SWC part completely wrong. The 1272 error happens when all of these are met:

  • the project uses emitDecoratorMetadata
  • the project uses isolatedModules
  • the project uses moduleResolution set to Node16, NodeNext or Bundler
  • the imported type is referenced in a decorated signature
  • the imported type is just a type, that is – there’re no runtime objects defined under the name you’re importing

So yeah, if you’re:

  • creating a new TS project with sane defaults
  • AND using some framework/library that relies on decorators
  • AND you’re thinking about using a different transpiler/bundler to tsc

then you’re probably better off with a separate tsconfig.json for ESLint with emitDecoratorMetadata set 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?