Typescript error : A 'super' call must be the first statement in the constructor when a class contains initialized properties
When you extend a class, your constructor:
- Must call
super()
- Must do that before it does anything else
In your instance, you just need to re-order things:
declare var App: ng.IModule;
module CoreWeb {
export class EntityMasterController extends Controller {
private currenciesDataSet;
private entity: IEntityMasterModel;
private merchandisingConstants;
private typeAheadOptions;
constructor(
$q:ng.IQService,
$rootScope,
$scope:ng.IScope,
$state,
$translate:ng.translate.ITranslateService,
appEvents,
commonValidationsService,
entityDataService,
merchandisingConstants,
ngDialog,
notificationsService,
titleDataFactory
) {
// Must be first
super(
$q,
$rootScope,
$scope,
$state,
$translate,
appEvents,
commonValidationsService,
"entity",
null,
entityDataService,
"name",
ngDialog,
notificationsService,
"createEntity",
"getCurrentEntity",
"getEntity",
"updateEntity",
titleDataFactory
);
this.merchandisingConstants = merchandisingConstants;
}
This is quite a hack, but it is a simple workaround to the problem:
super(
(this.merchandisingConstants = merchandisingConstants, $q),
$rootScope,
$scope,
$state,
$translate,
appEvents,
commonValidationsService,
"entity",
null,
entityDataService,
"name",
ngDialog,
notificationsService,
"createEntity",
"getCurrentEntity",
"getEntity",
"updateEntity",
titleDataFactory
);
That uses the somewhat weird and not-often-useful JavaScript ,
operator to stuff the assignment in there as a side effect. You could do it with any of the parameters, really, but I did it with the first one. The comma operator — which is not the same as the comma that separates arguments to a function, even though of course it's exactly the same character — lets you string together a list of expressions, all of which will be evaluated. Only the last one is used as the value of the overall expression.
Thus, by doing that, you do your assignment while the argument list is being evaluated. The value of the first parameter will still be $q
, but the assignment will happen too, before the function call to super()
is made.