Skip to content

Function truthy check doesn't apply if the function is in a boolean expression #37598

Closed

Description

TypeScript Version: 3.8.2, nightly

Search Terms:
Function truthiness, function truthy check

Code

const someBool = true;

function f() {}

if (f) {}

if (someBool && f) {}

if (!someBool || f) {}

Expected behavior:
All three if statements should throw a This condition will always return true since the function is always defined. Did you mean to call it instead? error.

Actual behavior:
Only the first statement throws the error.

Playground Link: https://www.typescriptlang.org/play/index.html?ts=Nightly#code/MYewdgzgLgBBIFsCmAhEIA2MC8MoCcBXJAbgFgAoSgM0LGCgEtwZqAKAShgG8BfSyo2ow21LnwEUhI+MjSYYAMkWtx-KlOFsAhLNTosAH0OqevIA

Related Issues:
#37149

Activity

  1. Andarist commented on Sep 13, 2022

    @Andarist
    Contributor

    The situation depicted by this particular playground has been actually fixed~ in 4.2.x: TS playground. It was most likely improved by #41599

    However, some cases still don't work, like this one: TS playground

    const thing = {
      isBeautiful() {
        return true;
      },
      isBiggerThanBreadBox() {
        return false;
      },
      isYellow() {
        return false;
      },
    };
    
    if (thing.isBeautiful || thing.isYellow) {
      console.log("Hi");
    }
    
    // Should error like above
    if (thing.isBeautiful() && (thing.isBiggerThanBreadBox || thing.isYellow)) {
      console.log("Hi");
    }
  2. amunger commented on Dec 9, 2022

    @amunger

    Is the fact that simply negating the check causes the warning to disappear covered by this issue?

    https://www.typescriptlang.org/play?#code/MYewdgzgLgBFAWBLMBzGBeGBvAsAKBhkQgCEBTAQwFcpEAzKgGwAoBKbfQwgJzKiu5g43KmQDcnGAF98UiXnz0YzBMhQA6YuWq0GjdrgIxQkEIzLrGIFMwBEACUS3W8mQrwB6DzADK8EEwAJjBk3Nwg3DCMiADWZDAUAEYgAG5kinTKAISqqJqklDT0TAaSJhBmFlY2Dk4uskA

    const thing = {
      isBeautiful() {
        return true;
      }
    };
    
    if (thing.isBeautiful) {
      console.log("Hi");
    }
    
    // Should error like above
    if (!thing.isBeautiful) {
      console.log("Hi");
    }
    
  3. Andarist commented on Dec 9, 2022

    @Andarist
    Contributor

    Aaron Munger (@amunger) this is definitely related but I'd say that perhaps it's worth creating a separate issue about this (if there is none about it already). I have a PR open that aims to fix this but it only deals with binary(like) expressions - I didn't touch unary expressions anyhow there.

  4. RyanCavanaugh commented on Dec 9, 2022

    @RyanCavanaugh
    Member

    The uncalled function check is intentionally very limited, since there are a lot of cases where you might be checking a function truthiness on purpose. For the case of if (!x) it's reasonable to not see an x in the body of the function, and we didn't want you to have to write !!!x to get around the checking error, so not including that case was intentional.

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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions