Here is the code.
type Combinable = string | number;
function add(n: Combinable, b: Combinable) {
if (typeof n === 'string'&& typeof b ==='string') {
return n.toString() + b.toString();
} else if (typeof n === 'string'&& typeof b ==='number') {
return n.toString() + b.toString();
} else if (typeof n === 'number'&& typeof b ==='string') {
return n.toString() + b.toString();
} else {
// here raises error
// Operator '+' cannot be applied to types 'Combinable' and 'Combinable'.ts(2365)
return n + b;
}
}
// But it works fine when I change "and" operator to "or" operator.
function add(n: Combinable, b: Combinable) {
if (typeof n === 'string' || typeof b ==='string') {
return n.toString() + b.toString();
} else {
return n + b;
}
}
As you can see, I got the error on the last line n + m.
I thought it should work if I cover all cases, but the error still remains.
What am I missing here?