External module and interfaces

Topics: Language Specification
Mar 13, 2014 at 11:24 AM

I found that the syntax to import external modules was a bit too heavy because of the module prefix in front of each type, so I decided to switch to one type per file (like in Java, where you create a new file for each class) and use the "export =" syntax, so that I could use my imported type without any prefix.

While doing that change I run into a problem : some of my files were containing interfaces, so to comply with my new approach, i moved them into some dedicated external module. While the code was working fine, I realize it was a kind of nonsense because typescript interface do not generate javascript code when compiled, so I was basically dynamically loading (through an HTTP query) an empty javascript file.

Then, I wonder, because interface do not generate javascript code, would an "d.ts" ambient declaration more appropriate. Then i discovered that only class could be "declared" and not interface, so i decided to swith to an internal module for my interface, and keep external module for the implementation.

According to you, does this pattern of mixing internal module (for declaration that do not generate javascript code) and external module (for implementations, concrete classes) makes sense ? Have you got something better ?

And don't you think there is a kind of redundancy between the "ambient declaration" part of the spec and non-ambient object like interface, enum that feeds only the type system without generating code.

While we have both ambient class and interface ? Aren't them the same thing with two different syntax ?
Mar 13, 2014 at 7:40 PM
Just put interfaces in the .d.ts along with the ambient class definitions. The reason you can't add 'declare' to an interface is because it's a completely redundant modifier. Declare is saying 'this is a type information for something but do not generate JavaScript code for it,' but an interface is already exactly that kind of thing. That's different from needing to define type information for something which does ultimately have JavaScript code defining an implementation like a class definition.

Do not mix internal and external modules, you will have a bad time.