2

I am trying to extend Promise base class, with a static method and an instance method. I am having trouble with the typescript definitions. See my code below!

declare global {
    class PromiseConstructor {
        static timeout(): null;
    }
    interface Promise<T> {
        timeout(): null
        finally<T>(f: () => void): Promise<T>,
    }

}

Promise.timeout = (n: number) => {
  // code...
}

Promise.prototype.finally = function (onFinally) {
  // code...
};

With this code, when I try to defined Promise.timeout above, typescript gives me the error : Property timeout is a static member of type PromiseConstructor.

If I try to define the typing timeout() inside the interface Promise block, typescript gives me the error 'static' modifier cannot appear on a type member.

How can I type the timeout method?

yachaka
  • 5,429
  • 1
  • 23
  • 35

1 Answers1

2

As I know that you would have to extend from interface PromiseConstructor instead of a class PromiseConstructor.

declare global {
  interface PromiseConstructor {
    timeout(n: number): any;
  }
}
tmhao2005
  • 14,776
  • 2
  • 37
  • 44