Compute integer square root with binary search, no Math.sqrt
Create a function mySqrt that computes and returns the square root of a given number x. The function should return the integer part of the square root, similar to how the built-in Math.sqrt function works, but without using it directly. Your implementation should use a binary search algorithm to efficiently find the square root. Computing a square root in JavaScript this way should return the same results as Math.sqrt().
Additionally, your function should handle edge cases such as negative numbers, non-numeric inputs, and NaN by returning NaN.
Function Signature:
function mySqrt(x: number): number
Example Usage:
console.log(mySqrt(4)); // Output: 2
console.log(mySqrt(8)); // Output: 2 (since the square root of 8 is approximately 2.82842..., and the function returns the integer part)
The input x is guaranteed to be a non-negative number. The function should aim for an efficient solution, ideally with a time complexity of O(log n).