mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-15 04:50:40 +08:00
27 lines
1.1 KiB
TypeScript
27 lines
1.1 KiB
TypeScript
// The Map surface beyond the lowering: the lib declares all of this, so
|
|
// it TYPECHECKS (seeded constructors, iterators, chaining, 3-param forEach
|
|
// callbacks) — each use is fenced per site by the lowerer, never truncated.
|
|
const m = new Map<string, number>();
|
|
|
|
// Entry-seeded construction lowers for an array literal of pair literals
|
|
// at the construction site and for [K, V][]-typed tuple-array values;
|
|
// other seeds (another Map, general iterables) stay fenced.
|
|
const seeded = new Map(m);
|
|
|
|
// keys()/values()/entries() lower ONLY where an array spread drains them
|
|
// on the spot ([...m.keys()]); a stored iterator would need the protocol.
|
|
const ks = m.keys();
|
|
const vs = m.values();
|
|
const es = m.entries();
|
|
|
|
// Spreading the MAP itself (no [Symbol.iterator] lowering) stays fenced —
|
|
// for-of over the map and spreads of its keys()/values()/entries() are the
|
|
// lowered iteration forms.
|
|
const spread = [...m];
|
|
|
|
// set() is declared void (not the JS `this`), so chaining is a type error.
|
|
m.set("a", 1).set("b", 2);
|
|
|
|
// The forEach callback receives (value, key) — no third map parameter.
|
|
m.forEach((v, k, theMap) => console.log(k, v));
|