typescript - 在新文件中扩展ALIAS

时间:2016-02-15 21:51:57

标签: typescript

我正在使用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

1 个答案:

答案 0 :(得分:0)

您应该只需将以下内容放在src: 'views/**/**/*.html', 课程上方:

RegisterController

然后在interface IRegisterScope extends ng.IScope { } 的构造函数中:

RegisterController

这样您就不会导出新的示波器界面来污染其他文件,仍然可以在此文件中获得IntelliSense的好处。

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

constructor($scope: IRegisterScope) {
}

<强>更新

如果您希望///<reference path="some_path_to_other_file/scopes.d.ts" /> 下的IRegisterScope可用,则可以执行此操作。

scopes.d.ts:

ng

app.ts:

declare module angular {
    interface IRegisterScope extends ng.IScope {
        someProperty: string;
    }
}
相关问题