Skip to content
← Back to rules

unicorn/prefer-number-coercion Pedantic

💡 A suggestion is available for this rule.

What it does

Prefer Number() over parseFloat() and base-10 parseInt().

Why is this bad?

parseFloat() and parseInt() parse numeric prefixes and ignore trailing text. Number() parses the full input, which better matches intent when coercing values.

Examples

Examples of incorrect code for this rule:

javascript
const value = parseFloat(input);
const integer = parseInt(input, 10);

Examples of correct code for this rule:

javascript
const value = Number(input);
const integer = Math.trunc(Number(input));

How to use

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

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

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

Version

This rule was added in vnext.

References