-
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
Impossible to use type inference while redefining an inherited class field while reusing the inherited value. #42372
Comments
Related to #10570 |
Looks like a duplicate of #33899. |
Respectfully, I do not think this issue is a duplicate of those two. The first one does not seem to reuse the previous value of the property, thus only encompassing the "redefinition" part of this issue, while the second one uses I am not that much into Typescript so I might be wrong and my issue might indeed be a duplicate of those but I don't think any of them include the typing issue of EDIT: Woops, sorry jcalz I did not realize you said "related to" and not "duplicate". Indeed the two issues are definitely related. |
The issue of |
The behavior is the same when you use object spread: Playground link But in the end it's in the discretion of the TypeScript team, of course.
jcalz said related, I said duplicate. :-) |
I don't think there's anything here not covered by the existing linked issues. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
redefine class fields inference implicit any circular this
🕗 Version & Regression Information
This is the behavior in every version I tried including nightly and I assume it is a consequence of how Typescript currently handle class fields, which is different from the actual implementation of public class fields in current browser engines.
I assume this bug will disappear when Typescript handle class fields as is without transforming them into assignment on this.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
It is not possible to use type inference when redefining a class field in a sub class using the same name as the super class and reuse the value of the super class field without explicitely typing the proeprty.
It throws: 'prop' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022).
The error would be correct in a standard assignment scenario, but it seems abusive in class fields considering the type of
this.prop
at the moment of the spread operator is actually the type (wrongly) inferred forsuper.prop
, thus not circular.However, the type of
super.prop
, while being the one I would have expected to get forthis.prop
in the context of class fields definition, is wrong forsuper
in itself assuper
is the prototype of the Base class, not the instance itself.So there are two bugs here but they are somewhat linked together because
this.prop
in the context of class field definition should be the type ofsuper.prop
andsuper.prop
should not exist per se.🙂 Expected behavior
this.prop
in the context of class field definition should be the type currently used forsuper.prop
andsuper.prop
should not exist per se as this would more correctly match the behavior of public class fields.As I mentionned earlier, I assume this would change when Typescript outputs actual public class fields, which is not the case for now, but the behavior seems wrong anyway. Especially the typing of
super.prop
. Anyway, the compiler should be able to distinguish between the "normal" context, wherethis.prop
indeed meansthis.prop
and the typing would indeed be circular, and the class field context, wherethis.prop
means the prop previously defined in the super class, thus no circularity.For anyone who would be facing the same issue as I was and who really wants to use type inference rather than manually type the property, I have found a rather hacky workaround to make
super.prop
return the value ofthis.prop
:Playground link with hacky workaround
Please do not fix the type of
super.prop
without fixing the circular type inference ofthis.prop
in the context of class fields, as this would break the workaround. Ideally, these two bugs should be fixed together.I don't expect this to be fixed before Typescript actually starts outputing javascript code with public class fields but I thought you would like to know nonetheless.
The text was updated successfully, but these errors were encountered: