Strange issues with modules being empty when invoking in javascript

Topics: General
Apr 25, 2013 at 1:20 PM
Edited Apr 25, 2013 at 1:26 PM
This is probably related to something I am doing, and it is a bit to complex to give a FULL example of the problem, however I can describe the scenario and see if anyone else has had anything similar...

So I have a pretty big framework, which is split into many smaller re-usable components, then there is a thin UI layer which basically instantiates the components and does stuff with them. Now the components have recently been re-written to make use of Typescript and are compiled into single output files and then included via resource loaders (labjs in this specific instance) in the page.

How I had everything working fine, I had a class similar to this:
module Something.Binders
{
    export class SomeSpecificPageBinder implements Core.Binders.IPageBinder
    {
        public Bind(serviceLocator: any)
        {
            // Do some binding to the page
        }
    }
}

// this would then be used in the page like so
<script ...>
    var pageBinding = new Something.Binders.SomeSpecificPageBinder();
    pageBinding.Bind(somePreviouslyInstantiatedServiceLocator);
</script>
However then there were about 3 or 4 pages which were all within the same logical section so it made sense to make a module specific binder and then let all other binders inherit from that, so I went about doing so:
// SomeSpecificPageBinder.ts
module Something.Binders
{
    export class SomeSpecificPageBinder extends SomeGenericPageBinder
    {
        public Bind(serviceLocator: any)
        {
            super.Bind(serviceLocator);
            // Do some binding to the page
        }
    }
}

// SomeOtherSpecificPageBinder.ts
module Something.Binders
{
    export class SomeOtherSpecificPageBinder extends SomeGenericPageBinder
    {
        public Bind(serviceLocator: any)
        {
            super.Bind(serviceLocator);
            // Do some binding to the page
        }
    }
}

// SomeGenericPageBinder.ts
module Something.Binders
{
    export class SomeGenericPageBinder implements Core.Binders.IPageBinder
    {
        public Bind(serviceLocator: any)
        {
            // Do some binding for all pages
        }
    }
}


// this would then be used in the page like so
<script ...>
    var pageBinding = new Something.Binders.SomeSpecificPageBinder();
    pageBinding.Bind(somePreviouslyInstantiatedServiceLocator);
</script>
So all compiles file like before, however when I go to use the object in the page I get the error:
Something.Binders.SomeSpecificPageBinder is not a constructor
Which is odd, as if I debug Something.Binders is just an empty object whereas before it contained all the exported classes. Now what makes it even more odd is the fact that if I look in the outputted javascript all the classes are there, and have been exported.

So is there any way in which Typescript could somehow output the javascript in such a way that it would cause the object to be emptied? I was originally doing this with 0.8.3 however I thought maybe it was a bug and am trying the newer 0.8.9 (0.9.0 alpha) version and this has same issue...

The javascript file outputted from the compiler has lots of other classes which are all exported fine and these files mentioned are the final ones in the file, and the LAST class outputted is the Generic one, which I would have expected to be before the 2 others, but I dont think it should matter too much as they are only invoked later on once all the files have been loaded.
Apr 25, 2013 at 1:34 PM
I have just tried to mess about with a few things after doing the brain dump above it the issue is down to the order in which TS is outputting the classes in the javascript, as all is compiled through the typescript compiler with the --out flag. The files are found via a wildcard lookup in a rake script (*/.ts) so its not like you can really infer any order, it just finds them when it finds them.

I have however changed the file name and now that it is alphabetically before the others everything works, although this is not a solution just a temporary workaround, so its related to this issue:

https://typescript.codeplex.com/workitem/913