You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think we should be explicit in our conditional statements instead of relying on truthiness. This should limit gotchas such as the one referenced above.
let a: ReadonlyArray<number> | null = null
let s: string | undefined = undefined
if (a) { }
if (a != null) { } //preferred
a = [0, 1, 2, 3]
if (a.length) { }
if (a.length > 0) { } //preferred
The intent behind this issue is that we should be explicit instead of relying on truthiness. I think booleans are exempt from this since they are the primitives all other conditional expressions evaluate.
I prefer using === and !== as opposed to == and !=
Me too! To be clear, I'm only talking about the case of null. I think differentiating between null and undefined in the context of an if is making a distinction without a difference. What we're trying to check is whether the value is something or nothing, regardless of whether the particular form of nothing is null or undefined.
Activity
j-f1 commentedon Dec 7, 2017
I’ll take a look at this.
1️⃣ ❓:
Should this throw?
joshaber commentedon Dec 7, 2017
👍
I'd lump these two together and suggest that we prefer:
It's rare that we care about the distinction between
nullandundefined, and I'd go further and say that caring is usually an anti-pattern.iAmWillShepherd commentedon Dec 7, 2017
The only reason I included
undefinedis that we are using optional types.Consider this:
In this case, we'd have to check for
undefined. We've been getting around this issue by relying on the truthiness ofundefined.iAmWillShepherd commentedon Dec 7, 2017
Should this throw?
@j-f1 no it should not.
The intent behind this issue is that we should be explicit instead of relying on truthiness. I think
booleansare exempt from this since they are the primitives all other conditional expressions evaluate.joshaber commentedon Dec 7, 2017
Hmm, I'm not sure I understand?
!= nullcoercesundefinedso that bothnull != nullandnull != undefinedare false. Or in other words, it generalizes across bothnullandundefined.iAmWillShepherd commentedon Dec 7, 2017
But
null !== undefinedreturntrue.I prefer using
===and!==as opposed to==and!=, but so long as we make sure we remember to use the loose equality operators, we should be fine.joshaber commentedon Dec 8, 2017
Me too! To be clear, I'm only talking about the case of
null. I think differentiating betweennullandundefinedin the context of anifis making a distinction without a difference. What we're trying to check is whether the value is something or nothing, regardless of whether the particular form of nothing isnullorundefined.http://adripofjavascript.com/blog/drips/equals-equals-null-in-javascript.html does a better job of explaining this than I think I am doing 😂
j-f1 commentedon Dec 8, 2017
In fact, that’s already configured as part of the
eqeqeqESLint rule:desktop/.eslintrc.yml
Lines 65 to 67 in f12ff8c
iAmWillShepherd commentedon Dec 8, 2017
@joshaber that made sense, also thanks for the article. So I'm down for using
==and!=only when checking againstnullorundefined.iAmWillShepherd commentedon Dec 8, 2017
@j-f1 were you intending to tackle this work?
j-f1 commentedon Dec 8, 2017
Yes 👍
iAmWillShepherd commentedon Dec 8, 2017
Could you open a PR to track since I can't assign you to the issue?
j-f1 commentedon Dec 8, 2017
Yup. ⬆️
j-f1 commentedon Jan 8, 2018
From my PR above, these appear to be the requirements:
&&,||, or!expressions and either:ifstatement, oriAmWillShepherd commentedon Jan 9, 2018
@j-f1 not sure I understand...
j-f1 commentedon Jan 9, 2018
@iAmWillShepherd Those are the requirements for a node to be reported by this rule.
iAmWillShepherd commentedon Jan 9, 2018
Got it.
This threw me off because I read it as a value is in a number, &&, ||, or ! expression.
j-f1 commentedon Jan 9, 2018
Looks like I forgot an
ofwhen typing that part. Edited above to correct.iAmWillShepherd commentedon May 25, 2018
Closing this out since the team has started being explicit with
nullandundefinedchecks.j-f1 commentedon Dec 15, 2018
Aha! https://palantir.github.io/tslint/rules/strict-boolean-expressions/