Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

It looks like I have to do full naming when accessing a TypeScript module in javascript. Is this correct?

My TypeScript is:

export module App.editor.menu {

    export class File {
        static isOpenEnabled() {
            return false;
        }

        static openClicked() {
            debugger;
        }
    }
}

And my javascript is:

Ext.onReady(function () {

    define(["../../scripts/ribbon", "./menu-handler"], function (ribbon, handler) {

And I have to call "handler.App.editor.menu.File.isOpenEnabled()" instead of "handler.isOpenEnabled()"

All the examples I've seen don't require the namespace be included. But I haven't seen any examples that are half TypeScript, half javascript so I figure this might be different.

Is the full namespace required in this case?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The idea with namespaces is that you want to avoid name conflicts and clearly specify the domain of a particular type or variable. This is important in things like .NET where the transitive closure of all required types are getting loaded into one domain and a name collision would be highly problematic.

In an external module, this is wholly unnecessary because the code that loads you gets to define exactly what identifier you are bound to, and callers will only be "seeing" the code that they explicitly import.

In general, you should not have a TypeScript file whose only top-level element is export module. Move everything outside and put export on it so your callers can find it more easily. In your specific example, your file should just say:

export class File { ... }

without being in a module (unless you have a bunch of other subdivisions you really want to expose through one external module, which is unlikely).

share|improve this answer
    
If I eliminate the Module part, won't that mean a name conflict if I create another File class elsewhere? –  David Thielen Nov 8 '13 at 22:32
    
No. That's the entire idea with external modules - they are in a different namespace by default. –  Ryan Cavanaugh Nov 8 '13 at 22:49
    
Thanks for the information, Ryan. I haven't done a lot with RequireJS and I was always curious about that. Also, I submitted a small edit to your answer. :) –  Alex Dresko Nov 8 '13 at 23:41
    
First off thank you. I want to make sure I understand this. As long as I have no global code in a .js file, and I always access it using RequireJS define(), then I can have 8 .js files with the same class name and because I get the object via define(), I'm safe as I just use the returned object. Is this correct? –  David Thielen Nov 9 '13 at 20:54
    
And... this is how it should be done? Not using namespaces in this case feels very wrong to me because I'm coming from the C#/Java world. This is the right way to do it? –  David Thielen Nov 9 '13 at 20:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.