简体   繁体   中英

How do you extend the class of an object's property in Typescript?

Working on an Angular 1 application, and trying to extend my promises with an 'abort' function. When I try to add abort to deferred.promise I get an abort does not exist on type IPromise<{}> error, obviously.'

How do I tell the property of this object to be a new class inline so that I can do this?

  deferred.promise.abort = function() {
     deferred.resolve();
  };

It can be

interface IAbortablePromise<T> extends ng.IPromise<T> {
    abort: () => void;
}

(deferred.promise as IAbortablePromise<any>).abort = function() { ... };

Or better,

interface IAbortableDeferred<T> extends ng.IDeferred<T> {
    promise: IAbortablePromise<T>;
}

const deferred = <IAbortableDeferred<any>>$q.defer();
deferred.promise.abort = function() { ... };

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM