Files
scriptc/tests/corpus/6477-fresh-array-spread-insertion.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

20 lines
1.1 KiB
TypeScript

// Spreading a fresh array literal whose elements are typed by their own
// literal (an omitted member reads `undefined`) into an array of the
// declared element type converts the elements; nothing else can reach
// them.
type Result = { message: string; file: { path: string } | undefined; noisy: boolean };
const results: Result[] = [{ message: "first", file: { path: "/a" }, noisy: false }];
const spam = results.length > 0 ? [{ message: "spam", file: undefined, noisy: true }] : [];
const more: Result[] = [{ message: "m", file: undefined, noisy: false }];
results.unshift(...spam, ...more);
const tail = results.length > 5 ? [] : [{ message: "tail", file: undefined, noisy: false }];
results.push(...tail);
console.log(results.map((r) => `${r.message}:${r.file?.path ?? "-"}:${r.noisy}`).join(" "));
console.log(JSON.stringify(results[1]));
// A const declared in the loop body is a new array on every iteration.
for (let i = 0; i < 2; i++) {
const step = [{ message: `step${i}`, file: undefined, noisy: false }];
results.push(...step);
}
console.log(results.map((r) => r.message).join(" "), results[results.length - 1] === results[results.length - 2]);