11

I've been setting up the paths option in my tsconfig.json file. So far everything works fine. I can run my tests, and I can execute the program as usual. My only problem is, that ESLint does not find the modules that are accessed with one of the paths defined in tsconfig.json.

Here are all of the files that are related to the problem:

tsconfig.json:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "allowJs": true,
        "sourceMap": true,
        "outDir": "./dist",
        "rootDir": "./",
        "strict": true,
        "esModuleInterop": true,
        "declaration": true,
        "resolveJsonModule": true,
        "baseUrl": ".",
        "paths": {
            "@/*": ["./src/*"]
        }
    },
    "include": ["src/**/*", "test/**/*", "index.ts"]
}

tsconfig.eslint.json:

{
    "extends": "./tsconfig.json",
    "include": ["src/**/*", "test/**/*", "index.ts", ".eslintrc.js"]
}

.eslintrc.js:

{
    root: true,
    env: {
        browser: true,
        es6: true,
        node: true
    },
    extends: ['plugin:@typescript-eslint/recommended', 'plugin:@typescript-eslint/recommended-requiring-type-checking'],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        project: ['tsconfig.eslint.json'],
        sourceType: 'module'
    },
    settings: {
        'import/resolver': {
            typescript: {}
        }
    },
    plugins: ['@typescript-eslint', 'import', 'prefer-arrow'],
    rules: {...}
}

Am am using the package eslint-import-resolver-typescript.

Now, if I try to import the file './src/test.ts' into './index.ts' with the path '@/test', then ESLint will not be able to resolve that import (although TypeScript resolves it just fine).

I've mostly copied my current solution from here, because I thought the person who asked that problem had the same problem that I had, but my setup still does not work.

I am in an NodeJS environment by the way.

EDIT: I've also tried using the package eslint-import-resolver-alias. This only helped partially. I could get rid of the 'import/no-unresolved' errors, but whenever I call an imported function, I get '@typescript-eslint/no-unsafe-call' because apparently, ESLint does not find the types for the imported files and thusly gives everything the type any.

3 Answers 3

8

Could you try adding the tsconfigRootDir to your .eslintrc.js? It has worked for me.

parserOptions: {
    tsconfigRootDir: __dirname,
}
8

For those using Angular and.eslintrc.json, I use this (notice the 2 stars in "Project"

        "parserOptions": {
            "project": ["**/tsconfig.json"],
            "createDefaultProgram": true
        },
1
  • 1
    This is finally what worked for me. Putting in a tsconfigRootDir entry worked for me in VsCode but failed when I ran "ng lint". Taking out tsconfigRootDir worked for me when I ran "ng lint" but failed in VsCode (errors in every .ts file). But adding the "**/" made it work in both places. May 2, 2022 at 16:17
1

you need to add eslint plugin https://github.com/benmosher/eslint-plugin-import#typescript

there under the extends you can specify option for typescript

extends:
  - plugin:import/typescript

it should do the trick

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.