import-js/eslint-plugin-import

`import/extensions` does not like patterns with `.` in them

osama-akhter opened this issue · 2 comments

It looks like the import/extensions rule only works with the last part of a file's extension. This is a problem if, for example, you want to allow importing .d.ts files. So, given a config like this

"import/extensions": [
  "warn",
  "never",
  {
    "pattern": {
      "d.ts": "always",
      "json": "always"
    }
  }
]

This would allow .json extensions in imports but still not allow .d.ts (putting .d.ts also doesn't work). This can be important, for example, with libraries that do not export all types in TypeScript. This requires you to do something like

declare module "foo" {
  export * from "foo/dist/types/index.d.ts";
}

Version Information

"eslint": "^8.56.0",
"eslint-plugin-import": "^2.31.0",

Similar root cause as #2147

That's because a file's extension is always only the part after the last dot. A file ending in .foo.bar has only the extension .bar, the .foo is part of the filename. In other words, that's a fictional convention that some people follow (including TS, for .d.ts files) but that has nothing to do with the actual file extension.

For a library that doesn't export all types, I'd suggest using helpers to extract the types from things that are exported (Parameters<>, ReturnType<>, ThisParameterType<>, etc).

Duplicate of #2147.