TypeScript Version: Latest online playground version
Code
var [a, b, c] = {
[Symbol.iterator]:
() => [1, 2, 3][Symbol.iterator]()
}
Expected behavior: It to compile.
Actual behavior: The following error:
Type '{ [Symbol.iterator]: () => IterableIterator<number>; }' is not an array type.
A couple notes on downlevel emits:
- A correct downlevel emit for iterators could be accomplished with this helper + current array destructuring, if you'd rather keep the code concise:
function _fromN(rhs, len) {
var ret = Array(len), iter = rhs[Symbol.iterator]();
for (var i = 0; i < len; i++) {
var next = iter.next();
if (next.done) { iter.close(); for (; i < len; i++) ret[i] = void 0; break; }
ret[i] = next.value;
}
return ret;
}
- A correct downlevel emit for iterators could be accomplished with this code gen, if you'd rather keep it fast (i.e. no array allocation - this is what modern engines do):
let iter = gensym(), next = gensym();
emit(`var ${iter} = (${rhs})[Symbol.iterator]()`);
emit(`var ${next} = ${iter}.next()`);
emit(`if (!${next}.done) do {`);
for (const name of bindings) {
if (!isElision(name)) emitAssign(name, `${next}.value;`);
emit(`if ((${next} = ${iter}.next()).done) break;`);
}
emit(`${iter}.close();`);
emit(`} while (false);`);
TypeScript Version: Latest online playground version
Code
Expected behavior: It to compile.
Actual behavior: The following error:
A couple notes on downlevel emits: