DEV Community

Discussion on: The meaning of union and intersection types

miloszpp profile image
Milosz Piechocki Author


Union would indeed contain all objects from both sets. However, the only thing we can know for sure about these objects is that they must contain the xyz property. Therefore, the type is { xyz: string }.

TypeScript has the concept of type assignability. An object type is assignable to { xyz: string } type even if it has some excess properties (there are exceptions, though - see here). So, all objects from both union members are assignable to { xyz: string } type.

anduser96 profile image
Andrei Gatej

I think I finally understood it!

So it’s typescript’s magic that translates a union that resulted from a mathematical point of view into a single type that all the other types are assignable to.

So, without typescript, I’d get { { xyz, foo }, { xyz, bar } }, but after typescript weighs in, the resulted type will only be { xyz: string }.

I hope I got it right.

Thank you!

Thread Thread
miloszpp profile image
Milosz Piechocki Author

Yeah, I think you've got it right :)