Skip to content

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 in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-native-coercion-functions
json
{
  "rules": {
    "unicorn/prefer-native-coercion-functions": "error"
  }
}

References

Released under the MIT License.