Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在TypeScript中继承PromiseLike接口?_Typescript_Promise - Fatal编程技术网

如何在TypeScript中继承PromiseLike接口?

如何在TypeScript中继承PromiseLike接口?,typescript,promise,Typescript,Promise,我想扩展PromiseLike接口 (我使用TypeScript v3.6.3) 我使用了原始的PromiseLike界面: interface PromiseLike<T> { then<TResult1 = T, TResult2 = never>( onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,

我想扩展PromiseLike接口

(我使用TypeScript v3.6.3)

我使用了原始的PromiseLike界面:

interface PromiseLike<T> {
    then<TResult1 = T, TResult2 = never>(
        onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
        onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null,
    ): PromiseLike<TResult1 | TResult2>
}
接口承诺{
然后(
OnCompleted?:((值:T)=>TResult1 | PromiseLike)|未定义| null,
onrejected?:((原因:任意)=>TResult2 |承诺式)|未定义|空,
):允诺
}
然后我只继承PromiseLike并将嵌套的PromiseLike类型替换为表:

interface Thenable<T = any> extends PromiseLike<T> {
    then<TResult1 = T, TResult2 = never>(
        onfulfilled?: ((value: T) => TResult1 | Thenable<TResult1>) | undefined | null,
        onrejected?: ((reason: any) => TResult2 | Thenable<TResult2>) | undefined | null,
    ): Thenable<TResult1 | TResult2>
}
接口表扩展PromiseLike{
然后(
oncompleted?:((值:T)=>TResult1 |表)|未定义|空,
onrejected?:((原因:任意)=>TResult2 |可选项)|未定义|空,
):表格
}
得到了这个错误:

Error:(28, 18) TS2430: Interface 'Thenable<T>' incorrectly extends interface 'PromiseLike<T>'.
  Types of property 'then' are incompatible.
    Type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | Thenable<TResult1>, onrejected?: (reason: any) => TResult2 | Thenable<TResult2>) => Thenable<TResult1 | TResult2>' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
      Types of parameters 'onrejected' and 'onrejected' are incompatible.
        Type 'TResult2 | PromiseLike<TResult2>' is not assignable to type 'TResult2 | Thenable<TResult2>'.
          Type 'PromiseLike<TResult2>' is not assignable to type 'TResult2 | Thenable<TResult2>'.
            Type 'PromiseLike<TResult2>' is not assignable to type 'Thenable<TResult2>'.
              Types of property 'then' are incompatible.
                Type '<TResult1 = TResult2, TResult2 = never>(onfulfilled?: (value: TResult2) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>' is not assignable to type '<TResult1 = TResult2, TResult2 = never>(onfulfilled?: (value: TResult2) => TResult1 | Thenable<TResult1>, onrejected?: (reason: any) => TResult2 | Thenable<TResult2>) => Thenable<TResult1 | TResult2>'.
                  Types of parameters 'onrejected' and 'onrejected' are incompatible.
                    Type 'TResult2 | Thenable<TResult2>' is not assignable to type 'TResult2 | PromiseLike<TResult2>'.
                      Type 'Thenable<TResult2>' is not assignable to type 'TResult2 | PromiseLike<TResult2>'.
                        Type 'Thenable<TResult2>' is not assignable to type 'PromiseLike<TResult2>'.
                          Types of property 'then' are incompatible.
                            Type '<TResult1 = TResult2, TResult2 = never>(onfulfilled?: (value: TResult2) => TResult1 | Thenable<TResult1>, onrejected?: (reason: any) => TResult2 | Thenable<TResult2>) => Thenable<TResult1 | TResult2>' is not assignable to type '<TResult1 = TResult2, TResult2 = never>(onfulfilled?: (value: TResult2) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => PromiseLike<TResult1 | TResult2>'.
                              Types of parameters 'onrejected' and 'onrejected' are incompatible.
                                Type 'TResult2 | PromiseLike<TResult2>' is not assignable to type 'TResult2 | Thenable<TResult2>'.
                                  Type 'PromiseLike<TResult2>' is not assignable to type 'TResult2 | Thenable<TResult2>'.
错误:(28,18)TS2430:接口“Thenable”错误地扩展了接口“PromiseLike”。
“then”属性的类型不兼容。
类型’(OnCompleted?:(值:T)=>TResult1 |表,onrejected?:(原因:任意)=>TResult2 |表)=>TheName’不可分配给类型’(OnCompleted?:(值:T)=>TResult1 | PromiseLike,onrejected?:(原因:任意)=>TResult2 | PromiseLike)=>PromiseLike’。
参数“onrejected”和“onrejected”的类型不兼容。
类型“TResult2 | PromiseLike”不可分配给类型“TResult2 | Thenable”。
类型“PromiseLike”不可分配给类型“TResult2 | Thenable”。
类型“PromiseLike”不可分配给类型“Thenable”。
“then”属性的类型不兼容。
类型'(OnCompleted?:(值:TResult2)=>TResult1 | PromiseLike,onrejected?:(原因:任意)=>TResult2 | PromiseLike)=>PromiseLike'不可分配给类型'(OnCompleted?:(值:TResult2)=>TResult1 | Thenable,onrejected?:(原因:任意)=>TResult2 | Thenable)=>Thenable'。
参数“onrejected”和“onrejected”的类型不兼容。
类型“TResult2 | Thenable”不可分配给类型“TResult2 | PromiseLike”。
类型“Thenable”不可分配给类型“TResult2 | PromiseLike”。
类型“Thenable”不可分配给类型“PromiseLike”。
“then”属性的类型不兼容。
类型'(OnCompleted?:(值:TResult2)=>TResult1 |表,onrejected?:(原因:任意)=>TResult2 |表)=>Thenable'不可分配给类型'(OnCompleted?:(值:TResult2)=>TResult1 | PromiseLike,onrejected?:(原因:任意)=>TResult2 | PromiseLike)=>PromiseLike'。
参数“onrejected”和“onrejected”的类型不兼容。
类型“TResult2 | PromiseLike”不可分配给类型“TResult2 | Thenable”。
类型“PromiseLike”不可分配给类型“TResult2 | Thenable”。
如何修复此错误

此外,我希望扩展此接口,如下所示:

export interface Thenable<T = any> extends PromiseLike<T> {
    then<TResult1 = T, TResult2 = never>(
        onfulfilled?: ((value: T) => TResult1 | ThenableOrIteratorOrValue<TResult1>) | undefined | null,
        onrejected?: ((reason: any) => TResult2 | ThenableOrIteratorOrValue<TResult2>) | undefined | null,
    ): ThenableOrIteratorOrValue<TResult1 | TResult2>
}
导出接口表格扩展PromiseLike{
然后(
OnCompleted?:((值:T)=>TResult1 | EnableOriTeratorOverValue)|未定义| null,
onrejected?:((原因:任意)=>TResult2 | EnableOriTeratorOverValue)|未定义|空,
):制表符或右值
}

我希望它与PromiseLike保持兼容,以便异步等待函数与之兼容。

与PromiseLike保持兼容意味着您的
表可以作为参数传递给期望
PromiseLike
的代码,并且该代码仍然可以工作。要使其正常工作,
then
应该接受
oncompleted
回调,该回调返回
PromiseLike
,而不是
Thenable
——因为这是使用
PromiseLike
的代码将传递给
then
的实现的回调类型

因此,将
PromiseLike
添加到回调可以返回的类型的联合中,这样可以消除错误:

    onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
oncompleted:((值:T)=>TResult1 | PromiseLike)|未定义| null,

出于我不理解的原因,
onrejected
是兼容的,显然是因为它的类型不依赖于泛型参数
t

您不能扩展接口和覆盖属性。请查看为什么要扩展PromiseLike接口?你想达到什么目标?ThanksI使用generator实现了异步函数的同步版本(yield代替wait)。我使用迭代器类型作为同步承诺。现在我想把所有承诺式的接口结合起来。一个
union
类型会有帮助吗?我不知道您的
标签或右值的代码。也许以下内容可以帮助您:
键入promiselikeorthanble=PromiseLike | Thenable | IteratorResult