unicorn/new-for-builtins
Require new for built-in classes and disallow it for String, Number, Boolean, Symbol, BigInt
Some built-ins work with and without new, some throw without it, and some do something else. Array(3) is the same as new Array(3), Map() throws a TypeError, and Date() returns a string, not a Date. This rule requires new for built-in classes such as Array, Object, Date, Error, RegExp, Promise, Map, Set and the typed arrays.
The primitive wrappers are the other way around. new Boolean(false) is an object, so it's truthy, and typeof new String("a") is "object". new Symbol() and new BigInt(1) throw. Call these without new.
Configure#
Off by default. To turn it on:
[lint.rules]
"unicorn/new-for-builtins" = "error" # or "warn"Examples#
const slots = Array(3)Use new Array() instead of Array();
const seen = Map()Use new Map() instead of Map();
const failure = Error("Not found")Use new Error() instead of Error();
const now = Date()Use String(new Date()) instead of Date();const enabled = new Boolean(false)Use Boolean() instead of new Boolean();
const total = new Number(input.value)Use Number() instead of new Number();const slots = new Array(3);
const seen = new Map();
const failure = new Error("Not found");
const now = String(new Date());const enabled = Boolean(input.checked);
const total = Number(input.value);Object(value) === value is a common test for "is this an object", so it's allowed:
const isObject = value => Object(value) === value;A name that your file declares or imports isn't the built-in, so it isn't checked:
import { Map } from "immutable";
const seen = Map();