deno.com

有效的 typeof

注意: 此规则是 recommended 规则集的一部分。
deno.json 中启用完整规则集
{
  "lint": {
    "tags": ["recommended"]
  }
}
使用 Deno CLI 启用完整规则集
deno lint --tags=recommended

限制 typeof 运算符的使用,使其仅限于特定的字符串字面量。

当与值一起使用时,typeof 运算符返回以下字符串之一

  • "undefined"
  • "object"
  • "boolean"
  • "number"
  • "string"
  • "function"
  • "symbol"
  • "bigint"

此规则禁止在使用 typeof 运算符时与这些字符串字面量之外的任何内容进行比较,因为这可能表示字符串中的类型错误。该规则还禁止将 typeof 操作的结果与任何非字符串字面量值进行比较,例如 undefined,这可能表示无意中使用关键字而不是字符串。这包括与字符串变量进行比较,即使它们包含上述值之一,因为这无法保证。此规则的一个例外是比较两个 typeof 操作的结果,因为它们都保证返回上述字符串之一。

无效

// typo
typeof foo === "strnig";
typeof foo == "undefimed";
typeof bar != "nunber";
typeof bar !== "fucntion";

// compare with non-string literals
typeof foo === undefined;
typeof bar == Object;
typeof baz === anotherVariable;
typeof foo == 5;

有效

typeof foo === "undefined";
typeof bar == "object";
typeof baz === "string";
typeof bar === typeof qux;

您找到您需要的内容了吗?

隐私政策