Closed
Description
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