我想知道我为什么要接受
Type 'typeof TestBody' does not satisfy the constraint 'typeof AbstractBodyWithTabs'.
Construct signature return types 'TestBody' and 'AbstractBodyWithTabs<T, U>' are incompatible.
The types of 'contents' are incompatible between these types.
Type 'TestContent' is not assignable to type 'T'.
'TestContent' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'AbstractSelector'.
class TestPage extends PageWithTabs<typeof TestBody> {"T“约束是正确类型的扩展类。
export class AbstractSelector {
protected readonly myRoot : Selector
constructor(inSelector : Selector) {
this.myRoot = inSelector;
}
}
export abstract class AbstractBodyWithTabs<
T extends AbstractSelector,
U extends Tabs
> {
abstract get contents () : T
abstract get tabs () : U
get wrapper () : Selector {
return Selector(".app-component");
}
}
export abstract class Tabs extends AbstractSelector {
get active () {
return this.wrapper.find(":scope > button[contains(@class, 'focus')]");
}
get all () {
return this.wrapper.find(":scope > button");
}
abstract get named_tab () : Record<string, Selector>;
get wrapper () {
return this.myRoot.find("div.btn-group");
}
}
export abstract class PageWithTabs<V extends typeof AbstractBodyWithTabs> {
abstract get body () : InstanceType<V>
get footer () {
return new SaveAndCancelFooter();
}
get header () {
return new Header();
}
}
class TestContent extends AbstractSelector {
}
class TestTabs extends Tabs {
get named_tab () {
return { get country () { return Selector("div"); } };
}
}
class TestBody extends AbstractBodyWithTabs<TestContent, TestTabs> {
get contents () {
return new TestContent(this.wrapper);
}
get tabs () {
return new TestTabs(this.wrapper);
}
}
class TestPage extends PageWithTabs<typeof TestBody> {
get body () {
return new TestBody();
}
}当我做出这些改变时:
export abstract class PageWithTabs<V extends typeof AbstractBodyWithTabs> {
abstract get body () : InstanceType<V>
get footer () {
return new SaveAndCancelFooter();
}
get header () {
return new Header();
}
}至
export abstract class PageWithTabs<T extends AbstractSelector, U extends Tabs, V extends AbstractBodyWithTabs<T, U>> {
abstract get body () : V
get footer () {
return new SaveAndCancelFooter();
}
get header () {
return new Header();
}
}class TestPage extends PageWithTabs<typeof TestBody> {
get body () {
return new TestBody();
}
}至
class TestPage extends PageWithTabs<TestContent, TestTabs, TestBody> {
get body () {
return new TestBody();
}
}它可以工作,但我不喜欢如何传递已经声明的多个类型。
打字本游乐场
发布于 2021-10-10 13:48:25
在重新审视了cherryblossom的答案之后,在这种情况下,似乎不需要所有的泛型。
重写:
class Selector {
constructor (inValue : string) {
console.log(inValue);
}
}
export class AbstractSelector {
protected readonly myRoot : Selector
constructor(inSelector : Selector) {
this.myRoot = inSelector;
}
}
export abstract class AbstractBodyWithTabs {
abstract get contents () : AbstractSelector
abstract get tabs () : Tabs
get wrapper () : Selector {
return new Selector(".app-component");
}
}
export abstract class Tabs extends AbstractSelector {
get active () {
return;
}
get all () {
return;
}
abstract get named_tab () : Record<string, Selector>;
get wrapper () {
return;
}
}
export abstract class PageWithTabs {
abstract get body () : AbstractBodyWithTabs
get footer () {
return;
}
get header () {
return;
}
}
class TestContent extends AbstractSelector {
get has_content () {
return "has ocntent";
}
}
class TestTabs extends Tabs {
get named_tab () {
return { get country () { return new Selector("div"); } };
}
}
class TestBody extends AbstractBodyWithTabs {
get contents () {
return new TestContent(this.wrapper);
}
get tabs () {
return new TestTabs(this.wrapper);
}
}
class TestPage extends PageWithTabs {
get body () {
return new TestBody();
}
}https://stackoverflow.com/questions/69493891
复制相似问题