镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Uncalled Function Checks don't check negative condition #34815

Description

function foo() {
}

if (!foo) {
    let x = 10
}
else {
}

Expected: Error on !foo that the condition is always true because foo is defined.
Actual: No error.

Also, any work on this should include the following test:

function foo() {

}

if (!foo) {
}
else {
    foo()
}

Activity

  1. brandon-leapyear commented on Feb 17, 2021

    @brandon-leapyear

    ✨ This is an old work account. Please reference Brandon Chinn (@brandonchinn178) for all future communication ✨


    Very much agree with this. In our code, we just got bit by

    if (!nock.isActive) {
      nock.activate()
    }

    In general, I would usually not intentionally want !foo, and if I did want to do something like that, I would do foo === undefined. Even more generally, I would personally disallow coercion from function to boolean completely. Forcing devs to write foo === undefined would make it clear what their intention is, and would force the dev to type foo as Function | undefined, if that's truly what they want.

  2. AlexMarkUSDS commented on Jan 21, 2024

    @AlexMarkUSDS

    This bit me last week in one of the codebases I maintain. I also see that it's bitten a number of others:
    There's relevant info in:

    Daniel Rosenwasser (@DanielRosenwasser), would the TS team accept a contribution for this? I've optimistically opened a PR at #57114

  3. RyanCavanaugh commented on Jan 23, 2024

    @RyanCavanaugh
    Member

    There are many, many false positives turned up in that PR. Unless these were ~all good finds, I think this is appropriately left still in "awaiting more feedback" state

  4. AlexMarkUSDS commented on Jan 24, 2024

    @AlexMarkUSDS

    The tool for finding false positives is super neat. It makes sense to me to not merge this -- I followed ~20 links and I think this was the only legitimate bug I found (it should have an await). The rest were all just undefined checks on things that the typings say should always be defined.

    I'm happy to close out that PR (or have you close it). Whatever works for you.

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

    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