typescript:angular.d.ts用ng.IPromise防御打字
typescript: angular.d.ts defently typed with ng.IPromise
我在尝试链接ng.IPromise函数时遇到了这个错误:
Error:(101, 23) TS2345: Argument of type 'IPromise<IResult[]>' is not assignable to parameter of type 'IResult[]'. Property 'length' is missing in type 'IPromise<IResult[]>'.
代码:
public search(stateParams : any, page : any) : ng.IPromise<Array<app.search.entities.IResult>> {
// create promise
let deferred : ng.IDeferred<Array<app.search.entities.IResult>> = this.$q.defer();
// ensure page is a number
page = (isNaN(page)) ? 1 : parseInt(page, 10);
// create query
this.searchServiceSearchQuery.getQuery(
stateParams,
this._itemsPerPage,
(page * this._itemsPerPage) - this._itemsPerPage,
null
).then(
(query : any) : void => {
// search
deferred.resolve(this._search(query)); // <= THIS "this" THROWS THE ERROR ABOVE
}
);
// return promise
return deferred.promise;
}
private _search(query : any) : ng.IPromise<Array<app.search.entities.IResult>> {
// create deferred
let deferred : ng.IDeferred<Array<app.search.entities.IResult>> = this.$q.defer();
// run query
this.coreServiceRestangularHelpmeSearch
.one('search')
.get({q : query})
.then(
(data : any) : void => {
// found results
if (
data !== null &&
'results' in data === true
) {
// create entities
let entities : Array<app.search.entities.IResult> = [];
// walk through all results
data.results.forEach((result : any) : void => {
// create entity
let entity : app.search.entities.IResult = new app.search.entities.Result(
result.id,
result.cat1Ids[0],
result.cat2Ids[0],
result.cat3Ids[0],
result.typeCode,
(result.sort) ? parseInt(result.sort, 10) : null,
result.title,
result.description,
this.$sce.trustAsHtml(result.summary),
false
);
entities.push(entity);
});
// sort all by sort key
entities.sort(
(
a : app.search.entities.IResult,
b : app.search.entities.IResult
) : number => {
if (a.getSortKey() > b.getSortKey()) {
return 1;
} else if (a.getSortKey() < b.getSortKey()) {
return -1;
}
return 1;
}
);
deferred.resolve(this._extendResults(entities));
}
// return empty object
deferred.reject('invalid api response');
},
() : any => {
deferred.reject('failed to fetch search results');
}
)
;
是我做错了什么,还是def-typed文件有问题?如果我将_search()
函数的泛型更改为any
,那么一切看起来都很好。
在解析方法内部,您应该传递类型为的变量
Array<app.search.entities.IResult>
非类型:
ng.IPromise<Array<app.search.entities.IResult>>.
如果你写这样的东西,你的代码将是正确的:
this._search(query).then((data)=>{
deferred.resolve(data);
});
而不是
deferred.resolve(this._search(query));
它会被编译,但我不知道你的代码逻辑,调整它本身。
相关文章:
- ng映射方向备选方案
- AngularJS UI路由器不能像ng路由器那样工作
- 角度:在ng重复上切换图像
- 正在使用$location.path(.)路由ng视图
- AngularJS:ng之后,重复$scope值未按预期更新
- ng打开空字符串
- 我应该使用Ng提交还是点击表格
- ng init中的表达式无法使用ng repeat
- ng更改事件不适用于Dropdown
- ng视图外的链接重定向到ng视图内的页面
- ng应用程序使脚本无限运行
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- ng隐藏和ng显示无法正常工作
- 从ng模板访问作用域
- 防止ng重复中的重复值(AngularJS)
- 从键值结构中获取数据,并将其与AngularJS中ng重复的值进行比较
- AngularJs列表ng单击以选择选项转换
- 如何使用AngularJs禁用ng选项中的选项
- ReferenceError: cordova is not defined @ng-cordova.min.js:7
- typescript:angular.d.ts用ng.IPromise防御打字