扩展新文件中的别名



我正在与typescriptangular 1.5一起工作,并且有一个有趣的情况。

angular.d.ts 中,来自 DefinitelyTyped,angular实际上声明了一个名为 ng别名,所以它是这样使用的;

class SomeController {
   constructor($scope: ng.IScope) { }
}

现在,我想扩展IScope并添加我自己的属性以实现类型安全。不过,我对如何做到这一点有点困惑,因为以下内容不起作用......

declare module ng { 
   interface IRegisterScope extends ng.IScope {
      // ...
   }
}

我真的很困惑如何开始做这件事。我正在使用 es5 标准导入角度,我的模块根据我的tsconfig.json文件编译为 amd

所以这就是我的文件的样子,到目前为止...

import "jquery";
import "angular";
class RegisterController {
   constructor($scope: ng.IScope) {
      // ...
   }
}
export = RegisterController;

我想扩展 ng 以添加一个新接口,IRegisterScope .

您应该能够简单地将以下内容放在RegisterController类的上方:

interface IRegisterScope extends ng.IScope {
}

然后在RegisterController的构造函数中:

constructor($scope: IRegisterScope) {
}

这样,您就不会导出新的作用域接口来污染其他文件,并且仍然可以在此文件中获得智能感知的好处。

如果您希望它在其他文件中可用,只需将其放在其他文件中并在您要使用的文件中引用它即可。

///<reference path="some_path_to_other_file/scopes.d.ts" />

更新

如果您希望IRegisterScopeng 下可用,您可以这样做。

scopes.d.ts:

declare module angular {
    interface IRegisterScope extends ng.IScope {
        someProperty: string;
    }
}

应用:

///<reference path="typings/angularjs/angular.d.ts" />
///<reference path="scopes.d.ts" />
import "jquery";
import "angular";
class RegisterController {
   constructor($scope: ng.IRegisterScope) {
   }
}
export = RegisterController;

最新更新