Files
scriptc/tests/corpus/destructure-map-set-iterables.ts
T
Malte UblandClaude Opus 5.5 e6dbad9567 Destructure Maps and Sets with array patterns
`const [[k, v], ...rest] = map` was refused (SC1031). The source now
snapshots once into its iteration order (Map entries as pairs, Set
values), the spread's own machinery, and the pattern lowers as an array
pattern over that snapshot, as string sources already do. Element defaults
keep a fence because they run between iterator steps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 15:16:17 -07:00

19 lines
681 B
TypeScript

// Array destructuring of a Map or Set walks its iterator: Map entries as
// [key, value] pairs, Set values, holes and rest included.
const m = new Map([["a", "1"], ["b", "2"], ["c", "3"]]);
const [[name, value], ...rest] = m;
console.log(name, value, rest.length, JSON.stringify(rest));
const [first, , third] = m;
console.log(JSON.stringify(first), JSON.stringify(third));
const s = new Set([3, 1, 2]);
const [x, y, ...others] = s;
console.log(x, y, others);
let [p] = new Map<string, number>([["only", 1]]);
p = ["changed", 2];
console.log(p);
function firstKey(map: Map<string, number>): string {
const [[k]] = map;
return k;
}
console.log(firstKey(new Map([["z", 26]])));