Skip to content

Bug: [no-unused-vars] Conditional assignments are not counted as variable usages #7745

Closed
@sxxov

Description

@sxxov

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://typescript-eslint.io/play/#ts=5.0.4&fileType=.tsx&code=PQKhFgCgAIWhhATgUwIYBdnVdAzu1Ac0OUQEsA7Q6AE2QBtUBPaAMwHtFoBlANwczQosaOkSoKuMujLtJogBYZoKXMnS5srTF2T9ELGQGMA1gDphcS9AACyAB6oAtgAd6ya0bn48BYqWgAXmgjFAxkbj8SRAAKAEYABgSASgBuS09vdGgAb1oGZgAuWgToAF8g3yJo9Jg4L0lsvLpGJmKaRNKK4Pxq0lqRBp9mgrbaACYk8sre-0QB%2Bqzc-Nb2gGYp7qq5hZClkdXaABZNmaj%2BjLq8dQAVMidkdgBXdBiY5KCAPlzraD3G5YtIolaY9c7zaxlAA00BSC2AUAcLk42SGqLCmEifS4wRi7FYrDU6GKFCeTgARqQPoFvjlhNB3NkyJUEgMGeoVKhWAAJCQ0dzFABK6ieiAoNyYLmQAB50JLHqwVMgAI5PZD4ACCFHuGFkFAAYuIHt8AD7QJ4UOisSjIGi1ekodCiig-GB-aAkbJApjvV3u93iHl89zQAD8oeCKFV6vQWp1MjkhucyDe1Np9P9f2ZwVZGczgd5lpDwQtVptdrzfzKaSglaVTrF0HxhI5cDIAGp22yq1D6WVav2oEA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tidmUQAmtAG4BDaKgyRE0aB2iRwYAL4hlQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

/**
 * Create a staggering delay for Svelte 
 * transitions that resets after every tick.
 * 
 * @example
 * const stagger = createStagger(100);
 *
 * const { delay: d0 } = stagger;
 * const { delay: d100 } = stagger;
 * const { delay: d200 } = stagger;
 * const { delay: d300 } = stagger;
 * const { delay: d400 } = stagger;
 *
 * setTimeout(() => {
 *   const { delay: d0 } = stagger;
 * }, 0);
 */
export const createStagger = (offset: number) => {
  let i = 0;
  let rafHandle: ReturnType<typeof requestAnimationFrame> | undefined;

  return {
    get delay() {
      rafHandle ??= requestAnimationFrame(() => {
        i = 0;
        rafHandle = undefined;
      });

      return offset * i++;
    },
  };
};

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unused-vars": "error",
  },
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

The lint error is not raised for rafHandle

Actual Result

The lint error is raised for rafHandle

Additional Info

It seems like TypeScript follows the erraneous behaviour with the error 6133: 'rafHandle' is declared but its value is never read. 20:7 - 20:16. HOWEVER, it is only raised on the playground, not on the latest stable version of VSCode:

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions