Skip to content

Error in generated js by typescript compiler #10224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pooyaho opened this issue Aug 9, 2016 · 4 comments
Closed

Error in generated js by typescript compiler #10224

pooyaho opened this issue Aug 9, 2016 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@pooyaho
Copy link

pooyaho commented Aug 9, 2016

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 is incorrect

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 9, 2016
@RyanCavanaugh
Copy link
Member

Code runs in the order you write it; TypeScript doesn't re-order it on your behalf. It's possible you needed to run module.provider before class initialization (for example, because LegoProvider initialized a static field in a way that depended on the module.provider call).

@normalser
Copy link

@RyanCavanaugh is there any way that TS could throw an error in such cases - kind of like when you try to use variable that is declared later in the file - got bitten by it myself couple of times

@basarat
Copy link
Contributor

basarat commented Aug 10, 2016

@wallverb you are always going to run into errors if you depend on ordering across files : https://basarat.gitbooks.io/typescript/content/docs/tips/outFile.html

However in this case I think TS should error similar to how it does for other block items:

console.log(foo); // Block scoped variable used before its declaration
let foo;

This is because ES6 classes are block scoped and not hoisted (similar to let/const) 🌹

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 10, 2016

There's #5207.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants