strictBooleanExpressions
Reports non-boolean types in boolean contexts that may cause unexpected behavior.
✅ This rule is included in the ts logical presets.
JavaScript allows any value in boolean contexts like if statements, ternary expressions, and logical negation.
While primitives like strings and numbers have well-understood coercion rules, certain types in boolean contexts can indicate bugs or unclear code.
This rule reports:
- Using
anyin a boolean context, which defeats type safety - Nullable booleans (
boolean | nullorboolean | undefined), wherenull/undefinedsilently coerce tofalse - Non-nullable objects that are always truthy, making the condition redundant
Examples
Section titled “Examples”declare const value: any;if (value) {}
declare const flag: boolean | null;if (flag) {}
const config = { enabled: true };if (config) {}declare const flag: boolean;if (flag) {}
declare const text: string;if (text) {}
declare const count: number;if (count) {}
declare const item: object | null;if (item) {}
declare const flag: boolean | null;if (flag === true) {}if (flag ?? false) {}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase intentionally relies on truthy/falsy coercion for nullable booleans or uses any types extensively, you may want to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.