I am struggling a bit with union types and am wondering if typescript can infer the value without the extra checks all over the place. Lets say I have this interface setup (left out IBaseDiscount for brevity) where the values can be different but the nested discount type names are fixed
interface IFlatDiscount extends IBaseDiscount {
value:{ formatted: string; value: number };
discountType: {
name: DiscountType.flat;
id: number;
};
}
interface IOpenDiscount extends IBaseDiscount {
value?: number;
discountType: {
name: DiscountType.open;
id: number;
};
}
export interface IPercentageDiscount extends IBaseDiscount {
value: number;
discountType: {
name: DiscountType.percentage;
id: number;
};
}
export type IDiscount = IOpenDiscount | IPercentageDiscount | IFlatDiscount;
Now in my code when I am trying use these values I end up having to do stuff like the following
if (discount.discountType.name === DiscountType.flat && typeof discount.value === 'object) {
// now my value is properly typed -- if I leave out the object check it doesnt know the correct type for the value
}
Is there a proper way for typescript to infer the value based off the discountType.name instead of having all the checks on the value everywhere?