-
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
Methods are not accepted as implementation of abstract members with function type #51261
Comments
includes workaround for microsoft/TypeScript#51261.
This is working as intended. Methods and properties behave different. The base class defines a property, your implementation is a method. |
I don't think we need to enforce this rule for abstract members -- it doesn't really make any sense. |
@RyanCavanaugh Can't formulate my thoughts properly about this right now (stupid covid), but wouldn't this risk the implementation screwing up an intended variance? If you allow this, wouldn't it allow this to compile? abstract class Base {
abstract member: (n: string | number) => number;
}
class Derived extends Base {
member(x: number): void {}
} |
|
It wouldn't compile. Method bivariance is determined by the target type, not the source type (I had to look this up). You can try it today and see that that error, along with the one about methodness/propertyness, are both issued. |
includes workaround for microsoft/TypeScript#51261.
includes workaround for microsoft/TypeScript#51261.
includes workaround for microsoft/TypeScript#51261.
includes workaround for microsoft/TypeScript#51261.
includes workaround for microsoft/TypeScript#51261.
It may also be worth noting that whilst |
includes workaround for microsoft/TypeScript#51261.
includes workaround for microsoft/TypeScript#51261.
includes workaround for microsoft/TypeScript#51261.
Bug Report
I am declaring a named type with the signature for an abstract method(ish)* on a base class. But I am not able to use the Signature type directly to declare the method, instead I need to pluck off
Parameters<Sig>
andReturnType<Sig>
to declare a "real" method. Given how methods usually behave in TS interfaces, this is certainly surprising.call
method that implements the interface, and others doconstructor(public readonly call: Sig, ...otherArgs)
so that they have an injectablecall
member. In either case, the consumer of the api just doesmyObj.call(blah, blah, blah)
and it Does The Right Thing. I am declaring the namedSig
to avoid repeating it everywhere.🔎 Search Terms
abstract method member function TS2425
🕗 Version & Regression Information
Repros in oldest and nightly in playground.
I'll admit this is certainly similar to the third from the last issue in that list:
However, that summary and the linked issue are all about bivariance/invariance/contravariance which controls matching semantics for functions with different signatures. In this case, the signatures are identical, so that difference doesn't apply. This appears to be a different difference (at least to me). If this is intentional, perhaps the FAQ should be updated to focus less on variance. Ideally, explaining why even identical signatures are rejected.
Issue #27965 also seems related, but that case is a bit different. It was declaring an actual member, which may exist in JS land on the base, while this issue is specifically about adding an
abstract
member, which I would expect to be more like adding something to theinterface
portion of a class type, since it explicitly isn't set by the base in JS. Also that case tried to useany
as the "function" type of the member, and I am using an exact-matching function type.⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
🙂 Expected behavior
Compile without error
The text was updated successfully, but these errors were encountered: