Skip to content
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

Allow default methods/instance variables in interfaces #3536

Closed
dead-claudia opened this issue Jun 17, 2015 · 3 comments
Closed

Allow default methods/instance variables in interfaces #3536

dead-claudia opened this issue Jun 17, 2015 · 3 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@dead-claudia
Copy link

This would be epic to have. Java itself had a problem with forcing the implementing class to implement the abstract methods, and solved it with default interface implementations. Sometimes, multiple inheritance is necessary to have. For example, I have a sea of crisscrossing is-a relationships to handle, with a ton of things like the following:

class Identifier extends Node implements Assignable, ImportPatternEntry,
        IdentifierLike, PropertyLike, Subtypable {
    // stuff...
}

class MemberExpression extends Node implements Assignable,
        IdentifierLike, PropertyLike, Subtypable {
    // stuff...
}

It's not pretty, but I also have to check many of these relationships at runtime as well. I can't use traditional single inheritance for this, either, because none of these are mutually exclusive. I would love to have ways to set default properties on these classes, to where I don't have to make a build script to generate all this. (I have 30+ base classes and 20+ interfaces with very intertwined relationships like the above.)

What I was thinking of was something like this:

interface IFoo {
  bar = true;
}

class Foo {}

class Bar implements IFoo {}

new Foo().bar; // undefined
new Bar().bar; // true
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Jun 17, 2015
@danquirk
Copy link
Member

Definitely feels like part of a mixin story.

@dead-claudia
Copy link
Author

I'm going to close this for now, since I'm looking at the fact it's not as nice as the original proposal on the other thread.

@izogfif
Copy link

izogfif commented May 31, 2018

Now that mixins were added in the language, could someone provide an example of the interface with default method via mixin? How to write

interface Foo {
  getValue: number;
}
interface Bar extends Foo {
  getValue(): number { // Default implementation, may be overridden in children
    return 42;
  }
}

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants