打字稿中的多态承诺
Polymorphic Promise in Typescript
我正在尝试在 Typescript class 中参数化一个函数,returns 一个 Promise。完成承诺后,我将返回 this
,它由调用者以多态方式使用。我遇到了一个我不太明白的编译时错误。
此(平凡化)代码编译良好:
class foo {
aFoo(): Promise<foo> {
return new Promise<foo>(resolve => resolve(this));
}
}
class bar extends foo {
test() {
this.aFoo().then(result => {
let val: bar;
val = result as bar;
});
}
}
但是,我宁愿不必低估结果,即。 val = result as bar
每次我调用它,所以我试图在 superclass:
中参数化函数
class foo {
aFoo<T extends foo>(): Promise<T> {
return new Promise<T>(resolve => resolve(this));
}
}
class bar extends foo {
test() {
this.aFoo<bar>().then(result => {
let val: bar;
val = result;
});
}
}
我在从 aFoo 返回的承诺 resolve(this)
上收到编译器错误。
错误说:
this: this
Argument of type 'this' is not assignable to parameter of type 'T | PromiseLike<T> | undefined'.
Type 'foo' is not assignable to type 'T | PromiseLike<T> | undefined'.
Type 'foo' is not assignable to type 'PromiseLike<T>'.
Type 'this' is not assignable to type 'PromiseLike<T>'.
Property 'then' is missing in type 'foo' but required in type 'PromiseLike<T>'.ts(2345)
lib.es5.d.ts(1393, 5): 'then' is declared here.
我可以通过一些无关的转换来抑制编译器错误:
return new Promise<foo>(resolve => resolve((this as unknown) as T));
我可以使用变通方法,但我想了解编译器反对什么。我认为这可能与 JS/TS 中的 this 的怪异有关,但是将 this 更改为箭头函数并没有消除错误。这个错误也让我感到奇怪,因为它将 this 描述为一种类型,而不是一个实例——但我确实看到 this 可以在 TS 的类型上下文中使用。知道我做错了什么吗?
TypeScript 对此有 polymorphic this 类型。
您可以使用 this
作为类型,例如声明具有 Promise<this>
类型的东西,并且它按预期工作:
class foo {
aFoo(): Promise<this> {
return new Promise<this>(resolve => resolve(this));
}
}
class bar extends foo {
test() {
this.aFoo().then(result => {
let val: bar;
val = result;// OK, this is bar here;
});
}
}
我正在尝试在 Typescript class 中参数化一个函数,returns 一个 Promise。完成承诺后,我将返回 this
,它由调用者以多态方式使用。我遇到了一个我不太明白的编译时错误。
此(平凡化)代码编译良好:
class foo {
aFoo(): Promise<foo> {
return new Promise<foo>(resolve => resolve(this));
}
}
class bar extends foo {
test() {
this.aFoo().then(result => {
let val: bar;
val = result as bar;
});
}
}
但是,我宁愿不必低估结果,即。 val = result as bar
每次我调用它,所以我试图在 superclass:
class foo {
aFoo<T extends foo>(): Promise<T> {
return new Promise<T>(resolve => resolve(this));
}
}
class bar extends foo {
test() {
this.aFoo<bar>().then(result => {
let val: bar;
val = result;
});
}
}
我在从 aFoo 返回的承诺 resolve(this)
上收到编译器错误。
错误说:
this: this
Argument of type 'this' is not assignable to parameter of type 'T | PromiseLike<T> | undefined'.
Type 'foo' is not assignable to type 'T | PromiseLike<T> | undefined'.
Type 'foo' is not assignable to type 'PromiseLike<T>'.
Type 'this' is not assignable to type 'PromiseLike<T>'.
Property 'then' is missing in type 'foo' but required in type 'PromiseLike<T>'.ts(2345)
lib.es5.d.ts(1393, 5): 'then' is declared here.
我可以通过一些无关的转换来抑制编译器错误:
return new Promise<foo>(resolve => resolve((this as unknown) as T));
我可以使用变通方法,但我想了解编译器反对什么。我认为这可能与 JS/TS 中的 this 的怪异有关,但是将 this 更改为箭头函数并没有消除错误。这个错误也让我感到奇怪,因为它将 this 描述为一种类型,而不是一个实例——但我确实看到 this 可以在 TS 的类型上下文中使用。知道我做错了什么吗?
TypeScript 对此有 polymorphic this 类型。
您可以使用 this
作为类型,例如声明具有 Promise<this>
类型的东西,并且它按预期工作:
class foo {
aFoo(): Promise<this> {
return new Promise<this>(resolve => resolve(this));
}
}
class bar extends foo {
test() {
this.aFoo().then(result => {
let val: bar;
val = result;// OK, this is bar here;
});
}
}