Skip to content

TypeScript erroneously reports unused variable 'toDate' #61674

@wizardnet972

Description

@wizardnet972

Description:

I'm encountering an issue where TypeScript reports that the variable toDate is declared but its value is never read (Error Code 6133). However, the variable toDate is actually used, so this seems to be a false positive in TypeScript's analysis.

Steps to Reproduce:

  1. Use the following TypeScript code:

    function foo() {
      let toDate = moment().startOf('day');
      if (true) {
        toDate = moment(new Date(this.datePicker.fromDate)).endOf('day');
      }
    }
  2. The TypeScript compiler will raise the error:

    'toDate' is declared but its value is never read. (6133)
    

Expected Behavior:

TypeScript should recognize that toDate is assigned a new value within the if block, and it should not report it as "unused."

Actual Behavior:

TypeScript reports that toDate is declared but never used, even though the variable is reassigned in the if block.

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABMOcAUBKRBvAUIxAGwFMpEo4ARAQymMQF5EBbOZ4sKTAOgGcpqAJygB5YGgDkAE2oBPCRgDc+RDGCI0UQSGJY8BAhRp1GLNhy5hiAd0THimgBYxe3GXQAKMCAGtig7mBBNnsMDG4OKTFJGXklFQBfXASgA

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions