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
LegoProviderbefore 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 viaclassbefore theclassstatement in the source; you get an error. TypeScript's apparently not being as strict about it, but fundamentally looks like the same problem.)