Skip to content

Don't allow applying the ! operator to uncalled functions #61607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
DonaldDuck313 opened this issue Apr 22, 2025 · 3 comments
Closed
6 tasks done

Don't allow applying the ! operator to uncalled functions #61607

DonaldDuck313 opened this issue Apr 22, 2025 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@DonaldDuck313
Copy link

πŸ” Search Terms

This condition will always return true since this function is always defined. Did you mean to call it instead? TS2774

βœ… Viability Checklist

⭐ Suggestion

If someFunction is a function, if(someFunction){...} gives error TS2774 because foo is always truthy and was probably intended to be called. However, !someFunction is allowed, always returning false. I suggest that !someFunction should be an error in the same way as if(someFunction){...}.

Depending on how strict you are about backwards compatibility, this could be an option that's disabled by default, because technically it would be a breaking change (since !someFunction currently compiles but wouldn't compile with my suggestion). However, in practice, I can't think of any code that works as intended and that would be broken by this suggestion, since I don't see why anyone intentionally writing !someFunction wouldn't just use the literal false instead.

πŸ“ƒ Motivating Example

declare function someFunction(): boolean;

if(someFunction){    //Oops, should be someFunction(), TypeScript helpfully warns me about this
    //Do something
}

if(!someFunction){    //No error, the if block will never be executed which can lead to bugs at runtime
    //Do something else
}

πŸ’» Use Cases

  1. What do you want to use this for?

    To get an error when it's obvious that I forgot to call a function (the same reason TS2774 already exists).

  2. What shortcomings exist with current approaches?

    I have to run the code, see that something is wrong, and debug it. In the best of cases this is unnecessarily time consuming, in the worst of cases I won't notice that anything is wrong and the bug will go into production code.

  3. What workarounds are you using in the meantime?

    Runtime debugging, I guess I don't have much choice.

@MartinJohns
Copy link
Contributor

Duplicate of #45667.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 22, 2025
@kirkwaiblinger
Copy link

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants