Currently, I am working on developing a JavaScript function library for personal use. However, there is always a possibility that others may utilize it in their projects. As such, I am creating it with the mindset that it could be used by someone else.
The majority of the methods within the library only function correctly if the variables passed are of the appropriate data type. My main query revolves around how to notify users when a variable is not of the correct type. Is throwing an error the most effective way to address this?
function foo(thisShouldBeAString){
//Assuming this is a method and not a global function
if(typeof(thisShouldBeAString) === 'string') {
throw('foo(var), var should be of type string');
}
#yadayada
}
I understand that JavaScript performs internal type conversions, which can lead to unexpected results (e.g., '234' + 5 = '2345', but '234' * 1 = 234). This behavior could potentially cause issues with my methods.
EDIT
To clarify further: I aim to avoid type conversion; the variables passed must match the required type. What approach would be best to inform users of my library when incorrect types are being passed?