eslint-import-resolver-typescript (original) (raw)

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:

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:

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:

conditionNames

Default:

extensions

Default:

extensionAlias

Default:

mainFields

Default:

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

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 and Backers

Sponsors and Backers

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