Quantcast
Channel: User Brett Merrifield - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Answer by Brett Merrifield for Javascript / ES6 Array of single type

$
0
0

I would recommend using TypeScript if you want to add type safety to your Javascript. Below is a function you may use though to ensure what you push is the correct type

const pushType = (arr, value, type) => {  if (typeof value === type) {    arr.push(value);  }};pushType(['a', 'b'], 'c', 'string');

Viewing all articles
Browse latest Browse all 37

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>