Skip to content

Should prefer-const flag an uninitialized let x; that is never assigned? #19581

Closed
@jtbandes

Description

@jtbandes

Environment

Playground

What parser are you using?

@typescript-eslint/parser

What did you do?

{
  "extends": [
    "eslint:all",
    "plugin:@typescript-eslint/all"
  ],
  "rules": {
  }
}
const foo = (two: string): void => {
  "use strict";
  
  // eslint-disable-next-line @typescript-eslint/init-declarations
  let one: string | undefined;

  if (one === two) {
    // Noop
  }
}

foo("");

What did you expect to happen?

In this code snippet, let one; is read but never assigned. I would expect some rule to flag it – the most appropriate one seems like it would be prefer-const.

What actually happened?

prefer-const does not complain about this code. (Maybe because const one; is not allowed syntax?)

Link to Minimal Reproducible Example

Playground

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

Here is a typescript-eslint version of the playground.

If prefer-const is not the right rule to flag this error, then what is?

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThere is consensus among the team that this change meets the criteria for inclusionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

    Type

    No type

    Projects

    Status

    Complete

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions