-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Optional abstract members #6413
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
What would be an observable difference between optional and abstract optional? |
you can do this today with class an interface merging: interface Component {
optionalMethod?(l: GetLocalization): void;
}
abstract class Component {
public abstract mustImplement(): void;
}
class Post extends Component {
public mustImplement(): void {
if (this.optionalMethod) {
// do something
}
}
} |
@mhegazy your trick seem to have solve my issue. Though I think defining members and having some of them on a interface declaration and some of them in the class declaration is quite confusing. Besides there is some life-cycle methods that I don't want to expose publicly. I guess your solution only works with public members?
I guess this could be said with non-optionals as well? I guess the noticeable difference is that, access modifier can be set to private and protected. |
looking at the original post again, i do not think |
I think I'm fine with empty body methods if Little bit OT, is there a reason you don't autocomplete methods on an extended class? |
@mhegazy Currently TypeScript supports marking an abstract as optional but does not allow concrete classes to omit the implementation, this looks weird to me: abstract class Foo {
abstract foo?(): void;
}
class Bar extends Foo {
// ERROR: Non-abstract class 'Bar' does not implement inherited abstract member 'foo' from class 'Foo'.
} And an abstract method, though optional, could suggest the user that "you may want to try out implementing this", instead of "what can I do, I've implemented all those abstract methods, but I still need some tweaks over there". |
With interface/class merging, the types of the optional function are not inferred:
shouldn't p type be inferred here? |
abstract class Component {
optionalMethod?(l: GetLocalization): void;
public abstract mustImplement(): void;
}
class Post extends Component {
public mustImplement(): void {
if (this.optionalMethod) {
// do something
}
}
} I just do not use |
If |
@valotas Parameter types of subclass methods don't ever appear to be inferred: class Parent {
optionalMethod?(s: string): void;
}
class Child extends Parent {
optionalMethod(s) {
s // (parameter) s: any
}
}
abstract class AbstractParent {
abstract abstractMethod(s: string): void;
}
class ConcreteChild extends AbstractParent {
abstractMethod(s) {
s // (parameter) s: any
}
}
interface Iface {
method(s: string): void;
}
class Klass implements Iface {
method(s) {
s // (parameter) s: any
}
} So I don't think that's because of the interface/class merging. |
What helped for me is just to make them optional ( E.g.
|
I think this could be useful for situations where you explicitly want the child class to say But I think the error message should hint that you might have meant |
I know, I know. It's weird to have the populate() method in a PopulationService be optional. HOWEVER! there are a number of tables that we're populating through other services (e.g., meetings, course instances) and we may need to split those out into separate drop() functions so we can prevent deadlocks. Apparently there isn't a way to define an optional abstract method in an abstract class, but if we drop the abstract keyword from the method we can make it optional and it will still enforce the type rules on parameters and return values. See: microsoft/TypeScript#6413
I want to be able to make the implementation optional for abstract member, but it seems like it is not doable today.
I have some component classes and they MUST extend the base component class and optionally implement certain methods with a certain signature. Right now, I can only rely on good documentation.
I want the power of auto-completion to also kick in.
Why is this useful?
It is useful for classes with life cycle methods.
The text was updated successfully, but these errors were encountered: