-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Accessing static private class parameters via this.constructor
#48476
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
Comments
Duplicate of #32452. |
While class Class {
static #field = 3;
constructor() {
console.log(this.constructor.#field);
}
}
class Subclass extends Class {}
new Subclass(); // Error If you're just wanting the field, better to just use |
@Jamesernator Thank you for your input/suggestion but this is still a bug I don't think this is exactly a duplicate of #32452 since this issue is about a specific bug with static+private modifiers, but you're welcome to close it if I'm wrong |
Ah yes the fact it's warning that the private field is never used I would say is a bug. The fact Although I still don't reccomend using Like static private fields are only installed on the class they're declared on, they are not inherited in any way which is why my above example causes a runtime error (because So while the fact there's a warning here is a TypeScript error, my reccomendation is that in spite of this you should be using |
I originally planned to open this issue just about this (encountered in js file),
the way I see it that's intended behavior, the field is 'private' and not 'protected'/'public', so it shouldn't be inherited so for my use case |
This is exactly what #32452 is about. The constructor is simply typed |
And this is a duplicate of #27899. There's no assignment check for public static properties. The reasoning is that the static property can be initialized from anywhere in your code, and this does not apply to private fields. |
Yes but the main bug here is the fact that the warning which isn't a duplicate right? |
If |
ok gotcha closing this and leaving comment there (bumping since its like 3 years old) |
Bug Report
🔎 Search Terms
access static private class parameter this.constructor
🕗 Version & Regression Information
this probably has always been like this since its a 'new' feature
latest test on 4.7.0-dev.20220302 (nightly)
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
if the parameter is only static, it doesn't show the 6133 warning whether its actually used or not
the moment you make it both static + private (
#
), it suddenly shows the warning.and then if the parameter is accessed via
this.constructor
it stills shows the 6133 warning (declared but value never read)Property '#staticPrivateField' does not exist on type 'Function'.ts(2339)
🙂 Expected behavior
this.constructor.#staticPrivateField
should be recognized as accessing the parameter (it does work, and recommended on MDN)The text was updated successfully, but these errors were encountered: