gpt4 book ai didi

javascript - 扩展 Promise 基类类型

转载 作者:行者123 更新时间:2023-12-04 08:12:24 25 4
gpt4 key购买 nike

我正在尝试使用静态方法和实例方法扩展 Promise 基类。
我在 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 .
如果我尝试定义类型 timeout()interface Promise块, typescript 给了我错误 'static' modifier cannot appear on a type member .
如何输入超时方法?

最佳答案

据我所知,您必须从 interface PromiseConstructor 扩展而不是 class PromiseConstructor .

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

关于javascript - 扩展 Promise 基类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65887976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com