I have a scenario where I need to return a specific property from a function in various parts of an application. This property can fall into one of two categories, each with string literal properties. One category is an extension of the other. How can I effectively handle returning the same variable cast as one type or the other using a type guard?
type NameA = 'A' | 'B' | 'C'
type NameB = NameA | 'D' | 'E'
function nameTypeGuard(
name: NameA | NameB
): NameA | NameB {
if (some condition) {
return name as NameA;
} else {
return name as NameB;
}
}