-
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
Abstract methods #2947
Comments
I was just about to comment on asking for something like this, it's not yet reached a pain point in the project I'm working on (due to the size) but might soon. I simply want to make sure I don't forget to override an abstract method in a base (base...) class and that's it. The feature can be implemented as rigidly as you want as long as it helps me code better - meaning I really don't care about other subtleties as mentioned in #6 though I'm aware some are important to clarify before implementation begins as it clashes with some of the JS oddities. TypeScript is shaping up to be a really great language (seeing what you've started with - JavaScript - can't be considered great unless you're heavily drunk 😉 ) |
Anything new about this? |
@aozgaa is looking into this |
@RyanCavanaugh Is there a way I can write a contract where on extending class's static methods has a certain signature?
React seem to want to put a lot of stuff as static members(GrahpQL, Relay). And I want some autocompletion on those only by extending a class. |
There isn't. Let us know if you see other cases where this would be useful - we generally didn't see a need for it. |
@RyanCavanaugh I have another question about abstract classes. I wonder how I define a function that accepts a derived abstract class that is not abstract and the function should also "new" it. The following doesn't work: function testDerived<T extends typeof ComposerComponent>(DerivedClass: T) {
new DerivedClass(); // error
}
function testDerived(DerivedClass: typeof ComposerComponent) {
new DerivedClass(); // error
} |
|
@RyanCavanaugh does it makes sense with optional abstract methods? I want to define an abstract class with some abstract methods on it. But I want a derived class doesn't need to implement all of the methods. Since you allow optional on interfaces I think it makes very much sense to allow it on abstract methods as well. |
We don't plan to support optional class methods. It's too confusing from a consumer perspective. You can write something like this if you want (note that these would have to be instance members, not prototype methods): class MyClass {
foo: () => number = undefined;
} |
Why can't property getters / setters be abstract? |
@RyanCavanaugh The case when abstract static method can be useful is next. I want to have one abstract class and two child classes. abstract class Abs {
/*public static getSomeValue() {
return null;
}*/
}
class Child1 extends Abs {
public static getSomeValue() {
return 'Some value of class Child1';
}
}
class Child2 extends Abs {
public static getSomeValue() {
return 'Some value of class Child2';
}
}
class Factory {
public static getClasses(): (typeof Abs)[] {
return [
Child1,
Child2
];
}
}
var classes = Factory.getClasses();
for (var index in classes) {
if (classes.hasOwnProperty(index)) {
classes[index].getSomeValue();
}
} So there is two ways to be sure that
Declaring abstract static method in abstract class will resolve the problem. |
The one use case I would really like
Unless there's another way I can go about writing a simple |
Any progress on allowing |
Please log a new suggestion issue for |
@vyshkant @Aarilight did your use cases go away, or are they still valid? If they are, then we should submit an issue as @RyanCavanaugh suggests. If one of you guys starts the issue, I may eventually have a use case to add to it. |
@Aarilight , @troywweber7 , @RyanCavanaugh I have created suggestion issue #14600 about this problem. |
As a continuation of #6, which allows the
abstract
modifier on class declarations, we should extend this to class methods.Relevant points:
abstract
method may only be declared in anabstract
classabstract
methods may not have implementationsabstract
methods may not beprivate
, but may beprotected
super
in a derived clasabstract
abstract
abstract
if it does not overwrite all of its abstract base class'sabstract
methodsabstract
modifierabstract
abstract
modifier must come afterpublic
orprotected
modifierSecondary clarifications:
abstract
has no impact on assignability / subtype / supertype relationsThe text was updated successfully, but these errors were encountered: