Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-import-resolver-typescript

import-js45.4mISC4.3.1TypeScript support: included

This plugin adds TypeScript support to eslint-plugin-import

typescript, eslint, import, resolver, plugin

readme

eslint-import-resolver-typescript

GitHub Actions Workflow Status type-coverage npm GitHub Release

Conventional Commits Renovate enabled JavaScript Style Guide Code Style: Prettier changesets

This is a resolver for eslint-plugin-import(-x) plugin, not an ESLint plugin itself, it adds TypeScript support to eslint-plugin-import (Or maybe you want to try eslint-plugin-import-x for faster speed)

This means you can:

  • import/require files with extension .cts/.mts/.ts/.tsx/.d.cts/.d.mts/.d.ts
  • Use paths defined in tsconfig.json
  • Prefer resolving @types/* definitions over plain .js/.jsx
  • Multiple tsconfigs support just like normal
  • imports/exports fields support in package.json

TOC

Notice

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

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

eslint-plugin-import-x

# npm
npm i -D eslint-plugin-import-x eslint-import-resolver-typescript

# pnpm
pnpm i -D eslint-plugin-import-x eslint-import-resolver-typescript

# yarn
yarn add -D eslint-plugin-import-x eslint-import-resolver-typescript

eslint-plugin-import

# npm
npm i -D eslint-plugin-import eslint-import-resolver-typescript

# pnpm
pnpm i -D eslint-plugin-import eslint-import-resolver-typescript

# yarn
yarn add -D eslint-plugin-import eslint-import-resolver-typescript

Configuration

eslint.config.js

If you are using eslint-plugin-import-x@>=4.5.0, you can use import/require to reference eslint-import-resolver-typescript directly in your ESLint flat config:

// eslint.config.js, CommonJS is also supported
import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript'

export default [
  {
    settings: {
      'import-x/resolver-next': [
        createTypeScriptImportResolver({
          alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`

          bun: true, // resolve Bun modules https://github.com/import-js/eslint-import-resolver-typescript#bun

          // Choose from one of the "project" configs below or omit to use <root>/tsconfig.json or <root>/jsconfig.json by default

          // use <root>/path/to/folder/tsconfig.json or <root>/path/to/folder/jsconfig.json
          project: 'path/to/folder',

          // Multiple tsconfigs/jsconfigs (Useful for monorepos, but discouraged in favor of `references` supported)

          // use a glob pattern
          project: 'packages/*/{ts,js}config.json',

          // use an array
          project: [
            'packages/module-a/tsconfig.json',
            'packages/module-b/jsconfig.json',
          ],

          // use an array of glob patterns
          project: [
            'packages/*/tsconfig.json',
            'other-packages/*/jsconfig.json',
          ],
        }),
      ],
    },
  },
]

But if you are using eslint-plugin-import or the older version of eslint-plugin-import-x, you can't use require/import:

// eslint.config.js, CommonJS is also supported
export default [
  {
    settings: {
      'import/resolver': {
        typescript: {
          alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`

          bun: true, // resolve Bun modules https://github.com/import-js/eslint-import-resolver-typescript#bun

          // Choose from one of the "project" configs below or omit to use <root>/tsconfig.json or <root>/jsconfig.json by default

          // use <root>/path/to/folder/tsconfig.json or <root>/path/to/folder/jsconfig.json
          project: 'path/to/folder',

          // Multiple tsconfigs/jsconfigs (Useful for monorepos, but discouraged in favor of `references` supported)

          // use a glob pattern
          project: 'packages/*/{ts,js}config.json',

          // use an array
          project: [
            'packages/module-a/tsconfig.json',
            'packages/module-b/jsconfig.json',
          ],

          // use an array of glob patterns
          project: [
            'packages/*/tsconfig.json',
            'other-packages/*/jsconfig.json',
          ],
        },
      },
    },
  },
]

.eslintrc

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": {
      "typescript": {
        "alwaysTryTypes": true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`

        "bun": true, // resolve Bun modules https://github.com/import-js/eslint-import-resolver-typescript#bun

        // Choose from one of the "project" configs below or omit to use <root>/tsconfig.json or <root>/jsconfig.json by default

        // use <root>/path/to/folder/tsconfig.json or <root>/path/to/folder/jsconfig.json
        "project": "path/to/folder",

        // Multiple tsconfigs (Useful for monorepos, but discouraged in favor of `references` supported)

        // use a glob pattern
        "project": "packages/*/{ts,js}config.json",

        // use an array
        "project": [
          "packages/module-a/tsconfig.json",
          "packages/module-b/jsconfig.json",
        ],

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

Other environments

Bun

Bun provides built-in modules such as bun:test, which are not resolved by default.

Enable Bun built-in module resolution by choosing 1 out of these 3 options:

Options from unrs-resolver

conditionNames

Default:

[
  "types",
  "import",

  // APF: https://angular.io/guide/angular-package-format
  "esm2020",
  "es2020",
  "es2015",

  "require",
  "node",
  "node-addons",
  "browser",
  "default",
]

extensions

Default:

[
  // `.mts`, `.cts`, `.d.mts`, `.d.cts`, `.mjs`, `.cjs` are not included because `.cjs` and `.mjs` must be used explicitly
  ".ts",
  ".tsx",
  ".d.ts",
  ".js",
  ".jsx",
  ".json",
  ".node",
]

extensionAlias

Default:

{
  ".js": [
    ".ts",
    // `.tsx` can also be compiled as `.js`
    ".tsx",
    ".d.ts",
    ".js",
  ],
  ".jsx": [".tsx", ".d.ts", ".jsx"],
  ".cjs": [".cts", ".d.cts", ".cjs"],
  ".mjs": [".mts", ".d.mts", ".mjs"],
}

mainFields

Default:

[
  "types",
  "typings",

  // APF: https://angular.io/guide/angular-package-format
  "fesm2020",
  "fesm2015",
  "esm2020",
  "es2020",

  "module",
  "jsnext:main",

  "main",
]

Other options

You can pass through other options of unrs-resolver directly

Default options

You can reuse defaultConditionNames, defaultExtensions, defaultExtensionAlias and defaultMainFields by require/import them directly

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.

Sponsors

Sponsors

1stG RxTS UnTS
1stG Open Collective sponsors RxTS Open Collective sponsors UnTS Open Collective sponsors

Backers

1stG RxTS UnTS
1stG Open Collective backers RxTS Open Collective backers UnTS Open Collective backers

Changelog

Detailed changes for each release are documented in CHANGELOG.md.

License

ISC

changelog

Changelog

4.3.1

Patch Changes

4.3.0

Minor Changes

4.2.7

Patch Changes

4.2.6

Patch Changes

4.2.5

Patch Changes

4.2.4

Patch Changes

  • #407 6b183ff Thanks @JounQin! - chore: migrate to rebranding unrs-resolver with new targets supported:

    • i686-pc-windows-msvc
    • armv7-unknown-linux-musleabihf
    • powerpc64le-unknown-linux-gnu
    • s390x-unknown-linux-gnu

4.2.3

Patch Changes

  • #402 f21bf15 Thanks @SunsetTechuila! - fix: don't resolve not implemented node modules in bun

    is-bun-module is marked as dependency, again, for correctness, see isBunImplementedNodeModule for more details

    For Bun users: you don't need to install is-bun-module any more but bun: true option is still required if you're running without bun --bun nor run#bun enabled

4.2.2

Patch Changes

  • #397 14a7688 Thanks @JounQin! - chore: bump rspack-resolver for better P'n'P support

    Now rspack-resolver resolves pnpapi natively.

4.2.1

Patch Changes

4.2.0

Minor Changes

  • #391 c8121e5 Thanks @JounQin! - feat: make is-bun-module as optional peer dependency

    Technically this is a BREAKING CHANGE, but considering we just raise out v4 recently and this only affects bun users, bun --bun eslint even works without this dependency, so I'd consider this as a minor change.

    So for bun users, there are three options:

    1. install is-bun-module dependency manually and use bun: true option
    2. run eslint with bun --bun eslint w/o bun: true option
    3. enable run#bun in bunfig.toml w/o bun: true option

4.1.1

Patch Changes

  • #389 1b97d8a Thanks @JounQin! - fix: should prefer module.isBuiltin when process.versions.bun available

4.1.0

Minor Changes

  • #387 ef5cd10 Thanks @JounQin! - feat: add a new bun?: boolean option for bun users - close #386

    process.versions.bun is unavailable even with bun eslint due to its own design, but checking bun modules for non-bun users is incorrect behavior and just wasting time, so a new option is added for such case, you can still run with bun --bun eslint without this option enabled

4.0.0

Major Changes

  • #368 2fd7c2e Thanks @JounQin! - feat!: rewrite, speed up by using rspack-resolver which supports references natively under the hood

    BREAKING CHANGES:

    • drop Node 14 support, Node ^16.17.0 || >=18.6 is now required
    • alwaysTryTypes is enabled by default, you can set it as false to opt-out
    • array type of project is discouraged but still supported, single project with references are encouraged for better performance, you can enable noWarnOnMultipleProjects option to supress the warning message
    • root tsconfig.json or jsconfig.json will be used automatically if no project provided

3.9.1

Patch Changes

3.9.0

Minor Changes

3.8.7

Patch Changes

3.8.6

Patch Changes

  • #374 c9d5ab0 Thanks @JounQin! - fix: add support for importing with .js extension as tsx importee

3.8.5

Patch Changes

  • #372 366eeaf Thanks @carlocorradini! - fix: if file has no corresponding mapper function, apply all of them, starting with the nearest one.

3.8.4

Patch Changes

3.8.3

Patch Changes

3.8.2

Patch Changes

3.8.1

Patch Changes

3.8.0

Minor Changes

  • #345 fcc8883 Thanks @carlocorradini! - Enable the mapper function just for a set of allowed files. Improves project discovery using glob and POSIX separator.

  • #346 c124e87 Thanks @carlocorradini! - Update get-tsconfig to the the latest version. We now support the ${configDir} variable, introduced in TypeScript 5.5.

3.7.0

Minor Changes

  • #326 93ea130 Thanks @SukkaW! - This version has implemented the eslint-plugin-import-x's v3 resolver interface. This allows you to use import/require to reference eslint-import-resolver-typescript directly in your ESLint flat config:

    Previously

    // eslint.config.js
    module.exports = {
      settings: {
        'import-x/resolver': {
          typescript: {
            alwaysTryTypes: true,
          },
          // or
          require.resolve('eslint-import-resolver-typescript'):
            alwaysTryTypes: true,
          }
        }
      }
    }

    Now

    // eslint.config.js
    const {
      createTypeScriptImportResolver,
    } = require('eslint-import-resolver-typescript')
    
    module.exports = {
      settings: {
        'import-x/resolver-next': [
          createTypeScriptImportResolver({
            alwaysTryTypes: true,
          }),
        ],
      },
    }

    Note that this only works with eslint-plugin-import-x@>=4.5.0. You can't use createTypeScriptImportResolver with the older versions of eslint-plugin-import-x or any existing versions of eslint-plugin-import.

3.6.3

Patch Changes

3.6.2

Patch Changes

3.6.1

Patch Changes

3.6.0

Minor Changes

3.5.5

Patch Changes

3.5.4

Patch Changes

3.5.3

Patch Changes

  • #206 6531bad Thanks @marvinhagemeister! - Only try to resolve a module directory when we know that the path is a directory. This can lead to a 15% speedup on projects with many files.

3.5.2

Patch Changes

3.5.1

Patch Changes

3.5.0

Minor Changes

Patch Changes

3.4.2

Patch Changes

3.4.1

Patch Changes

3.4.0

Minor Changes

3.3.0

Minor Changes

  • #154 42f2dd6 Thanks @JounQin! - feat: add externsionAlias option support, .d.([cm]?ts|tsx) are always preferred than .([cm]?js|jsx)

    typescript resolves typescript/lib/typescript.d.ts instead of typescript/lib/typescript.js by default

  • #154 42f2dd6 Thanks @JounQin! - feat: exports globSync, defaultExtensions, defaultMainFields, defaultConditionNames and defaultExtensionAlias for reusing

Patch Changes

3.2.7

Patch Changes

3.2.6

Patch Changes

3.2.5

Patch Changes

3.2.4

Patch Changes

3.2.3

Patch Changes

3.2.2

Patch Changes

3.2.1

Patch Changes

3.2.0

Minor Changes

3.1.5

Patch Changes

3.1.4

Patch Changes

3.1.3

Patch Changes

3.1.2

Patch Changes

3.1.1 (2022-06-27)

Bug Fixes

3.1.0 (2022-06-25)

⚠ BREAKING CHANGES

  • use enhanced-resolve instead

Features

  • support angular-package-format out of box (7e0cd04)
  • use enhanced-resolve instead (39ab8b1), closes #85 #107

3.0.0 (2022-06-25)

⚠ BREAKING CHANGES

  • remove depracated directory option
  • use get-tsconfig to replace tsconfig-paths
  • bump globby, use synckit for sync fn
  • deps: bump tsconfig-paths to ^4.0.0 (#104)

Features

  • bump globby, use synckit for sync fn (322cb29)
  • ignore node_modules folder in projects option glob (#105) (1e1b5a6)
  • remove depracated directory option (67c8d59)
  • use get-tsconfig to replace tsconfig-paths (78a08e0)

  • deps: bump tsconfig-paths to ^4.0.0 (#104) (b2edbc8)

2.7.1 (2022-04-03)

Bug Fixes

2.7.0 (2022-03-23)

Features

  • support .cjs .mjs .cts .mts extensions (#84) (1e39028)

2.6.0 (2022-03-23)

Bug Fixes

2.5.0 (2021-09-13)

Features

  • allow passing through custom options to resolve (#79) (34c94c8)

Bug Fixes

  • bump (dev)Dependencies, apply stricter rules (#75) (866f32f)

2.4.0 (2021-02-16)

Features

  • remove any querystring from imports (#67) (82ef357)

Bug Fixes

  • remove .tsbuildinfo and d.ts.map files from package (#57) (15f2849)
  • remove redundant condition (#69) (ba62e65)

2.3.0 (2020-09-01)

Features

  • import with .js and .jsx file extensions (#56) (5340f96)

2.2.1 (2020-08-14)

Bug Fixes

  • replace postintall with prepare - fix #54 (f3ffd16)

2.2.0 (2020-07-30)

Features

  • rename option directory to project - close #23 (a662fc1)

2.1.0 (2020-07-30)

Bug Fixes

2.0.0 (2019-10-17)

Features

  • add alwaysTryTypes option, add tests (fe0aa6f)
  • replace glob with tiny-glob for faster speed, close #12 (f436627)
  • replace glob with tiny-glob for faster speed, close #12 (#13) (5f87698)
  • resolve .ts/.tsx/.d.ts first, and then fallback to @types/* (b11ede3)
  • support scoped packages from DefinitelyTyped (b4e72a5)
  • use types/typings/module first to use .d.ts whenever possible (74de3d9)

Bug Fixes

  • add pretest script which is required (1ffcd83)
  • deps: bump configurations, use resolutions to simplify tests (5eb4874)
  • only check alwaysTryTypes if foundNodePath is null (23e2e8c)