Repository navigation
Disallow truthiness coercions on known-true/known-false values #9041
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptHelp WantedYou can do thisYou can do thisEffort: DifficultGood luck.Good luck.
on Jun 9, 2016 RyanCavanaugh commented
on Jun 9, 2016 MemberAuthorMore actionsRyanCavanaugh commented
on Feb 16, 2017 MemberAuthorMore actionsI tried implementing this since it's pretty easy now under
--strictNullChecks. It did not go well.A nonexhaustive list of problems:
- Interfacing with .d.ts files from non-strictnullcheck libraries is a disaster, since we assume all values from those libraries are truthy. This is perhaps a dealbreaker because this is going to be very common for a long time.
- Need to special-case
while (true) { - Since we don't include
| undefinedin the type of index access expressions, good checks likeif (map[key]) {become errors. Not at all clear how we'd fix this except by perhaps trafficking around a "possibly-falsy but assumed-truthy" flag on those expressions, which would be a mess. while(foo) { while(foo) {becomes an error becausefoois assumed truthy inside the loop, but it may be mutated
RyanCavanaugh commented
on May 8, 2017 MemberAuthorMore actionsIdea from SBS is to check the specific syntactic forms
if (x) {
and
if (x.y) {
to see if
xis non-nullable with call signatures. This should catch a few bugs while not having too many false positives.I've had times where I went
if (somePromise())when I meantif (await somePromise()).Would be nice to also have an option to make it strictly require boolean expressions (or intersections with boolean types) without having to use a lint tool. Since, to me, it is closer to something like
strictNullTypesbut isstrictBooleanTypesAnyhowStep You could try the following TSLint rule as long this check isn't supported by TypeScript.
https://palantir.github.io/tslint/rules/strict-boolean-expressions/
Opened #32802 that implements this de-scoped suggestion #9041 (comment)
Reacted by AnyhowStepI don't think this is the scope of strictNullChecks. This change injures the certainty and reliance of strictNullChecks. This change should be enabled by a new flag.
RyanCavanaugh commented
on Oct 25, 2019 MemberAuthorMore actionsThis has been done to the best extent we believe currently possible
You could try the following TSLint rule as long this check isn't supported by TypeScript.
https://palantir.github.io/tslint/rules/strict-boolean-expressions/
This and the ts-eslint rule for the same thing are not great at the moment.
They trip up over generics and such because the type relationship API isn't exposed.So, lint rules requiring type information tend to be hacky and not always work.
Inspired by #7746 but making a new issue for clarity
Bad code:
Change: Under
--strictNullChecks, it becomes an error to use an expression in a truthiness position unless the type of the expression is possibly-falsyPossibly-falsy types are:
anystring, string literal types,number, enum types, andbooleanA truthiness position is:
if,while, ordo/whilefor?,&&or||!