typescript-eslint: Bug: [no-unused-var] issue with Angular app

Before You File a Bug Report Please Confirm You Have Done The Following…

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://github.com/typescript-eslint/typescript-eslint/blob/aba6e37c4050a2031ee4a5340cdbabe25dd5adc4/packages/eslint-plugin/src/util/collectUnusedVariables.ts#L151

Repro Code

import { InjectionToken as InjectionToken } from '@angular/core';

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unused-vars": [
        "error",
        {
            "args": "all",
            "argsIgnorePattern": "^_",
            "caughtErrors": "all",
            "caughtErrorsIgnorePattern": "^_",
            "destructuredArrayIgnorePattern": "^_",
            "varsIgnorePattern": "^_",
            "ignoreRestSiblings": true
        }
    ],
  },
};

tsconfig

{
    "compilerOptions": {
        "alwaysStrict": true,
        "baseUrl": ".",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "module": "esnext",
        "moduleResolution": "node",
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "removeComments": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "sourceMap": true,
        "strictFunctionTypes": true,
        "strictNullChecks": true,
        "strictPropertyInitialization": true,
        "target": "esnext",
        "version": true,
        "types": ["node", "webpack-env"],
        "lib": ["esnext", "dom"]
    }
}

Expected Result

no linting errors

Actual Result

I have upgraded to the latest eslint, and i have an angular application for which the first line is

import { InjectionToken } from '@angular/core';

I get the following error

Oops! Something went wrong! :(

ESLint: 9.8.0

TypeError: Cannot use 'in' operator to search for 'type' in undefined
Occurred while linting /src/client/app.config.ts:1
Rule: "@typescript-eslint/no-unused-vars"
    at UnusedVarsVisitor.markVariableAsUsed (/node_modules/@typescript-eslint/eslint-plugin/dist/util/collectUnusedVariables.js:92:22)
    at UnusedVarsVisitor.TSMappedType (/node_modules/@typescript-eslint/eslint-plugin/dist/util/collectUnusedVariables.js:235:14)
    at UnusedVarsVisitor.visit (/node_modules/@typescript-eslint/scope-manager/dist/referencer/VisitorBase.js:59:21)
    at UnusedVarsVisitor.visitChildren (/node_modules/@typescript-eslint/scope-manager/dist/referencer/VisitorBase.js:46:22)
    at UnusedVarsVisitor.visit (/node_modules/@typescript-eslint/scope-manager/dist/referencer/VisitorBase.js:64:14)
    at UnusedVarsVisitor.visitChildren (/node_modules/@typescript-eslint/scope-manager/dist/referencer/VisitorBase.js:41:30)
    at UnusedVarsVisitor.visit (/node_modules/@typescript-eslint/scope-manager/dist/referencer/VisitorBase.js:64:14)
    at UnusedVarsVisitor.collectUnusedVariables (/node_modules/@typescript-eslint/eslint-plugin/dist/util/collectUnusedVariables.js:30:17)
    at collectVariables (/node_modules/@typescript-eslint/eslint-plugin/dist/util/collectUnusedVariables.js:603:30)
    at collectUnusedVariables (/node_modules/@typescript-eslint/eslint-plugin/dist/rules/no-unused-vars.js:269:65)

Additional Info

for safety it’s better also to check on undefined

suggestion

 if (
     variableOrIdentifierOrName &&
      typeof variableOrIdentifierOrName !== 'string' &&
      !('type' in variableOrIdentifierOrName)
    ) {
      variableOrIdentifierOrName.eslintUsed = true;
      return;
    }

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (7 by maintainers)

Most upvoted comments

@moroine

$ yarn list @typescript-eslint/parser
├─ @typescript-eslint/parser@8.1.0
└─ eslint-config-next@13.5.6
   └─ @typescript-eslint/parser@6.21.0
Done in 0.11s.

ui/.eslintrc.json has "extends": ["next"]. i.e. your using v6 of our parser with v8 of our lint rules. it’s expected that this state is invalid and crashes.

The latest version of eslint-config-next (14.2.5) depends on a slightly more modern version of our packages (v7.2.0) – but it’s still miles behind the latest version (8.1.0) – it’s even miles behind the latest v7 version (7.18.0). For reference we released 7.2.0 FIVE months ago


Given we don’t have a valid repro after a week - I’m going to close this as unreproducible. If someone manages to create a repro using the latest version of our packages – happy to reopen and investigate!

ok, but that link is for version 9.3.0 while the issue is with 9.8.0 and for me it happens with compilation ./node_modules/.bin/eslint while i had no error in vscode , so i guess you might need a custom repo for this

You can close this now, after some debugging myself, there error message telling me line 1 and thinking it was an angular support was misleading. At the end i fixed by changing a few interfaces the problem was that eslint was requiring a “key” for TSMappedType, so i changed from

const enum ERoutes {
  home = 'home'
}

type IRoutes = {
  [route in ERoutes]: {
    component?: string;
  };
};
  }

to

type IRoutes = {
  [key: string]: {
      component?: string;
  };
};

which is strange, because that code i replaced previously never had any issue with lint and displays as ok on the playground, yet the compilation doesn’t like it

If it requires a specific version to reproduce, you can always make a cloneable repository.

Repro Code

A few scattered config files + a snippet of code isn’t a full reproduction. We need to be able to reproduce your issue locally, including seeing the versions of all relevant packages. Please post a way for us to do that, such as a link to your project or a minimum sample app reproduction.

In case it’s useful: https://antfu.me/posts/why-reproductions-are-required