thomasmost
thomasmost

Reputation: 1070

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

Upvotes: 0

Views: 56

Answers (1)

Estus Flask
Estus Flask

Reputation: 222503

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() { ... };

Upvotes: 1

Related Questions