Intersecting with an optional type

It is possible to intersect two types with cross properties that have different rules. A field in one type with the mention of an option member can be merged with a type that is not optional. The result is that the field becomes non-optional:

 interface InterfaceSameField1 {
m1: string;
}

interface InterfaceSameField2 {
m1?: string;
}

type Same = InterfaceSameField1 & InterfaceSameField2;
let same: Same = { m1: "This is required" };

The previous code example shows the intersection and the field m1 being required. If omitted or set to undefined, the code does not compile.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.200.154