【问题标题】:Typescript Mixin Dynamic InterfaceTypescript Mixin 动态接口
【发布时间】:2018-09-11 04:29:58
【问题描述】:
interface Base {
}

interface A {
    a: number
}

interface B {
    b: number;
}

interface C {
    c: number
}

interface AB extends A, B, Base {}
interface AC extends A, C, Base {}
interface BC extends B, C, Base {}
interface ABC extends A, B, C, Base {}

Base 可以将任何接口扩展为选择性。 我想知道将扩展接口作为其他语法。 因为可以增加可扩展类的数量。

interface Mix2<T, U> extends T, U, Base {
}
type AB = Mix2<A, B>
type BC = Mix2<B, C>

我知道,这是错误的语法,只是我所期望的示例。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    如果您只想要一个表示一个对象的类型,该对象必须具有其他两个已定义类型的成员,您可以使用交集类型

    type AB = A & B & Base
    type BC = B & C & Baee
    

    你可以阅读更多关于交叉口类型here

    【讨论】:

    • 按我的预期工作。只是我愚蠢的 IDE 不像 VSC 那样工作。
    猜你喜欢
    • 2020-08-22
    • 2020-11-24
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多