-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Constructor parameters interfere with bindings in field initializers #35085
Comments
Once the above compile-time error has been fixed, a closely related issue that I am willing to raise separately is that the emit with default compiler options ( var m = 0;
class C {
constructor(m) {
this.a = m;
}
} |
I tested back to 2.3, and this error is the same on every version 2.3 - present, so I guess that it's been this way since at least 1.1. It might even be a relic of the old class syntax: class C(m: number) {
a = m
} Edit: No I'm just being dense. It's because pre-class-field emit initialises property declarations in the constructor, so it's impossible to refer to the outer |
Yes, the build error has been around for a long time. And I can appreciate TypeScript's current downlevel emit incurs the need for this error. It is possible to fix this. Babel solves this downlevel case by renaming ("deconflicting") the inner declaration from var m = 0;
class C {
constructor(_m) {
Object.defineProperty(this, "a", {
enumerable: true,
configurable: true,
writable: true,
value: m
};
};
} |
#36405 fixes the already-working ENext+useDefineForClassFields case by removing the error, since the emit is already correct. It does not attempt to fix the emit for the classic case, and the error remains. @robpalme you are welcome to open a separate issue for the useDefineForClassFields: false case as you suggested, but I don't think it's worth fixing — Typescript has had this error for ages without huge user demand for removing it. In any case, code that relies on shadowing is usually error-prone when generated by humans. It would be worth considering if somebody has a need to easily generate code with this structure. |
Thank you for the checker fix, @sandersn I agree the |
TypeScript Version: 3.8.0-dev.20191112
Search Terms: class constructor parameter public class field initializer esnext
Code
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-levelvar m
!Playground Link: Minimal repro
Related Issues: Related Twitter discussion with @sandersn
The text was updated successfully, but these errors were encountered: