Skip to content

Latest commit

 

History

History
68 lines (48 loc) · 1.85 KB

prefer-native-coercion-functions.md

File metadata and controls

68 lines (48 loc) · 1.85 KB

Prefer using String, Number, BigInt, Boolean, and Symbol directly

This rule is part of the recommended config.

🔧 This rule is auto-fixable.

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.

Fail

const toBoolean = value => Boolean(value);
function toNumber(value) {
	return Number(value);
}

if (toNumber(foo) === 1) {}
const hasTruthyValue = array.some(element => element);

Pass

const toBoolean = Boolean;
if (Number(foo) === 1) {}
const hasTruthyValue = array.some(Boolean);
const toStringObject = value => new String(value);
const toObject= value => Object(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.