Given array of inconsistent types. This may be used to dynamically rendering of html elements for example.
interface IElement {
type: 'button' | 'input'
content: Button & Input
}
interface Button {
text?: string;
backgroundColor?: string;
}
interface Input {
value?: string;
placeholder?: string;
}
const elements: IElement[] = [
{
type: 'button',
content: {
text: 'Start',
backgroundColor: 'salmon'
}
},
{
type: 'input',
content: {
value: 'phone',
placeholder: 'Phone'
}
}
]
const newElement = elements.map(element => element.content.backgroundColor)
Is there any other ways to typecast it properly depends on type property without union?