, {width: 5, height: 5, type: 'colorBox'} logBox, Box. - Box, , type, - .
Box, disjoint union. Box BaseBox Box, . .
type BaseBox = {
width: number,
height: number,
}
type PositionedBox = BaseBox & {
type: 'positionedBox',
left: number,
top: number,
}
type ColorBox = BaseBox & {
type: 'colorBox',
color: string,
}
type Box = PositionedBox | ColorBox;
const logSize = (obj: Box) => {
console.log({ w: obj.width, h: obj.height });
};
const logPosition = (obj: PositionedBox) => {
console.log({ l: obj.left, t: obj.top });
};
const logColor = (obj: ColorBox) => {
console.log({color: obj.color});
};
const logBox = (obj: Box) => {
logBox(obj);
if (obj.type === 'colorBox') logColor(obj);
if (obj.type === 'positionedBox') logPosition(obj);
}