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.

What it does

Prefers built-in functions over custom ones with the same functionality.

Why is this bad?

If a function is equivalent to String, Number, BigInt, Boolean, or Symbol, you should use the built-in one directly. Wrapping the built-in in a function is moot.

Examples

Examples of incorrect code for this rule:

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

Examples of correct code for this rule:

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

How to use

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

Version

This rule was added in v0.0.19.

References