Skip to content

Constructor parameters interfere with bindings in field initializers #35085

Closed
@robpalme

Description

@robpalme

TypeScript Version: 3.8.0-dev.20191112

Search Terms: class constructor parameter public class field initializer esnext

Code

// @target: esnext
// @useDefineForClassFields: true
var m = 0;
class C {    
    a = m;  // <-- tsc error 2301
    constructor(m: number) { }
}

Expected behavior:

No error. Valid ES(next) should not error.

Actual behavior:

tsc error: Initializer of instance member variable 'a' cannot reference identifier 'm' declared in the constructor.(2301)

The error is unwanted and misleading because the user intends for the initializer to refer to the top-level var m and the emitted code does indeed refer to top-level var m!

var m = 0;
class C {
    a = m;
    constructor(m) { }
}

Playground Link: Minimal repro

Related Issues: Related Twitter discussion with @sandersn

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions