Extending the most upvoted answer, it is also type-safe if we use let
for the iteration variable, so:
for (let elem of setOfElems) {
... do anything with elem...
}
This will guarantee that elem
will have the type X, if setOfElems
was declared as Set
.