mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
Iterable<T> maps to a union of the sources that keep their own representation (T[], Set<T>, Map<K, V> for [K, V] entries, string) and a generator over T that holds every iterator. An array stays the same multi-pass object; a generator stays single-pass. for-of gives the array arm its own array loop, so iterating an Iterable that holds an array runs at array speed. Spread, Array.from, the Set/Map constructors and Object.fromEntries consume it; optional Iterables narrow into it; generator functions may declare an Iterable return. Class iterables and typed arrays are refused at the conversion with a named diagnostic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
18 lines
550 B
TypeScript
18 lines
550 B
TypeScript
// An Iterable<T> value holds an array, Set, Map, string or iterator. A
|
|
// class instance with its own [Symbol.iterator] and a typed array have no
|
|
// representation there, so the conversion site reports them by name.
|
|
class Countdown implements Iterable<number> {
|
|
*[Symbol.iterator](): Iterator<number> {
|
|
yield 2;
|
|
yield 1;
|
|
}
|
|
}
|
|
function sum(xs: Iterable<number>): number {
|
|
let s = 0;
|
|
for (const x of xs) s += x;
|
|
return s;
|
|
}
|
|
console.log(sum(new Countdown()));
|
|
console.log(sum(new Uint8Array([1, 2])));
|
|
export const marker: number = 1;
|