Files
scriptc/tests/diagnostics/fresh-spread-repeated.ts
T
Chris Tate 3195f98401 Convert a const array spread only when it runs once per initializer
A const initialized by an array literal and spread once can still run
that spread more than once: in a loop body, loop test or update, a
function, or a class field initializer. Each run inserts the same
elements in JavaScript, so copying them into the destination's layout
would break identity. Those spreads keep the element-layout refusal.
2026-10-20 18:11:57 -05:00

17 lines
791 B
TypeScript

// A const array spread that runs more than once per evaluation of its
// initializer inserts the same elements each time: in a loop body or in a
// function, `results[1] === results[2]` holds in Node. Converting the
// elements into the destination's layout would copy them on each run, so
// these spreads keep the element-layout refusal.
type Result = { message: string; file: { path: string } | undefined; noisy: boolean };
const results: Result[] = [];
const looped = [{ message: "loop", file: undefined, noisy: false }];
for (let i = 0; i < 2; i++) results.push(...looped);
const called = [{ message: "call", file: undefined, noisy: true }];
function addCalled(): void {
results.unshift(...called);
}
addCalled();
addCalled();
console.log(results.length, results[0] === results[1]);