I have the following typescript errors in my project.. let me share a sample so you can see what am dealing with.
JavaScript
x
92
92
1
module CoreWeb {
2
export class Controller implements IController {
3
public $q;
4
public $rootScope;
5
public $scope:ng.IScope;
6
public $state:ng.ui.IStateService;
7
public $translate:ng.translate.ITranslateService;
8
public appEvents;
9
public commonValidationsService;
10
public defaultPagingOptions = {
11
currentPage: 1,
12
pageSize: 10,
13
totalServerItems: 0,
14
maxSize: 5
15
};
16
public detailModelName:string;
17
public filter:string;
18
public listModelName:string;
19
public mode;
20
public modelDataService;
21
public modelDefaultProperty:string;
22
public ngDialog;
23
public notificationsService;
24
public pagingOptions:IPagingOptions;
25
public selectionStatus:boolean;
26
public serviceCreateFunction:string;
27
public serviceGetAllCanceller:ng.IDeferred<any>;
28
public serviceGetAllFunction:string;
29
public serviceGetOneFunction:string;
30
public serviceUpdateFunction:string;
31
public showInactive:boolean;
32
public tableAction:number;
33
public tableActions:ITableAction[];
34
public titleDataFactory;
35
public validationOptions;
36
public validationRules;
37
public orderBy = null;
38
public orderType = null;
39
constructor(
40
$q:ng.IQService,
41
$rootScope,
42
$scope:ng.IScope,
43
$state,
44
$translate:ng.translate.ITranslateService,
45
appEvents,
46
commonValidationsService,
47
detailModelName:string,
48
listModelName:string,
49
modelDataService,
50
modelDefaultProperty:string,
51
ngDialog,
52
notificationsService,
53
serviceCreateFunction:string,
54
serviceGetAllFunction:string,
55
serviceGetOneFunction:string,
56
serviceUpdateFunction:string,
57
titleDataFactory
58
) {
59
this.$q = $q;
60
this.$rootScope = $rootScope;
61
this.$scope = $scope;
62
this.$state = $state;
63
this.$translate = $translate;
64
this.appEvents = appEvents;
65
this.commonValidationsService = commonValidationsService;
66
this.detailModelName = detailModelName;
67
this.listModelName = listModelName;
68
this.modelDataService = modelDataService;
69
this.modelDefaultProperty = modelDefaultProperty;
70
this.ngDialog = ngDialog;
71
this.notificationsService = notificationsService;
72
this.serviceCreateFunction = serviceCreateFunction;
73
this.serviceGetAllCanceller = $q.defer();
74
this.serviceGetAllFunction = serviceGetAllFunction;
75
this.serviceGetOneFunction = serviceGetOneFunction;
76
this.serviceUpdateFunction = serviceUpdateFunction;
77
this.titleDataFactory = titleDataFactory;
78
79
this.mode = $rootScope.modeEnum.none;
80
this.pagingOptions = this.defaultPagingOptions;
81
this.selectionStatus = false;
82
this.showInactive = false;
83
this.tableAction = null;
84
this.tableActions = [
85
{id: 1, name: "Activate"},
86
{id: 2, name: "Deactivate"}
87
];
88
this.validationOptions = {showErrors: commonValidationsService.modes.property, showNotification: true};
89
90
this.activate();
91
}
92
This is the class that extends the controller class.. one among many others
JavaScript
1
46
46
1
declare var App: ng.IModule;
2
3
module CoreWeb {
4
export class EntityMasterController extends Controller {
5
private currenciesDataSet;
6
private entity: IEntityMasterModel;
7
private merchandisingConstants;
8
private typeAheadOptions;
9
10
constructor(
11
$q:ng.IQService,
12
$rootScope,
13
$scope:ng.IScope,
14
$state,
15
$translate:ng.translate.ITranslateService,
16
appEvents,
17
commonValidationsService,
18
entityDataService,
19
merchandisingConstants,
20
ngDialog,
21
notificationsService,
22
titleDataFactory
23
) {
24
this.merchandisingConstants = merchandisingConstants;
25
super(
26
$q,
27
$rootScope,
28
$scope,
29
$state,
30
$translate,
31
appEvents,
32
commonValidationsService,
33
"entity",
34
null,
35
entityDataService,
36
"name",
37
ngDialog,
38
notificationsService,
39
"createEntity",
40
"getCurrentEntity",
41
"getEntity",
42
"updateEntity",
43
titleDataFactory
44
);
45
}
46
Now, if I initialize the merchandisingConstants
before the super call like done above. I get the following error during gulp and my page does not display anything. A super
call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. I have tried all ways I can think of to fix these errors any idea of how I can go about this?
Advertisement
Answer
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:
JavaScript
1
48
48
1
declare var App: ng.IModule;
2
3
module CoreWeb {
4
export class EntityMasterController extends Controller {
5
private currenciesDataSet;
6
private entity: IEntityMasterModel;
7
private merchandisingConstants;
8
private typeAheadOptions;
9
10
constructor(
11
$q:ng.IQService,
12
$rootScope,
13
$scope:ng.IScope,
14
$state,
15
$translate:ng.translate.ITranslateService,
16
appEvents,
17
commonValidationsService,
18
entityDataService,
19
merchandisingConstants,
20
ngDialog,
21
notificationsService,
22
titleDataFactory
23
) {
24
// Must be first
25
super(
26
$q,
27
$rootScope,
28
$scope,
29
$state,
30
$translate,
31
appEvents,
32
commonValidationsService,
33
"entity",
34
null,
35
entityDataService,
36
"name",
37
ngDialog,
38
notificationsService,
39
"createEntity",
40
"getCurrentEntity",
41
"getEntity",
42
"updateEntity",
43
titleDataFactory
44
);
45
46
this.merchandisingConstants = merchandisingConstants;
47
}
48