|
| 1 | +<!--- |
| 2 | +{ |
| 3 | + "titles": [ |
| 4 | + "Invalid typeof value '{a}'", |
| 5 | + "W122" |
| 6 | + ], |
| 7 | + "slugs": [ |
| 8 | + "invalid-typeof-value-a", |
| 9 | + "w122" |
| 10 | + ], |
| 11 | + "linters": [ |
| 12 | + "jshint" |
| 13 | + ], |
| 14 | + "author": "jallardice" |
| 15 | +} |
| 16 | +--> |
| 17 | + |
| 18 | +### When do I get this error? |
| 19 | + |
| 20 | +The "Invalid typeof value '{a}'" error is thrown when JSHint encounters **a |
| 21 | +comparison with a `typeof` expression on one side and an invalid string literal |
| 22 | +on the other**. In the following example we have a function that will return |
| 23 | +`true` if the argument is of type `"bool"`: |
| 24 | + |
| 25 | +<!--- |
| 26 | +{ |
| 27 | + "linter": "jshint" |
| 28 | +} |
| 29 | +--> |
| 30 | +```javascript |
| 31 | +function demo(a) { |
| 32 | + return typeof a === "bool"; |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +This functionality was introduced in JSHint 2.3.0. Prior versions do not raise |
| 37 | +any error in this situation. Neither JSLint nor ESLint raises any error in this |
| 38 | +situation. |
| 39 | + |
| 40 | +### Why do I get this error? |
| 41 | + |
| 42 | +This error is raised to highlight **code that is unlikely to work as you |
| 43 | +expect**. The `typeof` operator returns one of a few possible values that are |
| 44 | +given by the ECMAScript specification (although in some situations in certain |
| 45 | +browsers it will return one of two extra possibilities). The possible values are |
| 46 | +as follows ([ES5 §11.4.3]): |
| 47 | + |
| 48 | + - `"undefined"` - when the operand has the `undefined` value |
| 49 | + |
| 50 | + - `"boolean"` - when the operand has a literal boolean value |
| 51 | + |
| 52 | + - `"string"` - when the operand has a literal string value |
| 53 | + |
| 54 | + - `"number"` - when the operand has a literal numeric value |
| 55 | + |
| 56 | + - `"function"` - when the operand is an object and implements the internal |
| 57 | + [[Call]] property |
| 58 | + |
| 59 | + - `"object"` - when the operand has the `null` value or is an object that does |
| 60 | + not implement the internal [[Call]] property |
| 61 | + |
| 62 | +In certain situations the operator could also return `"xml"` or `"unknown"` but |
| 63 | +you're unlikely to come across these. JSHint requires that any comparison with a |
| 64 | +`typeof` expression uses one of these strings. If a different string is used the |
| 65 | +condition will always evaluate to false since it is not possible for the |
| 66 | +`typeof` operator to ever return the value you're using. To solve this issue |
| 67 | +simply use a valid value: |
| 68 | + |
| 69 | +<!--- |
| 70 | +{ |
| 71 | + "linter": "jshint" |
| 72 | +} |
| 73 | +--> |
| 74 | +```javascript |
| 75 | +function demo(a) { |
| 76 | + return typeof a === "boolean"; |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +In JSHint 1.0.0 and above you have the ability to ignore any warning with a |
| 81 | +[special option syntax][jshintopts]. The identifier of this warning is **W122**. |
| 82 | +This means you can tell JSHint to not issue this warning with the `/*jshint |
| 83 | +-W122 */` directive. You can also set the `sub` option to `true`. |
| 84 | + |
| 85 | +[es5-11.4.3]: https://es5.github.io/#11.4.3 |
| 86 | +[jshintopts]: http://jshint.com/docs/#options |
0 commit comments