I'm trying to check whether a variable belongs to a certain type or not.
Code:
type GeneralType = SubTypeA | SubTypeB;
type SubTypeA = 'type1' | 'type2';
type SubTypeB = 'type3' | 'type4';
function someFunction(arg1: GeneralType) {
if (arg1 instanceof SubTypeA) {
// Do something
}
// Continue function
return arg1;
}
Of course this code fails in line 6 because instanceof
is not usable for types. Is there an alternative option I could use without needing to check explicitly every posible value for SubTypeA
?