-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
No error when initializing a class field defined as a read-only getter in base class #34585
Comments
Checked this snippet of code directly in Chrome, and there's no runtime error when evaluating I believe this issue has to do with: #27644 |
I don't know how TypeScript is supposed to handle this, but classfields in ES use |
@AviVahl Yeah, exactly, if TS compiles to the form constructor() {
this.foo = 10
} then it doesn't work. This can happen with either |
This is an error with useDefineForClassFields: true, which will be the default target for target: "esnext" when class fields reach stage 4. That's because it's quite confusing, even though it's legal [stage 3] JS. It could always be an error, but this is a problem that real code rarely encounters. When I looked at our user test suite, the only examples I found were in old, pre- This would be a nice feature but not required, and fairly easy to implement since the error is already there, so I'm going to move this into the backlog and put the Help Wanted label on it. It's a good feature if you want to focus on identifying breaks in the ecosystem. Edit: Well, I thought about it a bit more and decided to at least create the PR. There are lots of breaks in the test suite because we test exactly this weird case a lot, so I'll put it up tomorrow after verifying that they're correct. |
@sandersn Thanks for the fix! Btw, I see the error message is
in 4-beta playground even if That is amazing that when |
We decided to disallow properties overriding accessors in 4.0 for everyone, so that when class fields finally do move to stage 4, any code that could get tripped up by the [[Set]] -> [[Define]] migration would have time to get fixed. |
what's wrong with this if the Child class marks it as readonly? |
There's still some kind of initialisation, and whether that affects base accessor depends if you are compiling with [[Set]] (classic TS) or [[Define]] (ESNext) semantics. For example: class Base {
get foo() {
return 5
}
}
class Child extends Base {
readonly foo = 10
} Still crashes with [[Set]] semantics, because the emitted code has a class Base {
get foo() {
return 5;
}
}
class Child extends Base {
constructor() {
super(...arguments);
this.foo = 10;
}
} |
@sandersn |
@isral Please do open a new bug. It's a lot easier with an example to look at. |
TypeScript Version: 3.6.4
Search Terms:
"Related Issues" feature of GitHub uses the title as search terms.
Code
Expected behavior:
Type error during compile
Actual behavior:
No error until runtime.
Playground Link:
playground
Related Issues:
Might be duplicate of #13347, but not sure.
The text was updated successfully, but these errors were encountered: