-
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
"typeof this" returns "this" (instance) instead of its type (class) #51265
Comments
Out of curiosity, it works in flow: I can even extend in class B extends A {
static foo2 = 'bar2';
}
const sub = new B();
sub.getTypeofThis().foo;
sub.getTypeofThis().foo2; That's because flow supports |
This is right, because Basically |
Flow chose a different interpretation of it, I can't speak as to why. |
Is there a way we can do Ended up with this on my base class: /** @return {typeof CustomElement} */
get static() {
return /** @type {typeof CustomElement} */ (/** @type {unknown} */ (this.constructor));
} And these on the subclasses: /** @return {typeof Control} */
get static() { return /** @type {typeof Control} */ (super.static); } It's not major, or production blocking, I wish I didn't have to reapply every time. I did see an issue related to polymorphic this, but wasn't sure if that's the same "issue": #5863 |
Not today, no. We tried making |
@RyanCavanaugh Thanks. I figured I'm not breaking new ground here. Just wasn't sure where to track the conversation. I'll keep an eye on the other filed issue. |
Bug Report
🔎 Search Terms
typeof this
this
constructorOf
🕗 Version & Regression Information
v.4.8.4 will say a property doesn't exist.
v.4.9.0 (nightly) will give the same, but also cannot find this
this and typeof
⏯ Playground Link
https://www.typescriptlang.org/play?suppressImplicitAnyIndexErrors=true&ts=4.9.0-dev.20221021#code/MYGwhgzhAECC0G8CwAoa0IBcyYJbGgDMB7Y6AXmgHIAjMAJyoG5VV1hiA7LegV2EzF6ACgCUiAL6s00ABYBTECGJjE0evMy96nagHchIACZVoUlG2gBzTQBEc8sQC57meYkvoNWndE7y9aFdHURYZc0sbTAAVAE8AB3liQmDnTASkwiCHDxl0AHp89U1tXWCw9C8S32F-QNTRUQA6Dm5MPgEhaEhodMTk7LcKs2l0KOjZXAg0yZhkPOKfXUxZ4YiZcYzkiam0rayVqdzKxdLe2ZauHn5Bem6YXk4Aa05iPV0evszzqbXUddaWGgZEodTgYjCqGITSiDRhdjAsQhUPhMX2cNeemRFhQ0PGszETQUShUoRRm36hB202axOUTGghQwsmIvGMRDAuBA5M0cUp1MJJGIYSAA
💻 Code
🙁 Actual behavior
(typeof this) returns this
🙂 Expected behavior
(typeof this) should return the static context of an instance.
I am aware that I can do
typeof A
, but I'm simplifying for the reproducible.Actual usage is web components:
HTMLElement
andstatic observedAttributes
. Classes that extend the base class (eg =>InputElement
extendsFormControlElement
) will override the static attributes. Therefore, the base classFormControlElement
needs to accessthis.constructor.observedAttributes
, notFormControlElement.observedAttributes
in order to properly see what attributes are being observed. (ie: subclass is extending superclass static properties).The text was updated successfully, but these errors were encountered: