-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disallow truthiness coercions on known-true/known-false values #9041
Comments
cc @ahejlsberg |
I tried implementing this since it's pretty easy now under A nonexhaustive list of problems:
|
Idea from SBS is to check the specific syntactic forms if (x) { and if (x.y) { to see if |
I've had times where I went 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 |
@AnyhowStep 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) |
I 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. |
This has been done to the best extent we believe currently possible |
This and the ts-eslint rule for the same thing are not great at the moment. 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:
any
string
, string literal types,number
, enum types, andboolean
A truthiness position is:
if
,while
, ordo/while
for
?
,&&
or||
!
The text was updated successfully, but these errors were encountered: