I have a function to loop over an Array and look for a number.
function search(arr, num) { }
This function takes an Array and an Integer as the input. But I want the parameters to be of their respective types, is there any way I can set the type of the parameters.
I have a function to loop over an Array and look for a number.
function search(arr, num) { }
This function takes an Array and an Integer as the input. But I want the parameters to be of their respective types, is there any way I can set the type of the parameters.
Share Improve this question asked Aug 18, 2021 at 6:43 Dhruv AnandDhruv Anand 1331 gold badge2 silver badges11 bronze badges 5- 4 Javascript doesn't support static types, that's why TypeScript was born. – MorKadosh Commented Aug 18, 2021 at 6:45
- 2 Sidenote: Though it doesn't prevent you from passing a different type you can add JSDoc ments for better autoplete, which get picked up by most editors. – StackByMe Commented Aug 18, 2021 at 6:50
-
You can add assertions like
function search(arr, num) { if (!Array.isArray(arr)) throw new Error('Wrong type'); if (!typeof num !== 'number')) throw new Error('Wrong type'); }
– jabaa Commented Aug 18, 2021 at 6:54 - for checking types in javascript please refer javascript typeof – JsNgian Commented Aug 18, 2021 at 6:57
- @Reyno This is exactly what I was looking for, thanks a lot. I was having trouble with auto-pletion. – Dhruv Anand Commented Aug 18, 2021 at 7:00
3 Answers
Reset to default 6if you want to enforce an specific data type you need to explicitly write that
/**
* looks for an specific item of the list.
*
* @param {Array<string>} data Array to search from.
* @param {number} num Number of where to find bla bla.
* @throws {TypeError} in case data or num do not have the expected type
* @returns {string} item found.
*/
export function search(data, num) {
if (!Array.isArray(data)) {
throw new TypeError("data should be an array");
}
if (typeof num !== "number") {
throw new TypeError("num should be a number");
}
return data[num];
}
JavaScript is a loosely typed language, there's no way to say declaratively that arr
should be an array and num
should be a number other than documentation (for instance, JSDoc). That would look like this:
/**
* Searches (insert fuller description here).
*
* @param {number[]} arr The array to search through.
* @param {number} num The number to (use? search for?)
* @returns (you can use the same {syntax} to say what the return type is)
*/
function search(arr, num) {
if (!Array.isArray(arr)) {
throw new Error(`'arr' argument must be an array`);
}
if (typeof num !== "number"/* Or use `Number.isInteger(num)` for an integer check [it includes the typecheck]*/) {
throw new Error(`'num' argument must be a number`);
}
// ...do the work...
}
You could check at runtime. That would look something like this:
function search(arr, num) {
if (!Array.isArray(arr)) {
throw new Error(`'arr' argument must be an array`);
}
if (typeof num !== "number"/* && perhaps a check for int if that part is really important*/) {
throw new Error(`'num' argument must be a number`);
}
// ...do the work...
}
There's a language built on top of JavaScript called TypeScript, which adds a static type system. It's then piled to JavaScript for use with the browser, Node.js, etc. You may want to look into using that, if static typing is important for what you're doing.
In TypeScript, you'd do it like this (if you want the array to be an array of numbers):
function search(arr: number[], num: number) {
// ...
}
That doesn't require an integer, all JavaScript numbers are IEEE-754 floating point or BigInt
s.
javascript is a loosely type language. beacuse of it there are no types, you can actively check if the given parameter is array by .isArray() and .isInteger(), this both methods will return the booleen.
the answer given by pravat work but it can be devasting as they will not only set the type its will set its default value, in case if you didn't send the value of num then function will asume its 10 and start to work, which can be pain in debuging if you need strict type please look into typescript.