I'm having enums with identical values but different names:
enum EnumA{
HELLO,
WORLD
}
enum EnumB{
HELLO,
WORLD
}
As you they are bot generated by an external source an have the same values, but are used in an component where it can be EnumA or EnumB.
let x: EnumA | EnumB = EnumA.HELLO;
// or
let x: EnumA | EnumB = EnumB.HELLO;
If I wan to check for HELLO I need to
if(x === EnumA.HELLO || x === EnumB.HELLO)
Is there an easier way to do this check or tell typescript that these two enums are identical?
Something like:
const x: EnumA | EnumB = EnumB.HELLO;
if(x === EnumA.HELLO) {
console.log('they are the same')
}
If I try this I get the typescript warning:
This condition will always return 'false' since the types 'EnumB.HELLO' and 'EnumA.HELLO' have no overlap.
But the console.log is called! Demo: https://www.typescriptlang.org/play/#code/KYOwrgtgBAouEEEDeBYAUFTUASMAyeA8gDTpZQDqhASngCLoC+6oks8AQqhlrgSWSxVaDNMzToAxgHsQAZwAuUAB4AudpARQAPhogcoAXj0cAdHyIBudOgCWAMwAUyo4eNxN5-EQCUUbuQy8tIANsCmIdIA5o4A5AoAFsAAnlAAhgBOwFCJ2XJpEMCxPkxAA