eslint-import-resolver-typescript
这是 eslint-plugin-import(-x) 插件的解析器,本身并非 ESLint 插件,它为 eslint-plugin-import 添加了 TypeScript 支持。(或者你可以尝试 eslint-plugin-import-x 以获得更快的速度)
这意味着你可以:
- 解析扩展名为
.cts/.mts/.ts/.tsx/.d.cts/.d.mts/.d.ts的import/require文件 - 使用在
tsconfig.json中定义的paths] - 优先解析
@types/*定义,而非普通的.js/.jsx - 支持多个 tsconfig,与普通情况相同
- 支持
package.json中的imports/exports字段
目录
注意事项
在 2.0.0 版本之后,解析 node_modules 包时,.d.ts 将优先于普通的 .js/.jsx 文件,以支持 @types/* 定义或其自身定义。
如果你在使用 eslint-plugin-import 中的规则 import/default 或 import/named 时遇到一些问题,请不要在此处提交 issue,因为它们在 我们这边是符合预期的。请参考 import-js/eslint-plugin-import#1525 或在 eslint-plugin-import 上提交新的 issue。
安装
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
# bun
bun 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
# bun
bun add -d eslint-plugin-import eslint-import-resolver-typescript
配置
eslint.config.js
如果你正在使用 eslint-plugin-import-x@>=4.5.0,你可以使用 import/require 在你的 ESLint flat config 中直接引用 eslint-import-resolver-typescript:
// 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 if 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',
],
}),
],
},
},
]
但如果你使用的是 eslint-plugin-import 或 eslint-plugin-import-x 的旧版本,则无法使用 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 if 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
将以下内容添加到您的 .eslintrc 配置中:
{
"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 if 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",
],
},
},
},
}
其他环境
Bun
Bun 提供了诸如 bun:test 之类的内置模块,这些模块默认不会被解析。
通过选择以下 3 个选项中的 1 个,启用 Bun 内置模块解析:
- 设置
bun: true选项,如上文 配置 所示。 - 使用
bun --bun eslint运行 ESLint。 - 在
bunfig.toml](https://bun.sh/docs/runtime/bunfig#run-bun-auto-alias-node-to-bun) 中 [配置run.bun。
来自 unrs-resolver 的选项
conditionNames
默认:
[
"types",
"import",
// APF: https://angular.io/guide/angular-package-format
"esm2020",
"es2020",
"es2015",
"require",
"node",
"node-addons",
"browser",
"default",
]
extensions
默认:
[
// `.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
默认:
{
".js": [
".ts",
// `.tsx` can also be compiled as `.js`
".tsx",
".d.ts",
".js",
],
".ts": [".ts", ".d.ts", ".js"],
".jsx": [".tsx", ".d.ts", ".jsx"],
".tsx": [
".tsx",
".d.ts",
".jsx",
// `.tsx` can also be compiled as `.js`
".js",
],
".cjs": [".cts", ".d.cts", ".cjs"],
".cts": [".cts", ".d.cts", ".cjs"],
".mjs": [".mts", ".d.mts", ".mjs"],
".mts": [".mts", ".d.mts", ".mjs"],
}
mainFields
默认:
[
"types",
"typings",
// APF: https://angular.io/guide/angular-package-format
"fesm2020",
"fesm2015",
"esm2020",
"es2020",
"module",
"jsnext:main",
"main",
]
其他选项
你可以直接透传 unrs-resolver 的其他选项。
默认选项
你可以直接使用 require/import 来复用 defaultConditionNames、defaultExtensions、defaultExtensionAlias 和 defaultMainFields。
贡献指南
- 确保你的更改被测试导入所覆盖。
- 确保
yarn test运行无失败。 - 确保
yarn lint运行无冲突。 - 确保你的代码更改符合我们的 type-coverage 设置:
yarn type-coverage。
我们拥有 GitHub Actions,它将在你的 PR 上运行上述命令。
如果其中任何一个失败,在修复之前我们将无法合并你的 PR。
赞助商与支持者
赞助者
| 1stG | RxTS | UnTS |
|---|---|---|
支持者
| 1stG | RxTS | UnTS |
|---|---|---|
变更日志
每个版本的详细更改记录在 CHANGELOG.md 中。