简体   繁体   English

具有多种可能的 object 类型的接口类型数组

[英]Interface typing array with multiple possible object types

I have an interface that contains a property which is an array containing 2 different possible object types:我有一个接口,其中包含一个属性,该属性是一个包含 2 种不同可能 object 类型的数组:

<Type>FormerComponent and FormerGenericComponent

Here are my interfaces这是我的界面

interface FormerConfigColumn {
    container: ViewContainerRef
    components?: Type<FormerComponent>[]|FormerGenericComponent[];
}

interface FormerGenericComponent {
    component: Type<FormerComponent>;
}

The above doesn't work the way I would like it to.上面的方法不像我想要的那样工作。 I think the above defines an array containing only Type<FormerComponent> s or only FormerGenericComponent s我认为上面定义了一个仅包含Type<FormerComponent>或仅FormerGenericComponent的数组

I want FormerConfigColumn.components to contain an array of objects, and said objects can either be Type<FormerComponent> or FormerGenericComponent .我希望FormerConfigColumn.components 包含一个对象数组,并且所述对象可以Type<FormerComponent>FormerGenericComponent

How can I do this?我怎样才能做到这一点?

Thanks!谢谢!

interface FormerConfigColumn {
    container: ViewContainerRef
    components?: (FormerComponent | FormerGenericComponent)[];
}

This will define components as an optional property of FormerConfigColumn that holds an array which can be filled with FormerComponent or FormerGenericComponent .这会将components定义为FormerConfigColumn的可选属性,该属性包含一个可以用FormerComponentFormerGenericComponent填充的数组。

If you want components to hold an array of FormerComponent or FormerGenericComponent , but not both at the same time, then use:如果您希望components保存一个数组FormerComponentFormerGenericComponent ,但不能同时保存两者,那么使用:

    components?: (FormerComponent[] | FormerGenericComponent[]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM