What is eslint-import-resolver-typescript?
The eslint-import-resolver-typescript package is a plugin for ESLint that helps in resolving import paths for TypeScript files. It allows ESLint to understand TypeScript syntax and resolve paths according to the TypeScript compiler options. This is particularly useful when using features like path aliases and custom module directories in TypeScript projects.
What are eslint-import-resolver-typescript's main functionalities?
Resolving TypeScript Path Aliases
This feature allows the resolver to use TypeScript's path aliases defined in tsconfig.json to resolve modules, which helps ESLint to understand custom path mappings.
module.exports = {
settings: {
'import/resolver': {
typescript: {
// use <root>/tsconfig.json
project: '.'
}
}
}
};
Support for Multiple tsconfig Files
The resolver can be configured to use multiple tsconfig.json files, which is useful in monorepos or projects with multiple TypeScript configurations.
module.exports = {
settings: {
'import/resolver': {
typescript: {
// a list of tsconfig.json to use
project: ['tsconfig.json', 'packages/*/tsconfig.json']
}
}
}
};
Ignoring TypeScript Errors
This feature allows the resolver to ignore TypeScript errors when resolving paths. It is useful when you want ESLint to focus on import/export issues without being blocked by TypeScript compilation errors.
module.exports = {
settings: {
'import/resolver': {
typescript: {
// ignore TypeScript errors
alwaysTryTypes: true
}
}
}
};
Other packages similar to eslint-import-resolver-typescript
eslint-import-resolver-node
This package provides a resolver for the ESLint plugin import that resolves node modules and commonjs modules. It does not have TypeScript-specific features but is suitable for projects using plain JavaScript.
eslint-import-resolver-webpack
This resolver is designed for projects using Webpack and allows ESLint to resolve module paths based on the Webpack configuration. It supports features like Webpack aliases and modules, which can be similar to TypeScript path aliases but is tailored for Webpack-specific configurations.
eslint-import-resolver-babel-module
This package integrates with Babel and allows ESLint to resolve modules using Babel's module resolver plugin. It is similar to eslint-import-resolver-typescript in that it helps resolve non-standard module paths, but it is focused on Babel rather than TypeScript.
eslint-import-resolver-typescript
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
.
Installation
npm install --save-dev eslint-plugin-import typescript-eslint-parser eslint-import-resolver-typescript
Configuration
Add the following to your .eslintrc
config:
{
"settings": {
"parser": "typescript-eslint-parser",
"plugins": [
"import"
],
"rules": {
// turn on errors for missing imports
"import/no-unresolved": "error"
},
"import/resolver": {
// use <root>/tsconfig.json
"typescript": {},
// use <root>/path/to/folder/tsconfig.json
"typescript": {
"directory": "./path/to/folder"
}
}
}
}
Contributing
- Make sure your change is covered by a test import.
- Make sure that
npm test
passes without a failure.