close

no-array-fill-with-reference-type

Unreleased

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/no-array-fill-with-reference-type': 'error',
    },
  },
]);

Rule Details

Disallow using reference values as Array#fill() values. Array#fill() uses the same value for every element, so mutating one filled object also affects all other elements.

Examples of incorrect code for this rule:

new Array(3).fill({});

const value = new Map();
array.fill(value);

Examples of correct code for this rule:

Array.from({ length: 3 }, () => ({}));
array.fill(0);

A receiver known not to be an array is ignored. This includes typed arrays, whose fill() method coerces the value to a number. Unknown receivers are still checked.

Original Documentation