Skip to content

for-of loop does not loop (declarations clash in generated js) #13925

Closed
@eekboom

Description

@eekboom

TypeScript Version: 2.1.4

Compiled with --target ES5.

Code

// A *self-contained* demonstration of the problem follows...
class Main {

	constructor() {
		this.register("a", "b", "c");
	}

	private register(...names: string[]): void {
		for(let name of names) {
			console.log(name);

			this.bar({
				[name + ".a"]: () => { this.foo(name); },
			});
		}
	}

	private bar(a: any): void {}

	private foo(name: string): void {}

}

new Main();

Expected behavior:
Code prints "a", "b", and "c" on three lines
Actual behavior:
Code prints only a single line containing "a".

The bug only occurs when using both a Rest parameter ("...names") and the computed property name ([name + ".a"]) and a for-of loop and the arrow function in the loop body.

When I look at the generated JS I can see that there is a clash in variable declaration in the for-loop and the loop-body that has been extracted into a function.

TypeScript 2.1.5 gives correct output, but I have not found any explicit bug that is supposed to fix this behavior. So I am afraid that it was fixed only as a side effect of some other change and might break again in the future.
Could you include a test case like this?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions