-
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
Abstract classes that implement interfaces shouldn't require method signatures #22815
Comments
Apparently this can be worked-around with class and interface merging: interface FooFace {
foo();
}
abstract class FooClass implements FooFace {}
interface FooClass extends FooFace {}
let x: FooClass;
x.foo(); // no error That seems redundant, though, I still think this should be fixed. |
If |
@RyanCavanaugh Yes, definitely. In my real-world use case (in which I'm currently using the interface/class merging workaround), my interface actually only has optional methods, I just thought I'd leave that out of this example for simplicity. |
This is all intuitive enough for required members but we can't figure out what should happen to optional members. All choices seem bad. If we copy down optional members and create optional or non-optional abstract members, then there's no way for your derived class to say "I don't want to implement that member". If we copy down optional members and create optional concrete members, then we're going to copy down optional properties (I'm presupposing here that differentiating properties and methods is right out) and you won't have any way for you to say "I don't want to have that property". If we don't copy down optional members, then we've created a "hole" in your type that will allow a derived class to accidently overwrite them with a mismatched type: interface I { x?: number; }
abstract class A implements I { }
// Would not be an error, but could break at runtime
class B extends A { x: string } The last problem is of course present today, but doing some of the copying would seem to create the appearance that we were doing more than was actually happening. Overall it seems like we'd be trading off understandability for convenience here. |
There actually is a way to copy optional members concretely and have a subclass declare that they don't want to implement it [playground]: interface I {
x?: number;
m?: (x: I) => void;
}
abstract class A implements I {
x?: number; // proposal: implicit if no 'x' is specified
m?: (x: I) => void; // proposal: implicit if no 'm' is specified
}
class C extends A {
x?: never; // "I don't want an x property"
m?: never; // "I'm not going to implement this and nobody else better either"
}
class D extends C {
x?: string; // error: Type 'string | undefined' is not assignable to type 'undefined'.
m(x: I) {} // error: Type '(x: I) => void' is not assignable to type 'undefined'.
} Today's state is both error-prone and unergonomic, and the only sound thing to do if a class wants to declare it won't implement an optional property is to carry around the tombstone to guarantee nothing implements it in an incompatible way. Making that (presumably rare case) explicit with a "never" seems like the best approach. |
@RyanCavanaugh Thank you for the thoughtful response, sorry for my delayed reply. I don't understand why this option seems bad?
@shicks is exactly right: the way you say "I don't want to have that property" should be the same as the way you do so when
By contrast, you expect |
For what it's worth, I'm interested in this because Closure Compiler (very recently) allows abstract classes to not redeclare unimplemented interface methods, and I don't want to see code taking advantage of this feature be more problematic to migrate to TypeScript. @RyanCavanaugh what's the process to actually put together a proposal for this? I think the basis is sound, and this should be perfectly viable. |
Another simple option, but it is still redundant is:
and the abstract class :
|
I also encountered this issue. This is what I want to do: interface IQueue<T> {
run: (job: Agenda.Job<T>, done: (err?: Error) => void) => Promise<void>;
}
abstract class Queue<T> implements IQueue<T> {
public abstract run; // This will be implemented in classes
}
class Q extends Queue<IJob> {
async run(job, done) { } // implementation
} But |
@TwoAbove try this structure: // Be aware that I have removed the inner class Job just for test purpose.
interface IQueue<T> {
run(job: Agenda, done: (err?: Error) => void): Promise<void>;
}
abstract class Queue<T> implements IQueue<T> {
public abstract run(job: Agenda, done: (err?: Error) => void): Promise<void>; // This will be implemented in classes
}
class Q extends Queue<IJob> {
public async run(job: Agenda, done: (err?: Error) => void): Promise<void> {
// impl
return null;
}
}
// how it looks like, trying to call test.run with no correct properties will give you error
class RunsQ {
private test: Q;
constructor() {
this.test = new Q();
}
public myTest(): void {
this.test.run({name: ''}, null);
}
} |
2 years later... this seems like an obvious and basic feature for any object-oriented language: how come it hasn't been implemented ? |
Same here, I cannot imagine that there's a problem to solve that, from my p.o.v. it's just a fix in the compiler and won't lead to any issue on runtime. |
Same, most OO languages have the similar feature |
Please, would someone update on the state of this issue ? I'm going to use the workaround but I'd really appreciate knowing more about a future proposal ! |
Ok came here to say wtf .... How can this be missing ? |
I want this fixed in typescript! |
Can't believe this still a problem omg |
Please fix it! |
Uh, It's going to be three years! |
FYI, just dropping "Ugh can't believe it's not fixed already" comments does not change our view of feature priorities. There are outstanding issues with "just doing it" outlined above that haven't been addressed yet. Please engage in meaningful discourse with the content of the thread so far instead of idly complaining. |
So, how can I help to solve this issue? |
If I knew how to solve it, I'd have done so already 🙃. The desirability isn't the missing piece of this - it's clear that this would be nice - but rather that we lack a solution without negative side effects that outweigh what is ultimately a pure convenience thing. We'd need some suggestion for how to implement this in a way that isn't starkly surprising or bad in any of the outlined scenarios. |
> So, how can I help to solve this issue?
If I knew how to solve it, I'd have done so already 🙃. The desirability isn't the missing piece of this - it's clear that this would be nice - but rather that we lack a solution without negative side effects that outweigh what is ultimately a pure convenience thing.
We'd need some suggestion for how to implement this in a way that isn't starkly surprising or bad in any of the outlined scenarios.
I'll try to find a solution and ASAP I found it, I'll share it here.
Thank you for all those things that you do for this community 🥰🙏
|
Can you provide some details about negative impact that you are worrying about? |
I have some interfaces that explain the behaviors, and when I implement them in the abstract class, I expect the abstract to inherit those behaviors. But in typescript, I should implement the interface's methods in an abstract class that introduces redundant and unnecessary boilerplate codes. |
I understand how it works now, but I wanted to know what holds @RyanCavanaugh from implementing. |
This comment outlines the conceptual difficulty of what this actually should mean |
I believe I addressed the concerns in that comment pretty thoroughly. The answer is that it should copy all properties, and that if it's intended to not have the property, then the implementer should declare it as optional |
That's a breaking change, though. This code is currently legal and would become illegal: interface Something {
go(): void;
opt?: number;
}
abstract class M implements Something {
abstract go(): void;
}
declare const x: M;
const m: { opt?: string, go(): void; } = x; |
That's a good point. Unfortunately you're going to break code when you improve safety, by definition. Expanding on your example, m.opt = 'string';
const s: Something = x;
expectOptionalNumber(s.opt); // whoops If the assignment to That said, it's only a minor safety improvement: you could still assign a string to I'd be interested in how much actually breaks in the real world, though, and whether any of the breakages actually point to design flaws in the underlying code. |
I'm not sure if it was mentioned before but; |
@gkdn Can you point to a concrete project where this occurred? I wonder if there's an incremental "half-way" sort of approach, where an initial version could support only non-optional properties in an intuitive way, and then a potential future update could later add support for the optional ones? Is it too confusing/surprising if only required properties are implicitly copied? One should be able to reason about it as copying the bare minimum required to actually satisfy the interface, and optional properties are not required (by definition) so they wouldn't be copied. Would that still be a breaking change? |
Sorry, maybe I wasn't clear earlier. I was not following the discussion on optional properties but I was only referring one of the limitation of the existing behavior of the type system on abstract classes. Library 1: interface IComponent {
onUpdate():void;
onRemoval():void;
} Library 2: abstract AbstractFancyComponent implements IComponent {
onUpdate() {
....
}
// This declaration is necessary today to make the TS type-system happy.
abstract onRemoval():void;
} All changes to However |
Sorry, I'm not very good at TS, but would it be a good solution to add some kind of new keyword, directive, or new instruction to fix this instead of trying to fit this functionality in the existing
interface FooFace {
foo();
bar();
}
abstract class FooClass extends FooFace {
// ^^^^^
// Instead of "implements" use "extends" to convey that you want all
// of the properties in the interfaces to be copied down into the FooClass.
// I'm sorry if this is a bad suggestion. I don't know what the implications of this could
// be, but we can maybe find a new keyword if using "extends" is a bad idea.
}
interface FooFace {
foo();
bar();
}
abstract class FooClass implements FooFace {
"use properties"
// ^^^^^^^^
// Write some kind of keyword or directive in the class to say that
// you want the properties to be copied down into the class.
// Again, I'm sorry if it is a bad suggestion, because I'm not even sure
// if there are any existing directives that can be written in a inner scope
// in a file, and I don't know of any other examples in TS that are similar
// to what I'm suggesting here.
} |
TypeScript Version: 2.7
Search Terms: abstract class implements interface
Code
Expected behavior:
Abstract classes that implement interfaces don't need to define abstract type definitions to satisfy interface.
Actual behavior:
They do.
Playground Link
Related Issues:
The text was updated successfully, but these errors were encountered: