Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parameter initializer wrongly references variable declared in function body #28474

Closed
ajafff opened this issue Nov 11, 2018 · 4 comments
Closed
Assignees
Labels
Bug A bug in TypeScript
Milestone

Comments

@ajafff
Copy link
Contributor

ajafff commented Nov 11, 2018

TypeScript Version: 3.2.0-dev.20181101

Search Terms:

Code

let baz = 2;
function foo(bar = () => baz) {
    var baz = 1;
    return bar();
}

Expected behavior:
No error.
baz in the initializer references the outer let baz = 2.
let baz = 1 is marked as unused.

Actual behavior:
Error Initializer of parameter 'bar' cannot reference identifier 'baz' declared after it.

At runtime: calling foo() returns 2, which shows that declarations in the function body are not in scope of parameter initializers.

Note that other parameters are in scope and can be used inside other parameters' initializer.

Playground Link: https://agentcooper.github.io/typescript-play/#code/DYUwLgBARghgXhAvBATAbgFADMCuA7AYzAEsB7PCLU0gClgCckIaBKJAPmnjYG8MIBEAG4xGsBMgCMmQRHrgc9Cg1aYAvkA

Related Issues:
#22769
/cc @Kingwl maybe you want to give this a shot as well (I hope this doesn't cause as much trouble as the last one 😆)

@Kingwl
Copy link
Contributor

Kingwl commented Nov 12, 2018

🤷🏻‍♂️ holy shit
I'will take a look soon

@weswigham weswigham added the Bug A bug in TypeScript label Nov 12, 2018
@ajafff
Copy link
Contributor Author

ajafff commented Nov 14, 2018

Continuing discussion from #28501 (comment)

@weswigham @Kingwl

I'm pretty sure that error reflects the behavior of our actual down level emit rather than the spec.

That's correct, but we already had that discussion in #22769. I'd argue that the emit should be changed to follow the spec. However, in #22769 (comment) there's the decision to change the lookup for ES6 and emit an error for ES5 and below (which means no change for that target).

That means it's still a bug if target is ES6 or above and still needs fixing.

@Kingwl
Copy link
Contributor

Kingwl commented Nov 14, 2018

ahhhhhh, I'll reopen that

@weswigham weswigham added this to the TypeScript 3.3 milestone Dec 8, 2018
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 3.3 milestone Feb 1, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
@sandersn
Copy link
Member

This is fixed in 3.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants