简体   繁体   English

扩展 Promise 基础 class 分型

[英]Extending Promise base class typings

I am trying to extend Promise base class, with a static method and an instance method.我正在尝试使用 static 方法和实例方法扩展 Promise 基础 class。 I am having trouble with the typescript definitions.我在使用 typescript 定义时遇到问题。 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 .使用此代码,当我尝试在上面定义Promise.timeout时,typescript 给了我错误: 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 .如果我尝试在interface Promise块内定义打字timeout() , typescript 会给我错误'static' modifier cannot appear on a type member

How can I type the timeout method?如何键入超时方法?

As I know that you would have to extend from interface PromiseConstructor instead of a class PromiseConstructor .据我所知,您必须从interface PromiseConstructor而不是class PromiseConstructor

declare global {
  interface PromiseConstructor {
    timeout(n: number): any;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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