wjohnsto
wjohnsto

Reputation: 4463

Intended use for interfaces that extend classes

It appears that TypeScript 0.9+ provides the ability for interfaces to extend classes as follows:

class Foo implements IFoo {
    /**
     * This is bar
     */
    bar = 2;
}

interface IFoo extends Foo { }

var x: IFoo,
    y: Foo = new Foo();

Does anyone know the intention for this feature?

From my experience, the biggest value I get from it is that in IDEs (such as VS), it allows me to see the JSDoc when I type x.bar or y.bar. This is incredibly useful as it avoids having to have duplicate documentation for what is essentially the same property/method.

Because of the huge benefit in terms of documentation, I am leaning towards migrating a bunch of interfaces to use this style, but I don't want to do anything that may be taken out of the compiler at some point, or might cause other issues. Is there anything I should be aware of with regards to this feature? Is there another useful result of this feature that I should know about?

Upvotes: 1

Views: 62

Answers (2)

John Pankowicz
John Pankowicz

Reputation: 4451

I don't know if this is the actual intention, but it does provide a simple way to implement multiple inheritance without needing to create 3 spurious interfaces just for that purpose:

class Tractor {
....
}

class Trailer {
    ....
}


interface ITractorTrailer extends Tractor, Trailer {} 

class TractorTrailer implements ITractorTrailer {
    ....
}

Upvotes: 1

basarat
basarat

Reputation: 276105

Does anyone know the intention for this feature?

Simply because it allows you to capture the type information of a class in an interface.

but I don't want to do anything that may be taken out of the compiler at some point, or might cause other issues

Its a part of the stabalized language specification. See section 7.1 http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf

InterfaceDeclaration:
   interface Identifier TypeParametersopt InterfaceExtendsClauseopt ObjectType
InterfaceExtendsClause:
   extends ClassOrInterfaceTypeList

Explicitly ClassOrInterfaceTypeList

Upvotes: 1

Related Questions