Does anyone know if there\'s a good tool for analyzing JavaScript code and detecting type errors? I know that JavaScript itself is weakly and dynamically typed, but it woul
Below is a basic example on how to do type checking in JavaScript:
const Int = (val) => {
if (Number.isInteger(val)) {
return val;
}else {
throw new TypeError(`Argument value ${val} is not integer`);
}
return 0; //Unreachable: for static analysis
};
const sum = function(_, numA = Int(_[0]), numB = Int(_[1])) {
return `Sum is ${numA + numB}`;
};
console.log(sum([1, 2])); //correct
console.log(sum([1, "2"])); //incorrect