Accidental interface extension

Topics: Language Specification
Oct 8, 2012 at 6:15 PM

TypeScript makes it possible to extend interfaces accidentally since the following will either add a name property to an existing interface or will create a new interface:

 

interface MyInterface {

	name : string;

}

 

Would it be preferable to separate defining and extending interfaces like this:

 

interface MyInterface { // If MyInterface already exists, this will cause a compile error

	name : string;

} 

interface MyInterface extends MyInterface { // If MyInterface doesn't exist, this will cause a compile error

	name : string;

}

 

Oct 9, 2012 at 1:16 PM

Rather than `extends`, I think the `partial` keyword makes more sense, but I agree that this could be a potential issue. It's possible that for compatibility reasons you might want all interfaces to be partial by default. In that case, perhaps there should be a `final` or `sealed` keyword that indicates the interface cannot be arbitrarily extended. 

Coordinator
Oct 9, 2012 at 3:45 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Oct 9, 2012 at 3:46 PM

Agreed.  It would be helpful to be able to say you know you want to extend an interface rather than extending it by default (perhaps accidentally).  I've copied this to a feature request in the issue tracker to keep track of it.

Oct 11, 2012 at 8:18 PM
Edited Oct 11, 2012 at 8:39 PM
MgSam wrote:

Rather than `extends`, I think the `partial` keyword makes more sense, but I agree that this could be a potential issue. It's possible that for compatibility reasons you might want all interfaces to be partial by default. In that case, perhaps there should be a `final` or `sealed` keyword that indicates the interface cannot be arbitrarily extended. 

impartial?

It's not sealed or final either, really.  You still want to be able to build on it, it's just like you said, it needs to be non-partial if partial is the default.