index.ts278 chars20 lines
In the example above, we declare two object types Type1
and Type2
. We want to combine them into a single type, so we'll use the &
operator to create a type intersection.
The resulting type MergedType
will have all the properties and their types from both Type1
and Type2
. We can then create an object of MergedType
and assign values to its properties.
TypeScript also offers type unions, which is another approach to combining two or more types.
index.ts243 chars18 lines
In this example, we declare a type union using the |
operator. The resulting type MergedType
can contain values that have either the properties and types of Type1
or Type2
.
Note that in the latter case, we can create an object containing only prop1
and prop2
, but not prop3
and prop4
.
gistlibby LogSnag