Skip to content
← Back to rules

unicorn/prefer-native-coercion-functions Pedantic

🚧 An auto-fix is planned for this rule, but not implemented at this time.

作用

优先使用原生函数,而不是具有相同功能的自定义函数。

为什么这不好?

如果一个函数等价于 StringNumberBigIntBooleanSymbol,你应该直接使用原生的那个。 将原生函数再包装一层是没有意义的。

示例

此规则的错误代码示例:

javascript
const foo = (v) => String(v);
foo(1);
const foo = (v) => Number(v);
array.some((v) => /* 注释 */ v);

此规则的正确代码示例:

javascript
String(1);
Number(1);
array.some(Boolean);

如何使用

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

json
{
  "rules": {
    "unicorn/prefer-native-coercion-functions": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-native-coercion-functions": "error",
  },
});
bash
oxlint --deny unicorn/prefer-native-coercion-functions

版本

此规则在 v0.0.19 中添加。

参考资料