I am currently creating a javascript function library. Mostly for my own use, but you can never be sure that someone else will eventually use it in their projects, I at least create it as if it could happen. Most methods work only if the variables that were passed have the correct data type. Now my question is: what is the best way to warn users that a variable does not match the correct type? Should someone miss such an error?
function foo(thisShouldBeAString){
if(typeof(thisShouldBeAString) === 'string') {
throw('foo(var), var should be of type string');
}
}
I know that javascript does an internal type conversion, but it can create very strange results (for example, "234" + 5 = "2345", but "234" * 1 = 234), and this can make my methods very strange.
EDIT
To make things clearer: I don't want to do type conversion, the passed variables must be of the correct type. What is the best way to tell the user of my library that the variables passed are not of the type?
source
share