eslint-import-resolver-typescript icon indicating copy to clipboard operation
eslint-import-resolver-typescript copied to clipboard

Missing file extension "ts" for [...] / passing through `enhanced-resolve`'s options.

Open Patryk-B opened this issue 2 years ago • 9 comments

I am trying to supply eslint-import-resolver-typescript with custom extensionAlias option. Is this the correct way:

{
  "plugins": ["import"],
  "settings": {
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      "typescript": {
        "alwaysTryTypes": true,
        "project": "./tsconfig.json",
        
        "extensionAlias": {

          ".js": [ ".ts", ".d.ts", ".js" ],
          ".cjs": [ ".cts", ".d.cts", ".cjs" ],
          ".mjs": [ ".mts", ".d.mts", ".mjs" ],

          ".ts": [ ".js", ".d.ts", ".ts" ],
          ".cts": [ ".cjs", ".d.cts", ".cts" ],
          ".mts": [ ".mjs", ".d.mts", ".mts" ],
        }
      }
    }
  },
  "rules": {
    "import/extensions": [
      "error"
      "always",
      {
	    "ignorePackages": false
      }
    ]
  }
}

bc it doesn't seem to work...

What am i trying to achieve?

I am trying to fix Missing file extension "ts" for [...] linting error from import/extensions rule by trying to "teach" eslint-import-resolver-typescript resolver to treat ".js" as aliases for ".ts" extension.

Patryk-B avatar Jul 09 '23 02:07 Patryk-B

Seems related to https://github.com/import-js/eslint-plugin-import/pull/2813

JounQin avatar Jul 09 '23 05:07 JounQin

@JounQin that PR needs your feedback - could you jump in over in https://github.com/import-js/eslint-plugin-import/pull/2813#pullrequestreview-1511653449 please? :)

phryneas avatar Jul 09 '23 09:07 phryneas

Sorry to bother you @JounQin & @phryneas, obviously I am completely green. I don't know how both typescript resolver and import/extensions rule cooperate to lint the code. I am not trying to question anything. I am simply curious.

Why is it necessary to specialize the import/extensions rule for this single edge case?

Shouldn't the import/extensions rule ask the typescript resolver if path “@src/foo/foo.js” is valid (aka. resolves to “<root>/src/foo/foo.ts”) instead of validating the path/extension?

Why can't the typescript resolver do that job?

AFAIK typescript resolver heavily depends on the webpack's enhanced-resolve, why passing through relevant configuration (the extensionAlias option) doesn't produce the correct result?

Patryk-B avatar Jul 09 '23 19:07 Patryk-B

@Patryk-B in short, in the past:

  • import/extensions looks at source code, sees import foo from "file"
  • import/extensions asks eslint-import-resolver-typescript "hey, what's the real path for "file"?
  • eslint-import-resolver-typescript responds with file.ts
  • import/extensions complains because the user is not really importing the file name with the right extension that it has on disk

Now, with importing a .js file with a .ts extension on disk:

  • import/extensions looks at source code, sees import foo from "file.js"
  • import/extensions asks eslint-import-resolver-typescript "hey, what's the real path for "file.js"?
  • eslint-import-resolver-typescript responds with file.ts
  • import/extensions complains because the user is not really importing the file name with the right extension that it has on disk

=> eslint-import-resolver-typescript is aware that importing .js is actually correct for a .ts file on disk. import/extensions doesn't know that yet.

phryneas avatar Jul 09 '23 21:07 phryneas

@phryneas, understood. Thank you very much for your in-depth explanation. :bow::bow::bow::bow:

Patryk-B avatar Jul 09 '23 22:07 Patryk-B

Thank you @phryneas for the detailed explanation. Would you know a way to avoid the error from import/extensions without disabling the rule?

fernandopasik avatar Feb 01 '24 00:02 fernandopasik

@fernandopasik you've seen my other comment?

phryneas avatar Feb 01 '24 07:02 phryneas

@phryneas You're original PR won't PR work with createTypeScriptImportResolver v3 interface for eslint-plugin-import-x now sadly.

See aslo https://github.com/import-js/eslint-import-resolver-typescript#eslintconfigjs

@Patryk-B

import/extensions setting is required to be set manually in the mean time.

See also https://github.com/import-js/eslint-plugin-import#importextensions

We'll improve this in eslint-plugin-import-x plugin, if @phryneas want to port https://github.com/import-js/eslint-plugin-import/pull/2813, that would be appreciated.

JounQin avatar Mar 17 '25 14:03 JounQin

I'm sorry, but I don't have a need anymore, since we changed the tsconfig moduleResolution setting and now TypeScript already highlights these kinds of errors for us.

phryneas avatar Mar 17 '25 20:03 phryneas