Having the following code:
function A(arg1?: string, arg2?: number) {
if (!arg1 && !arg2) {
throw new Error('At least one of two parameters must be present');
}
B(arg1 || arg2);
}
function B(arg3: string | number) {
// does something. we don't really care
}
Typescript throws the following compilation error for the expression B(arg1 || arg2):
Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'. ts(2345)
However, in function A I make sure that at least one of the arguments is not undefined, throwing an error in that case. That means that in the expression B(arg1 || arg2) the argument will never be undefined, it will always be either number or string.
Is something wrong in my assumption? Is there any way of making typescript understand it?
B(arg1 || arg2 || 0)Just to ensure the compiler has a fallback, that never is used.