How can a JS array be limited to one type? E.g. string.
Say I have the following code:
const arr = [];
arr.push('this is a string'); // accept
arr.push('I do not want numbers in this array'); // accept
arr.push(5); // reject - it's not a string!
How can I reject the last push or any other push that attempts to add a variable which is not of type string? The only way I can think of is to make a custom class which extends Array and overrides functions, such as push, to check the type of the element being added and throw an error if not of type string. However, this seems pretty nuclear and bug-prone!
pushmethod. Simple.