首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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中引入的

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com