【问题标题】:typescript: angular.d.ts defently typed with ng.IPromise打字稿:angular.d.ts 用 ng.IPromise 正确输入
【发布时间】:2016-03-11 07:35:37
【问题描述】:

我在尝试链接 ng.IPromise 函数调用时遇到此 ts 错误:

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 类型的文件有问题?如果我将_search() 函数的泛型更改为any,一切看起来都很好。

【问题讨论】:

  • 您的示例很长,示例的第 101 行有一个空行。我想这就是为什么没有人关注你的问题。只需在代码中标记错误发生的位置即可。

标签: javascript angularjs typescript typescript1.5


【解决方案1】:

在解析方法中你应该传递类型变量:

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));

会编译,但不知道你的代码逻辑,自己调吧。

【讨论】:

    猜你喜欢
    • 2021-07-27
    • 1970-01-01
    • 2022-08-12
    • 2019-03-27
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    相关资源
    最近更新 更多