Skip to content

parameter initializer wrongly references variable declared in function body #28474

@ajafff

Description

@ajafff

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 😆)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions