Skip to content
← Back to rules

eslint/valid-typeof 正确性

This rule is turned on by default.
🛠️ An auto-fix is available for this rule for some violations.

作用

强制将 typeof 表达式与有效字符串进行比较。

为什么这不好?

对于绝大多数用例,typeof 运算符的结果是以下字符串字面量之一:"undefined""object""boolean""number""string""function""symbol""bigint"。将 typeof 运算符的结果与其他字符串字面量进行比较通常是一个类型错误。

示例

以下是此规则的错误代码示例:

js
typeof foo === "strnig";
typeof foo == "undefimed";
typeof bar != "nunber"; // spellchecker:disable-line
typeof bar !== "fucntion"; // spellchecker:disable-line

以下是此规则的正确代码示例:

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

配置

此规则接受一个包含以下属性的配置对象:

requireStringLiterals

type: boolean

default: false

requireStringLiterals 选项设置为 true 时,只允许将 typeof 表达式与字符串字面量或其他 typeof 表达式进行比较,并禁止与任何其他值进行比较。默认值为 false

requireStringLiterals 设置为 true 时,以下是错误代码示例:

js
typeof foo === undefined;
typeof bar == Object;
typeof baz === "strnig";
typeof qux === "some invalid type";
typeof baz === anotherVariable;
typeof foo == 5;

requireStringLiterals 设置为 true 时,以下是正确代码示例:

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

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "valid-typeof": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "valid-typeof": "error",
  },
});
bash
oxlint --deny valid-typeof

版本

此规则在 v0.0.3 中加入。

参考