arsis-dev
arsis-dev

Reputation: 453

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:

<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 FormerGenericComponents

I want FormerConfigColumn.components to contain an array of objects, and said objects can either be Type<FormerComponent> or FormerGenericComponent.

How can I do this?

Thanks!

Upvotes: 1

Views: 938

Answers (1)

OSH
OSH

Reputation: 821

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.

If you want components to hold an array of FormerComponent or FormerGenericComponent, but not both at the same time, then use:

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

Upvotes: 5

Related Questions