UNPKG

9.22 kBMarkdownView Raw
1# eslint-import-resolver-typescript
2
3[![GitHub Actions](https://github.com/import-js/eslint-import-resolver-typescript/workflows/CI/badge.svg)](https://github.com/import-js/eslint-import-resolver-typescript/actions/workflows/ci.yml)
4[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fimport-js%2Feslint-import-resolver-typescript%2Fmaster%2Fpackage.json)](https://github.com/plantain-00/type-coverage)
5[![npm](https://img.shields.io/npm/v/eslint-import-resolver-typescript.svg)](https://www.npmjs.com/package/eslint-import-resolver-typescript)
6[![GitHub Release](https://img.shields.io/github/release/import-js/eslint-import-resolver-typescript)](https://github.com/import-js/eslint-import-resolver-typescript/releases)
7
8[![Conventional Commits](https://img.shields.io/badge/conventional%20commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
9[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com)
10[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
11[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
12[![changesets](https://img.shields.io/badge/maintained%20with-changesets-176de3.svg)](https://github.com/changesets/changesets)
13
14This plugin adds [`TypeScript`][] support to [`eslint-plugin-import`][] (Or maybe you want to try [`eslint-plugin-i`][] for faster speed)
15
16This means you can:
17
18- `import`/`require` files with extension `.cts`/`.mts`/`.ts`/`.tsx`/`.d.cts`/`.d.mts`/`.d.ts`
19- Use [`paths`](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) defined in `tsconfig.json`
20- Prefer resolving `@types/*` definitions over plain `.js`/`.jsx`
21- Multiple tsconfigs support just like normal
22- `imports/exports` fields support in `package.json`
23
24## TOC <!-- omit in toc -->
25
26- [Notice](#notice)
27- [Installation](#installation)
28- [Configuration](#configuration)
29- [Options from `enhanced-resolve`](#options-from-enhanced-resolve)
30 - [`conditionNames`](#conditionnames)
31 - [`extensions`](#extensions)
32 - [`extensionAlias`](#extensionalias)
33 - [`mainFields`](#mainfields)
34 - [Other options](#other-options)
35 - [Default options](#default-options)
36- [Contributing](#contributing)
37- [Sponsors](#sponsors)
38- [Backers](#backers)
39- [Changelog](#changelog)
40- [License](#license)
41
42## Notice
43
44After 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.
45
46If 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](https://github.com/import-js/eslint-import-resolver-typescript/issues/31#issuecomment-539751607) on our sides, take [import-js/eslint-plugin-import#1525](https://github.com/import-js/eslint-plugin-import/issues/1525) as reference or post a new issue to [`eslint-plugin-import`][] instead.
47
48## Installation
49
50```sh
51# npm
52npm i -D eslint-plugin-import eslint-import-resolver-typescript
53
54# pnpm
55pnpm i -D eslint-plugin-import eslint-import-resolver-typescript
56
57# yarn
58yarn add -D eslint-plugin-import eslint-import-resolver-typescript
59```
60
61## Configuration
62
63Add the following to your `.eslintrc` config:
64
65```jsonc
66{
67 "plugins": ["import"],
68 "rules": {
69 // turn on errors for missing imports
70 "import/no-unresolved": "error"
71 },
72 "settings": {
73 "import/parsers": {
74 "@typescript-eslint/parser": [".ts", ".tsx"]
75 },
76 "import/resolver": {
77 "typescript": {
78 "alwaysTryTypes": true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
79
80 // Choose from one of the "project" configs below or omit to use <root>/tsconfig.json by default
81
82 // use <root>/path/to/folder/tsconfig.json
83 "project": "path/to/folder",
84
85 // Multiple tsconfigs (Useful for monorepos)
86
87 // use a glob pattern
88 "project": "packages/*/tsconfig.json",
89
90 // use an array
91 "project": [
92 "packages/module-a/tsconfig.json",
93 "packages/module-b/tsconfig.json"
94 ],
95
96 // use an array of glob patterns
97 "project": [
98 "packages/*/tsconfig.json",
99 "other-packages/*/tsconfig.json"
100 ]
101 }
102 }
103 }
104}
105```
106
107## Options from [`enhanced-resolve`][]
108
109### `conditionNames`
110
111Default:
112
113```jsonc
114[
115 "types",
116 "import",
117
118 // APF: https://angular.io/guide/angular-package-format
119 "esm2020",
120 "es2020",
121 "es2015",
122
123 "require",
124 "node",
125 "node-addons",
126 "browser",
127 "default"
128]
129```
130
131### `extensions`
132
133Default:
134
135```jsonc
136[
137 // `.mts`, `.cts`, `.d.mts`, `.d.cts`, `.mjs`, `.cjs` are not included because `.cjs` and `.mjs` must be used explicitly
138 ".ts",
139 ".tsx",
140 ".d.ts",
141 ".js",
142 ".jsx",
143 ".json",
144 ".node"
145]
146```
147
148### `extensionAlias`
149
150Default:
151
152```jsonc
153{
154 ".js": [
155 ".ts",
156 // `.tsx` can also be compiled as `.js`
157 ".tsx",
158 ".d.ts",
159 ".js"
160 ],
161 ".jsx": [".tsx", ".d.ts", ".jsx"],
162 ".cjs": [".cts", ".d.cts", ".cjs"],
163 ".mjs": [".mts", ".d.mts", ".mjs"]
164}
165```
166
167### `mainFields`
168
169Default:
170
171```jsonc
172[
173 "types",
174 "typings",
175
176 // APF: https://angular.io/guide/angular-package-format
177 "fesm2020",
178 "fesm2015",
179 "esm2020",
180 "es2020",
181
182 "module",
183 "jsnext:main",
184
185 "main"
186]
187```
188
189### Other options
190
191You can pass through other options of [`enhanced-resolve`][] directly
192
193### Default options
194
195You can reuse `defaultConditionNames`, `defaultExtensions`, `defaultExtensionAlias` and `defaultMainFields` by `require/import` them directly
196
197## Contributing
198
199- Make sure your change is covered by a test import.
200- Make sure that `yarn test` passes without a failure.
201- Make sure that `yarn lint` passes without conflicts.
202- Make sure your code changes match our [type-coverage](https://github.com/plantain-00/type-coverage) settings: `yarn type-coverage`.
203
204We have [GitHub Actions](https://github.com/import-js/eslint-import-resolver-typescript/actions) which will run the above commands on your PRs.
205
206If either fails, we won't be able to merge your PR until it's fixed.
207
208## Sponsors
209
210| 1stG | RxTS | UnTS |
211| ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
212| [![1stG Open Collective backers and sponsors](https://opencollective.com/1stG/organizations.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective backers and sponsors](https://opencollective.com/rxts/organizations.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective backers and sponsors](https://opencollective.com/unts/organizations.svg)](https://opencollective.com/unts) |
213
214## Backers
215
216| 1stG | RxTS | UnTS |
217| -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
218| [![1stG Open Collective backers and sponsors](https://opencollective.com/1stG/individuals.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective backers and sponsors](https://opencollective.com/rxts/individuals.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective backers and sponsors](https://opencollective.com/unts/individuals.svg)](https://opencollective.com/unts) |
219
220## Changelog
221
222Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.md).
223
224## License
225
226[ISC][]
227
228[`eslint-plugin-i`]: https://github.com/un-es/eslint-plugin-i
229[`eslint-plugin-import`]: https://github.com/import-js/eslint-plugin-import
230[`enhanced-resolve`]: https://github.com/webpack/enhanced-resolve
231[`typescript`]: https://www.typescriptlang.org
232[isc]: https://opensource.org/licenses/ISC