close

new-for-builtins

Configuration

rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/new-for-builtins': 'error',
    },
  },
]);

Rule Details

Many builtin constructors can be called with or without new, but constructor calls are clearer and more consistent when they use new. This rule also reports selected builtin namespace objects that are neither callable nor constructible.

Optional calls that would need to become optional constructors are ignored, because new cannot be optional.

Examples of incorrect code for this rule:

const list = Array(10);
const map = Map([["foo", "bar"]]);
const now = Date();
const tag = WebAssembly.JSTag();
const text = new String("value");

Examples of correct code for this rule:

const list = new Array(10);
const map = new Map([["foo", "bar"]]);
const now = String(new Date());
const text = String("value");

Original Documentation