Skip to content

Apply uncalled function checks to boolean expressions (ts2774) #46051

Open
@soffes

Description

@soffes

Suggestion

Similar to #36048 (implemented in #36402), it would be great if uncalled function checks were applied to expressions like they are for if statements and ternaries.

export default class Range {
  lowerBound: number
  upperBound: number

  constructor(lowerBound: number, upperBound: number) {
    this.lowerBound = lowerBound
    this.upperBound = upperBound
  }

  isEmpty(): boolean {
    return this.lowerBound == this.upperBound
  }

  overlaps(other: Range): boolean {
    const isDisjoint = other.upperBound <= this.lowerBound
      || this.upperBound <= other.lowerBound
      || this.isEmpty || other.isEmpty // <-- Always true without an error
    return !isDisjoint
  }
}

Playground example

🔍 Search Terms

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

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

It would be great if using an uncalled function in a boolean expression was an error.

📃 Motivating Example

It would be great if this was true:

// Error
const bad = false || new Range().isEmpty

// Works!
const good = false || new Range().isEmpty()

💻 Use Cases

Recently spent awhile debugging some code that was always returning true before realizing I was checking the truthyness of an uncalled function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions