Skip to content

import-js/eslint-import-resolver-typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e5e6e6c · Jul 30, 2020

History

79 Commits
Jul 30, 2020
Sep 11, 2019
Jul 30, 2020
Jul 30, 2020
Jul 9, 2020
Sep 17, 2019
Sep 11, 2019
Sep 12, 2019
Nov 10, 2019
Jul 16, 2020
Sep 11, 2019
Sep 12, 2019
Oct 6, 2019
Sep 11, 2019
Sep 11, 2019
Sep 11, 2019
Oct 17, 2019
Feb 15, 2020
Jul 30, 2020
Apr 10, 2020
Jul 30, 2020

Repository files navigation

eslint-import-resolver-typescript

GitHub Actions type-coverage npm GitHub Release

David Peer David David Dev

Conventional Commits JavaScript Style Guide Code Style: Prettier codechecks.io

This plugin adds TypeScript support to eslint-plugin-import.

This means you can:

  • import/require files with extension .ts/.tsx!
  • Use paths defined in tsconfig.json.
  • Prefer resolve @types/* definitions over plain .js.
  • Multiple tsconfigs support just like normal.

TOC

Notice

After version 2.0.0, .d.ts will take higher priority then normal .js files on resolving node_modules packages in favor of @types/* definitions.

If you're facing some problems on rules import/default or import/named from eslint-plugin-import, do not post any issue here, because they are just working exactly as expected on our sides, take import-js/eslint-plugin-import#1525 as reference or post a new issue to eslint-plugin-import instead.

Installation

# npm
npm i -D eslint-plugin-import @typescript-eslint/parser eslint-import-resolver-typescript

# yarn
yarn add -D eslint-plugin-import @typescript-eslint/parser eslint-import-resolver-typescript

Configuration

Add the following to your .eslintrc config:

{
  "plugins": ["import"],
  "rules": {
    // turn on errors for missing imports
    "import/no-unresolved": "error"
  },
  "settings": {
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      // use <root>/tsconfig.json
      "typescript": {
        "alwaysTryTypes": true // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
      },

      // use <root>/path/to/folder/tsconfig.json
      "typescript": {
        "directory": "path/to/folder"
      },

      // Multiple tsconfigs (Useful for monorepos)

      // use a glob pattern
      "typescript": {
        "directory": "packages/*/tsconfig.json"
      },

      // use an array
      "typescript": {
        "directory": [
          "packages/module-a/tsconfig.json",
          "packages/module-b/tsconfig.json"
        ]
      },

      // use an array of glob patterns
      "typescript": {
        "directory": [
          "packages/*/tsconfig.json",
          "other-packages/*/tsconfig.json"
        ]
      }
    }
  }
}

Contributing

  • Make sure your change is covered by a test import.
  • Make sure that yarn test passes without a failure.
  • Make sure that yarn lint passes without conflicts.
  • Make sure your code changes match our type-coverage settings: yarn type-coverage.

We have GitHub Actions which will run the above commands on your PRs.

If either fails, we won't be able to merge your PR until it's fixed.