📝 Disallow Array#flatMap() callbacks that only wrap a single item.
💼🚫 This rule is enabled in the ✅ recommended config. This rule is disabled in the ☑️ unopinionated config.
🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
Array#flatMap() is useful when one input item can become multiple output items. When a callback only returns [item] or condition ? [item] : [], .map(), .filter(), or .filter().map() is clearer.
This rule currently checks simple arrow callbacks that return either a one-item array or condition ? [item] : []. More complex callback bodies are intentionally ignored.
In TypeScript files, including Vue SFC <script> blocks with lang="ts" or lang="tsx", conditional callbacks like value ? [value] : [] are ignored because rewriting them to .filter() or .filter().map() can lose TypeScript control-flow narrowing and change the inferred type. Direct one-item callbacks like value => [value.id] are still reported.
// ❌
const ids = array.flatMap(value => [value.id]);
// ✅
const ids = array.map(value => value.id);// ❌
const ids = array.filter(value => value.active).flatMap(value => [value.id]);
// ✅
const ids = array.filter(value => value.active).map(value => value.id);// ❌
const active = array.flatMap(value => value.active ? [value] : []);
// ✅
const active = array.filter(value => value.active);// ❌
const ids = array.flatMap(value => value.active ? [value.id] : []);
// ✅
const ids = array.filter(value => value.active).map(value => value.id);// ✅
const descendants = array.flatMap(value => value.children);// ✅
const values = array.flatMap(value => [value, value * 2]);