.map(...).flat() maps and flattens in two passes

tx0/ts:map-then-flatstrictwarningperformance

What it finds

items.map(f).flat() builds an array of arrays and then a second array from it; items.flatMap(f) does both in one pass and says so.

Reported are calls of .flat() or .flat(1) directly on the result of a .map call with one argument. The fix writes flatMap; it is unsafe, since the receiver may be an object other than an array with methods of these names.

From the rule's source, map_then_flat.rs.

Example from the rule's tests
export function pairs(items: number[][]) {  const a = items.map((x) => x).flat();tx0 reports: can be written .flatMap(...)  const b = items.map((x) => x).flat(2);  return [a, b];}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:map-then-flat" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:map-then-flat] reason="..."

tx0 explain tx0/ts:map-then-flat shows the same in the terminal. Found a false positive? Report it.