This is the approach that proved effective for me:
Firstly, in a separate file, I created a module called app
:
// IRootScopeService.d.ts
declare module app {
interface IRootScopeService extends ng.IRootScopeService {
//
$state: ng.ui.IState
//
previousState: any;
currentState: any;
}
}
I named this file as IRootScopeService.d.ts
because it contains declarations using the declare
keyword. This file is meant to be exclusively for accessing the interface
with app.IRootScopeService
.
Next, within the controller file, here's how the controller
function looks:
//CoreCtrl-ctrl.ts
...
function CoreCtrl(
$state: angular.ui.IState,
$rootScope: app.IRootScopeService
) {
var vm = this;
$rootScope.previousState = undefined;
$rootScope.currentState = undefined;
$rootScope.$on('$stateChangeSuccess', function(ev, to, toParams, from, fromParams) {
$rootScope.previousState = from.name;
$rootScope.currentState = to.name;
})
// $rootScope.previousState = undefined;
// $rootScope.currentState = undefined;
}
...
Take note of the app.IRootScopeService
that provides us with the desired type
for $rootScope
. With this setup, $rootScope.currentState
and $rootScope.previousState
will no longer cause errors in typescript
.
PART 2
If we want to include more interfaces
in the app
module, we can create a new file called IScope.d.ts
for better modularity:
// IScope.d.ts
declare module app {
interface IScope extends ng.IScope {
//
$root: IRootScopeService;
//
greet:string;
name:string;
address:string;
}
}
Now, we have two customized interfaces, app.IRootScopeService
and app.IState
, where we can continuously add new properties
we wish to incorporate into $rootScope
and $scope
.
It's worth mentioning that we did not use the ng.
prefix in $root: IRootScopeService;
since we are accessing app.IRootScopeService
from within the module app
.
I hope this explanation proves beneficial. Best of luck!