Skip to content

Never-initialized variable type is not narrowed to undefinedΒ #61496

@jtbandes

Description

@jtbandes

πŸ”Ž Search Terms

narrow uninitialized, uninitialized undefined

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about initialized/uninitialized

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250326#code/MYewdgzgLgBAZiEMC8MAUUDuIBcNoBOAlmAOYCUeAbiEQCYoB8MA3gFAwwBEArhAKb4oxYFC4BuDjCkB6GTH4QANiSgBaOkQgBDAEZL+asPwAe6lcZgABKAE8ADouDF76xRagySRdXX7AlbQJtKCJwCCkDWHB+PEISUhgAHxgeMD84En46SSkiOHQYlGRULBByVilOORgAPQB+KpgagDlEeykAXzZutgQQNC4ucnEgA

πŸ’» Code

const foo = (two: string): void => {
  "use strict";
  
  // eslint-disable-next-line @typescript-eslint/init-declarations
  let one: string | undefined;

  if (one === two) {
    // ^?  let one: string | undefined
    // Noop
  }
}

foo("");

πŸ™ Actual behavior

The type is not narrowed, which prevents typescript-eslint from flagging this comparison that always fails: typescript-eslint playground

πŸ™‚ Expected behavior

The one === two comparison should be flagged because it always returns false (there is no way one has any value other than undefined).

Additional information about the issue

Also filed a related issue with ESLint: eslint/eslint#19581

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions