Allow non-inherited static class members. #31493
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Search Terms
inheritance inherit static class extendStatics
Suggestion
Provide a way to mark static class members as non-inherited. This is particularly meaningful for factory methods.
Use Cases
Static factory methods are often useful because they can be used for async construction of an object. They also can be useful for providing alternative constructor implementations (effectively) since TypeScript doesn't support constructor overloads (without a bunch of conditional ugliness in the constructor). Unfortunately, while the constructor for a subclass isn't inherited by the superclass, all static members are which means you cannot have a factory method like
create
orfrom
in both the subclass and the superclass. One of them gets the name, the other doesn't.Looking at the emitted code, there is explicit code for emitting static class extension. It would be nice if there were a way to tell the compiler to not emit static extension for a particular static method.
Examples
The above code currently fails to compile. It would be nice if I could somehow annotate the
from
method onPoint2
such that it was not inherited. Currently, if I rename them tofrom1
andfrom2
then a user callingPoint3.from1(...)
will get back aPoint2
instead of the expectedPoint3
. When I am authoring aPoint2
factory method, I want to tell the compiler, "It doesn't make sense to call this static method from the context of a derived class".Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: