Skip to content
Advertisement

Extending Promise base class typings

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?

Advertisement

Answer

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;
  }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement