Skip to content
← Back to rules

unicorn/no-magic-array-flat-depth Restriction

What it does ​

Disallow magic numbers for Array.prototype.flat depth.

Why is this bad? ​

Magic numbers are hard to understand and maintain. When calling Array.prototype.flat, it is usually called with 1 or Infinity. If you are using a different number, it is better to add a comment explaining the reason for the depth provided.

Examples ​

Examples of incorrect code for this rule:

javascript
array.flat(2);
array.flat(20);

Examples of correct code for this rule:

javascript
array.flat(2 /* explanation */);
array.flat(1);
array.flat();
array.flat(Infinity);

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-magic-array-flat-depth": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-magic-array-flat-depth": "error",
  },
});
bash
oxlint --deny unicorn/no-magic-array-flat-depth

Version ​

This rule was added in v0.4.2.

References ​