I have a function that accepts x and y. One of the coordinates is always null. Another one is always number. The function returns the value of a not-null coordinate: either x or y. The function always returns a number.
function test({ x, y }: { x: null; y: number } | { x: number; y: null }): number {
if (x === null) {
// y should be recognized as 'number', but it is 'number | null'
return y; // ERROR
}
return x;
}
test({x: null, y: 42 }); // 42
Is it possible to keep the return type of this function always as a number?