ESLint Resolver for TypeScript Imports

4.4.4 · active · verified Sun Apr 19

This package provides a TypeScript-aware resolver for `eslint-plugin-import` and its faster alternative, `eslint-plugin-import-x`. It enables ESLint to correctly resolve import paths in TypeScript projects, including support for `tsconfig.json`'s `paths` mapping, resolving `.cts`, `.mts`, `.ts`, `.tsx`, `.d.cts`, `.d.mts`, and `.d.ts` extensions. A key feature is its preference for `@types/*` definitions or local `.d.ts` files over plain JavaScript files in `node_modules` for versions 2.0.0 and above. The package is actively maintained, with the current stable version being 4.4.4, receiving frequent patch updates and minor feature releases. It supports multiple `tsconfig.json` files and `package.json` `imports`/`exports` fields, ensuring robust module resolution within complex TypeScript setups.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up `eslint-import-resolver-typescript` within ESLint's modern flat configuration to resolve TypeScript module paths, including `tsconfig.json` `paths` mappings, and proper handling of various module extensions. It also illustrates how to configure specific parsers for different file types.

/* eslint.config.js */
import eslintPluginImport from 'eslint-plugin-import';
import tsResolver from 'eslint-import-resolver-typescript';

export default [
  {
    files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'],
    // Enable import plugin and set up resolver for TypeScript files
    plugins: {
      import: eslintPluginImport,
    },
    settings: {
      'import/resolver': {
        typescript: tsResolver, // Use the imported resolver module
        node: {
          extensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.mts', '.cjs', '.cts', '.d.ts'],
        },
      },
      'import/parsers': {
        '@typescript-eslint/parser': ['.ts', '.tsx', '.mts', '.cts'],
      },
    },
    rules: {
      'import/no-unresolved': 'error', // Ensure module paths resolve correctly
      'import/named': 'error',
      'import/default': 'error',
      'import/namespace': 'error',
      'import/order': ['error', { 'newlines-between': 'always' }],
      // Add other import rules as needed
    },
  },
  // Add configuration for JavaScript files if necessary
  {
    files: ['**/*.js', '**/*.jsx', '**/*.mjs', '**/*.cjs'],
    plugins: {
      import: eslintPluginImport,
    },
    settings: {
      'import/resolver': {
        node: {
          extensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.mts', '.cjs', '.cts', '.d.ts'],
        },
      },
    },
    rules: {
      'import/no-unresolved': 'error',
    },
  },
];

/* tsconfig.json (example, typically in project root) */
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "lib": ["ES2020", "DOM"],
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "Bundler",
    "baseUrl": ".", // Required for path mapping
    "paths": {
      "@components/*": ["./src/components/*"],
      "@utils/*": ["./src/utils/*"]
    }
  },
  "include": ["src/**/*.ts", "src/**/*.tsx"]
}

// Example usage in src/app.ts (assuming a component 'Button' and util 'formatDate')
// import { Button } from '@components/button';
// import { formatDate } from '@utils/date';

view raw JSON →