Skip to content

Allow resolving .js module path endings to .ts equivalent #82

Closed
@patrickarlt

Description

@patrickarlt

Moving this from import-js/eslint-plugin-import#2111 (comment) after import-js/eslint-plugin-import#2111 (comment).

It looks like this was previously discussed in #80 and it was requested that an issue get filed in eslint-plugin-import. import-js/eslint-plugin-import#2111 discusses this but it was recommended that this functionality move to eslint-import-resolver-typescript.

Below is my original comment from the eslint-plugin-import issue slightly modified for clarity.


I'm migrating a large project written in TypeScript to output native ESM modules for Node. This requires all the import statements in TypeScript to end in .js so they get output with the .js extensions and I would like to lint that import statements of file paths end in .js.

I don't think TypeScript is going to budge on their treatment of extensions, see microsoft/TypeScript#16577 (comment), neither will Node JS back off of required extensions for ESM so it is perfectly valid TypeScript to write this if you are compiling to ESM for Node:

// foo.ts
export const foo = 'foo';

// bar.ts
import { foo } from './foo.js';

Compiles to:

// foo.js
export const foo = 'foo';

// bar.js
import { foo } from './foo.js';

Currently this config produces Missing file extension "ts" for "./foo.js" which is illogical since TypeScript doesn't actually support .ts on file paths microsoft/TypeScript#37582.

module.exports = {
  root: true,
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: "module"
  },
  plugins: ["@typescript-eslint", "import"],
  extends: ["plugin:import/recommended", "plugin:import/typescript"],
  rules: {
    "import/extensions": ["error", "ignorePackages"]
  },
  settings: {
    "import/extensions": [".js", ".jsx"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      typescript: {
        project: "./"
      }
    }
  }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions