Is there a way to let a javascript function know that a certain parameter is of a certain type?
Being able to do something like this would be perfect:
Maybe a helper function like this. But if you see yourself using such syntax regularly, you should probably switch to Typescript.
function check(caller_args, ...types) {
if(!types.every((type, index) => {
if(typeof type === 'string')
return typeof caller_args[index] === type
return caller_args[index] instanceof type;
})) throw Error("Illegal argument given");
}
function abc(name, id, bla) {
check(arguments, "string", "number", MyClass)
// code
}