prefer-native-coercion-functions

March 27, 2026 ยท View on GitHub

๐Ÿ“ Prefer using String, Number, BigInt, Boolean, and Symbol directly.

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

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

// โŒ
const toBoolean = value => Boolean(value);

// โœ…
const toBoolean = Boolean;
// โŒ
function toNumber(value) {
	return Number(value);
}

if (toNumber(foo) === 1) {}

// โœ…
if (Number(foo) === 1) {}
// โŒ
const hasTruthyValue = array.some(element => element);

// โœ…
const hasTruthyValue = array.some(Boolean);
// โœ…
const toStringObject = value => new String(value);
// โœ…
const toObject = value => Object(value);
// โœ…
const strings = mixedData.filter((value): value is string => value);

Note

We don't check implicit coercion like:

const toString = value => '' + value;
const toNumber = value => +value;
const toBoolean = value => !!value;

It is recommended to enable the built-in ESLint rule no-implicit-coercion for a better experience.