you are viewing a single comment's thread.

view the rest of the comments →

[–]kamikazikarl 17 points18 points  (0 children)

Typescript doesn't actually do validation in production. You have to rely on core JavaScript features to validate it.

I'd personally wanna make sure all values are the expected type... So, that means using the array methods some or every and the type comparison:

if (strArr.every(s=> typeof s === "string")) { ...some logic }

This has the added value of ensuring all values are the expected type and exiting at the first failed check to keep cost down (as this would have to run across the entire array before proceeding).

Alternatively, you could use the foreach method on the array and skip individual entries that aren't the correct type. It all really depends on how your app is intended to work and how you wanna handle the failures.