1

I have a problem with type script generated js file and I can't solve it, this is my service file that I have problem with it:

namespace Providers {

    export interface IProvider {
        loadTranslations(translations: ITranslations, locale: string);
    }
    module.provider('lego', LegoProvider); 

    class LegoProvider implements ng.IServiceProvider ,IProvider{

        loadTranslations(translations:ITranslations , locale: string) {
            ......
        }

        $get() {
            .....
        }
    }

}

and generated js file is:

var Providers;
(function (Providers) {
    module.provider('lego', LegoProvider); // this is the problem
var LegoProvider = (function () {
    function LegoProvider() {
}
LegoProvider.prototype.loadTranslations = function (translations, locale) {
};
LegoProvider.prototype.$get = function () {
};
return LegoProvider;
}());
})(Providers || (Providers = {}));

It throws error because of the LegoProvider is variable and in that line it is still undefined. when I change code with this, it works correctly:

namespace Providers {

    export interface IProvider {
        loadTranslations(translations: ITranslations, locale: string);
    }


    class LegoProvider implements ng.IServiceProvider ,IProvider{

        loadTranslations(translations:ITranslations , locale: string) {
            ......
        }

        $get() {
            .....
        }
    }
    module.provider('lego', LegoProvider); // I've moved this line to bottom

}

In typescript code LegoProvider class is accessible from both positions and it doesn't make sense for me that the first position doesn't work

3
  • You're referring to LegoProvider before you've initialized it. Makes sense to me that that's a problem, which you've solved by waiting to use it until after you've initialized it. (FWIW, in ES2015, you can't access the constructor created via class before the class statement in the source; you get an error. TypeScript's apparently not being as strict about it, but fundamentally looks like the same problem.) Commented Aug 8, 2016 at 6:36
  • @T.J.Crowder How can I fix this problem? Commented Aug 9, 2016 at 4:09
  • 1
    The way you've fixed it in your question is the only way that makes sense to me. Commented Aug 9, 2016 at 6:45

1 Answer 1

1

It looks like a classic hoisting issue:

The generated js file has a provider that is in fact a constructor function that is stored in a variable (an IIFE to be percise):

var LegoProvider = (function () {
    function LegoProvider() { ...

When you register the provider to your app, the variable hasn't been initialized yet, only declared. This is what actually happens:

   var LegoProvider = undefined; // hoisting

   module.provider('lego', LegoProvider);

   LegoProvider = (function () {
       function LegoProvider() { ...

Your correction has moved the registration after the full initialization of your LegoProvider class.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I know, And the problem is how to solve it without moving the registration line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.