Eslint参考手册
规则 | Rules
valid-typeof
"extends": "eslint:recommended"
配置文件中的属性启用此规则。
对于绝大多数的使用情况下,结果typeof
操作是下列字符串常量之一:"undefined"
,"object"
,"boolean"
,"number"
,"string"
,"function"
和"symbol"
。将typeof
运算符的结果与其他字符串文字进行比较通常是打字错误。
规则细节
此规则强制将typeof
表达式与有效的字符串文字进行比较。
选项
该规则有一个对象选项:
-
"requireStringLiterals": true
要求typeof
表达式仅与字符串文字或其他typeof
表达式进行比较,并且不允许与任何其他值进行比较。此规则的代码不正确:/*eslint valid-typeof: "error"*/
typeof foo === "strnig"
typeof foo == "undefimed"
typeof bar != "nunber"
typeof bar !== "function"
此规则的正确代码示例:/*eslint valid-typeof: "error"*/
typeof foo === "string"
typeof bar == "undefined"
typeof foo === baz
typeof bar === typeof qux
具有以下选项的错误代码示例{ "requireStringLiterals": true }
:typeof foo === undefined
typeof bar == Object typeof baz === "strnig" typeof qux === "some invalid type" typeof baz === anotherVariable typeof foo == 5
正确的代码示例{ "requireStringLiterals": true }
option:typeof foo === "undefined"
typeof bar == "object" typeof baz === "string" typeof bar === typeof qux
何时不使用
你可能想关闭这条规则,如果你使用typeof
操作符在主机objects.Version此规则是在ESLint 0.5.0.Resources中引入的
- Rule source
- Documentation source
规则 | Rules相关

ESLint 是一个代码规范和错误检查工具,有以下几个特性。所有东西都是可以插拔的。你可以调用任意的 rule api 或者 formatter api 去打包或者定义 rule or formatter。任意的 rule 都是独立的。没有特定的 coding style,你可以自己配置。
主页 | https://eslint.org/ |
源码 | https://github.com/eslint/eslint |
发布版本 | 4.12.0 |