簡體   English   中英

擴展 Promise 基礎 class 分型

[英]Extending Promise base class typings

我正在嘗試使用 static 方法和實例方法擴展 Promise 基礎 class。 我在使用 typescript 定義時遇到問題。 請參閱下面的代碼!

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...
};

使用此代碼,當我嘗試在上面定義Promise.timeout時,typescript 給了我錯誤: Property timeout is a static member of type PromiseConstructor

如果我嘗試在interface Promise塊內定義打字timeout() , typescript 會給我錯誤'static' modifier cannot appear on a type member

如何鍵入超時方法?

據我所知,您必須從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