6

Closed

Compiler doesn't recognize named functions

description

This is a very common piece of code in JavaScript that does not compile in tsc:

window.addEventListener('load', function winLoad() {
window.removeEventListener('load', winLoad);
});

Compiler complains:

The name 'winLoad' does not exist in the current scope

This is valid JavaScript.
Closed Feb 25, 2013 at 5:18 PM by RyanCavanaugh
Confirmed fix.

comments

ricmrodrigues wrote Oct 3, 2012 at 5:15 PM

Indeed it's valid, but what's the point? Probably that's the type of clutter that Anders speaks about in the movie that they want to disable.

mateo2 wrote Oct 3, 2012 at 5:34 PM

The point is to not pollute the parent scope. Since all valid JavaScript is supposed to be valid TypeScript, this is definitely a bug.

rwaldron wrote Oct 3, 2012 at 6:36 PM

Yes, this violates ES5.1, 13

The production
FunctionExpression : function Identifier ( FormalParameterListopt ) { FunctionBody }
is evaluated as follows:

...skipping to the good stuff...


NOTE The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.

jopamer wrote Oct 3, 2012 at 9:12 PM

Thanks, everyone. This is a known bug in the TypeScript compiler, and one that I'd like to fix before too much more time transpires :)

jonturner wrote Feb 6, 2013 at 9:43 PM

I can't seem to repro this. Marking as fixed.