首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的类型不满足约束?

为什么我的类型不满足约束?
EN

Stack Overflow用户
提问于 2021-10-08 09:48:47
回答 1查看 2.8K关注 0票数 0

我想知道我为什么要接受

代码语言:javascript
复制
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“约束是正确类型的扩展类。

代码语言:javascript
复制
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();
    }
}

当我做出这些改变时:

代码语言:javascript
复制
export abstract class PageWithTabs<V extends typeof AbstractBodyWithTabs> {
    abstract get body () : InstanceType<V>
    get footer () {
        return new SaveAndCancelFooter();
    }
    get header () {
        return new Header();
    }
}

代码语言:javascript
复制
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();
    }
}
代码语言:javascript
复制
class TestPage extends PageWithTabs<typeof TestBody> {
    get body () {
        return new TestBody();
    }
}

代码语言:javascript
复制
class TestPage extends PageWithTabs<TestContent, TestTabs, TestBody> {
    get body () {
        return new TestBody();
    }
}

它可以工作,但我不喜欢如何传递已经声明的多个类型。

打字本游乐场

EN

回答 1

Stack Overflow用户

发布于 2021-10-10 13:48:25

在重新审视了cherryblossom的答案之后,在这种情况下,似乎不需要所有的泛型。

重写:

代码语言:javascript
复制
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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69493891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档