[英]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
的可选属性,该属性包含一个可以用FormerComponent
或FormerGenericComponent
填充的数组。
If you want components
to hold an array of FormerComponent
or FormerGenericComponent
, but not both at the same time, then use:如果您希望
components
保存一个数组FormerComponent
或FormerGenericComponent
,但不能同时保存两者,那么使用:
components?: (FormerComponent[] | FormerGenericComponent[]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.