0

I have a fn that inherit an existing fn ( take Angular1 $q for example )

//$q original behavior
var defer = $q.defer();
defer.promise.then(function(result){})
//or
$q( (resolve, reject) => {
   //promise execution here
}).then(function(result){});

If I want to decorate it, I would do :

var Qdecorator = function($delegate) {

     var Q = function(resolver:any): any {
       //do some extra stuff here
       return $delegate.apply($delegate, arguments);
     }

     //Assign the static methods here: 

     Q.defer = function() {
        //do some stuff 
        return $delegate.defer.apply($delegate, []);
     }
     //same goes for race, when, resole reject and so on
     return Q;
}

Problem is that typescript complains about Property defer, race, when, resolve, etc... does not exist on type '(resolver: any) => any'

I tried to use the IQService, and IPromise with no luck, btu I'd like to raise a more global question :

How do I define late static methods on function() that return an object without using new

Ant
  • 1,812
  • 5
  • 22
  • 39

1 Answers1

0

I am copying pasting the answer to my question from this link: https://www.typescriptlang.org/docs/handbook/interfaces.html

interface Counter {
  (start: number): string;
  interval: number;
  reset(): void;
}

function getCounter(): Counter {
  let counter = <Counter>function (start: number) { };
  counter.interval = 123;
  counter.reset = function () { };
  return counter;
}

let c = getCounter();
c(10);
c.reset();
c.interval = 5.0;
Ant
  • 1,812
  • 5
  • 22
  • 39