typescript/prefer-string-starts-ends-with Nursery
它做了什么
优先使用 startsWith 和 endsWith,而不是手动进行字符串边界检查。
为什么这是不好的?
使用 slice、indexOf、正则表达式锚点或手动索引编写的边界检查,比使用 startsWith/endsWith 难以阅读和维护。
示例
此规则的 错误 代码示例:
ts
value.slice(0, 3) === "foo";
value.slice(-3) === "bar";此规则的 正确 代码示例:
ts
value.startsWith("foo");
value.endsWith("bar");配置
此规则接受一个配置对象,包含以下属性:
allowSingleElementEquality
type: "always" | "never"
default: null
是否允许对第一个/最后一个字符进行相等性检查。
如何使用
要通过配置文件或在 CLI 中 启用 此规则,可以使用:
json
{
"rules": {
"typescript/prefer-string-starts-ends-with": "error"
}
}bash
oxlint --type-aware --deny typescript/prefer-string-starts-ends-with