eslint-plugin-import icon indicating copy to clipboard operation
eslint-plugin-import copied to clipboard

This plugin does not report `import/extensions` and `import/no-unresolved` errors on `import type …` constructs

Open kmoschcau opened this issue 1 year ago • 7 comments

Assuming we force extensions "always", I get the following results:

import type Foo from "foo"; // No errors reported.
import type { Bar } from "bar"; // No errors reported.
import Foo from "foo"; // Errors reported.
import { type Bar } from "bar"; // Errors reported.

This is not ideal, since I mostly deal with .d.mts files in the project where this occurs. And typescript enforces import type on any .d.ts and .d.mts files.

This is the .eslintrc.cjs of the project:

module.exports = {
  env: {
    es6: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended",
    "plugin:import/recommended",
    "plugin:import/typescript",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: "./tsconfig.eslint.json",
    sourceType: "module",
  },
  plugins: ["@typescript-eslint", "eslint-plugin-tsdoc", "import"],
  rules: {
    "@typescript-eslint/ban-types": "off",
    "@typescript-eslint/no-explicit-any": "off",
    "@typescript-eslint/no-namespace": ["error", { allowDeclarations: true }],
    "@typescript-eslint/no-unsafe-declaration-merging": "off", // TODO: reenable in V10
    "import/extensions": ["error", "always"],
    "tsdoc/syntax": "warn",
  },
  settings: {
    "import/parsers": {
      "@typescript-eslint/parser": [".mts", ".ts"],
    },
    "import/resolver": {
      typescript: {
        alwaysTryTypes: true,
      },
    },
  },
};

kmoschcau avatar Jul 14 '24 12:07 kmoschcau

tsc itself would fail on those, no?

ljharb avatar Jul 14 '24 15:07 ljharb

No it does not. In fact it requires import type on any declaration file imports with specific tsconfig settings.

kmoschcau avatar Jul 14 '24 18:07 kmoschcau

I can confirm tsc does not report those. Here's the rule I added to enforce use of extension in import type declarations in case that helps someone else .

        'no-restricted-syntax': ['error', {
          selector: 'ImportDeclaration[importKind="type"][source.value=/^\\.\\.?\\x2F.+\\.js$/]',
          message: 'Do not use ".js" file extension for relative import type declarations',
        }],

aduh95 avatar Aug 14 '24 09:08 aduh95

If you turn on the checkTypeImports option, it seems to work. Not very well documented, but it is mentioned here.

Rule config:

{
  rules: {
    'import/extensions': ['error', 'always', { ignorePackages: true, checkTypeImports: true }],
  },
}

Source file with error:

export type * from './types/store-context';

Source file without error:

export type * from './types/store-context.ts';

Shakeskeyboarde avatar Feb 05 '25 07:02 Shakeskeyboarde

@Shakeskeyboarde hey thanks for the hint. That would solve it for import/extensions, but not for import/no-unresolved. There is no option like that for the latter.

kmoschcau avatar Feb 05 '25 07:02 kmoschcau

@kmoschcau Ah... my typescript config does seem to report missing type imports, so I didn't see any reason to enable import/no-unresolved.

Image

Shakeskeyboarde avatar Feb 05 '25 08:02 Shakeskeyboarde

@Shakeskeyboarde if I recall correctly my motivation back then was CI, not editor checks. Might be that's why we didn't do it that way.

kmoschcau avatar Feb 05 '25 14:02 kmoschcau