Defect: CompareTypeofToString (Category General 5)
Diagnose: The string result of the “typeof” operator is compared with something that is not a string, leading to a contradiction.
You can find this one in npm/cli. As usual, just got to deepcode.ai, open the repo and follow along. This one is an easy one...
As you can see DeepCode highlights the following code:
typeof schema.minimum !== undefined and explains
typeof is compared to undefined. This comparison is invalid, because typeof always returns strings.
It seems to be a very common mistake - especially for developers used to Java (see
true as the typeof operator results in a string (even if the string is equal to
' undefined') - see MDN on typeof.
null is also a separate type (while calling
typeof null results in
object which is a bug in the language).
===) and an abstract equal (
==) (see MDN on Comparison Operators ). There are corresponding unequal operators (
undefined is strictly equal to itself and abstract equal to itself and
null. Comparing it to any other type (in our case a string) will result in
false. So, the above code will always result in
true. DeepCode and m, we guess the developer did not intend that.
But how should you do it now?
Well, using the above construct only in a correct fashion is possible and popular:
typeof myVar == typeof unknown because I think it is elegant and explains itself (also the IDE prevents misspelling).
myVar === undefined has some caveats. First, if the variable was not declared, this will throw an exception. Secondly, it is possible to redefine
void 0 (so
myvar === void 0) which generates an
undefined. You find this in some libraries.
Finally, let us have a look under the hood. For DeepCode, this is a type analysis. DeepCode knows that a call to
typeof results in a
string. It has a rule arguing that a comparison between
undefined and a different type such as a string is very very likely not what was intended. DeepCode has also seen this construct in other repos before and noticed that it has often been corrected there. It lists three occasions (the before and the after) in the selection called Example Fixes.
(Thanks to Nils Weller as proof reader :-) )