Section with classes implementing interfaces, and also think I may have found a potential interface bug ?

Topics: General
Oct 21, 2012 at 12:18 AM

I would like to suggest a new section about implementing an interface with code example.

For me, it was suprising that the example code ( http://www.typescriptlang.org/tutorial/ ) compiled, without the Student explicitly implementing the Person imterface, which is what I expect in a typed language.

It then took me a while to find a section in the documentation which explains this, but it was the last paragraph in 8.1.1 which is:

"Note that because TypeScript has a structural type system, a class doesn’t need to explicitly state that it implements an interface—it suffices for the class to simply contain the appropriate set of instance members. The implements clause of a class provides a mechanism to assert and validate that the class contains the appropriate sets of instance members, but otherwise it has no effect on the class type."

I think a new section could be named something like "Class interface implementation specification" (and be located just before or after the section 8.1.1 "Class heritage specification").

In such a section I think there also should be an explicit syntax example (i.e. not just the abstract kind of syntax, Backus–Naur Form I believe is used in 8.1.1) about how to implement an interface.

As far as I understand the above quoted specification text about interface, it is only optional to write that a class implements an interface, but it should work the same as if you do not write it.

In the currently published tutorial example (about Student and Person) the code compiles fine, so therefore I think it should work the same way if I simply add "implements Person" but it does not, because the types are then not the same.

To make it work with an explicit interface, I also had to change the types of firstname and lastname to be explicit declared as strings, like this:

class Student implements Person {
    fullname : string;
    constructor(public firstname: string, public middleinitial, public lastname: string) {
        this.fullname = firstname + " " + middleinitial + " " + lastname;
    }
}

Another alternative for the code example to work the same with or without the explicit "implements" clause would be to change the interface like this:

 

interface Person {    firstname: any;    lastname: any; }

 

Then just adding the "implements" clause as below would compile, without having to specify the type in firstname and lastname

class Student implements Person {
    fullname : string;
    constructor(public firstname, public middleinitial, public lastname) {
        this.fullname = firstname + " " + middleinitial + " " + lastname;
    }
}

My point is that if an interface is defining properties with a certain type, then the class (which can be used as implementing the interface) should also have the same types for those properties, regardless of whether or not the optional "implements" clasue is used.

As it seems to me, it currently works more like an untyped dynamic language if you leave out the optional implements clause. 

Is that really intentional or is it a bug ?