A union type describes a value that can be one of several types.
I want to define type union of A and B types. As far as I understood from docs the resulting type should be the type that represents A or B type. Surprisingly I'm able to have both paramA
and paramB
in UnitedAB
type. Why does this happen and how do I create the type that will return type A or type B
result?
type A = {
paramA: any;
};
type B = {
paramB: any;
};
type UnitedAB = A | B;
const a: UnitedAB = { paramA: 1, paramB: 2 }; //OK (WHY?)
const b: UnitedAB = { paramA: 1}; //ok
const c: UnitedAB = { param12: 1}; //error