2021-06-29

Typescript union types working like intersection types

I am having trouble distinguishing the difference between union and intersection types in TS even tho the difference should be very clear. Take this example:

   type CustomType1 = {
      customProp1: number;
    };
    
    type CustomType2 = {
      customProp2: number;
    };
    
    type UnionType = CustomType1 | CustomType2;
    
    const e1:  UnionType = {
      customProp1: 4,
      customProp2: 5,
    };

Note on how I created a union type which in theory should make a object that can only have one or the other type. This is not the case because the compiler nor my ide display any errors even tho the object I define has both of the properties from the two types which should be possible only after you merge the two types with the intersection & symbol. Is this the case?



from Recent Questions - Stack Overflow https://ift.tt/3A5qMgB
https://ift.tt/eA8V8J

No comments:

Post a Comment