-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
Effort: DifficultGood luck.Good luck.Help WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
Inspired by #7746 but making a new issue for clarity
Bad code:
function func(): boolean { return Math.random() > 0.5; }
if (func) {
// oops, meant to write func()
}Change: Under --strictNullChecks, it becomes an error to use an expression in a truthiness position unless the type of the expression is possibly-falsy
Possibly-falsy types are:
any- The primitives:
string, string literal types,number, enum types, andboolean - Type parameters
- Union types containing possibly-falsy types
- An intersection type where any member is possibly falsy
A truthiness position is:
- The test expression of an
if,while, ordo/while - The middle expression of a
for - The first operand of
?,&&or|| - The operand of
!
bouzuya, jamiewinder, blakeembrey, ulrichb, normalser and 10 moreGoToLoop and falsandtruzpdDG4gta8XKpMCd, jwbay, zspitz, FabianLauer, ulrichb and 2 more
Metadata
Metadata
Assignees
Labels
Effort: DifficultGood luck.Good luck.Help WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript