Skip to content

Uncalled function checks don't work when the variable is referenced in the block #43211

Closed

Description

Bug Report

🔎 Search Terms

uncalled function checks, conjunction, &&

🕗 Version & Regression Information

This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

💻 Code

// @strictNullChecks: true

declare function isFoo(): boolean;
declare function isBar(): boolean;

if (isFoo) {
    // uncalled function checks already work well here
}

if (isFoo && isBar()) {
    // uncalled function checks already work well here
}

if (isFoo && isFoo()) {
    // uncalled function checks should cover this case too
}

if (isFoo && isBar()) {
    isFoo;
    // uncalled function checks should cover this case too
}

🙁 Actual behavior

In the last two cases we didn't detect that the first occurrence of isFoo in the condition is uncalled.

🙂 Expected behavior

When isFoo cannot possibly be a falsy value (i.e. undefined) we should highlight this as an error even if isFoo occurs again later on. This is a follow-up to a conversation in #42835 (comment).
I would be happy to enable this check, then we could run this against some user tests and decide whether there are too many false positives 🙂

Activity

  1. RyanCavanaugh commented on Mar 12, 2021

    @RyanCavanaugh
    Member

    A reference should be sufficient here to not complain, since you might write something like

    if (isFoo) {
      invokeThisPlease(isFoo);
    }
  2. jonhue commented on Mar 12, 2021

    @jonhue
    ContributorAuthor

    Ryan Cavanaugh (@RyanCavanaugh) I'm probably misunderstanding your example, but if isFoo cannot possible be undefined, isn't the condition in the example you gave above redundant?

  3. RyanCavanaugh commented on Mar 12, 2021

    @RyanCavanaugh
    Member

    It's intentional that the rule is not ironclad. This is because there are some places in the language where TS thinks something is always not-undefined, but it's not necessarily the case.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions