'Typescript Prevent Unbound / Uncalled Methods in Conditionals Without Strict NULL Checks
For Angular / Typescript projects, I'm looking for a linting rule or similar means to prevent accidentally putting an unbound method in a conditional.
I'm looking for a linting rule / configuration that will work for projects using ES Lint, and with Typescript configured with strictNullChecks: false.
I would like for the following code to produce an error either at lint or compile time:
export class Example {
constructor() {
if (this.isStillThe1900s) {
// the above should be an error because:
console.error("this will always be true because we forgot () on this.isStillThe1900s()");
}
}
private isStillThe1900s(): boolean {
return (new Date()).getFullYear() < 2000;
}
}
I know Typescript added this check back in 3.7, however according the documentation this check requires strict null checking to be enabled.
I've also tried
- @typescript-eslint/unbound-method TS/ES Lint rule
- no-constant-condition ES Lint rule
- @typescript-eslint/no-unnecessary-condition TS/ES Lint rule doesn't seem usable on projects without stricNullChecks (as hinted at in the documentation), even when setting the linting flag
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: true
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
