I have a project that uses AngularJS and want to make a wrapper above the $http
service. I want my request promises to have abort()
method.
If I write
function request(params:ng.IRequestConfig):my.IRequestPromise {
var promise = $http(params).then(onSuccess, onError);
promise.abort = function () { // => here it fails with error "Property 'abort' does not exist on type 'IPromise<{}>'"
...
}
return promise;
}
it fails on the line with abort
definition.
I have written an interface IRequestPromise
which extends ng.IPromise
by adding an abort()
method. How can I make TypeScript consider promise
as my.IRequestPromise
but not ng.IPromise
?
You could cast the type ( ng.IPromise
) of promise returned to your type ( my.IRequestPromise
extended from ng.IPromise
):
ie
var promise = <my.IRequestPromise<yourreturntype>>$http(params).then(onSuccess, onError);
Assuming your extension is something like this:
export interface IRequestPromise<T> extends ng.IPromise<T> {
...
}
Also make sure your function return type is also property typed, ie
function request(params:ng.IRequestConfig):my.IRequestPromise<anyOrSpecificTypeResolvedByPromise> {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.