I am using TSLint for static analysis of my TypeScript code.
One of the default rules does not allow the use of !=
to check for "not available" values. To explain what I mean by "not available", I will show an example:
/* This is supposed to return an object with a property token.
* I need to use auth.token in my code but I have to be sure that I have a
* value for the token. I do not control the code in that method. */
const auth = someService.getAuthentication();
Of course, I can try to guess, doing some tests, what the method returns when it does not return a token, but I do not like to write code based on something that seems to return a null or seems to return an undefined value.
I want to be sure that my code works in both cases.
Wouldn't it be better, in this case, to write:
if (auth.token != null) {...
instead of:
if (auth.token !== null && auth.token !== undefined) {...
?
I understand that a person that does not know JavaScript could miss the fact that !=
is changing the type of the operands, but someone who ignores that can probably still guess the meaning of that code.