Cast declaration variable inside for of loop

Viewed 9971

Is there a shorter and simplified version of casting a declaration variable inside of "for of" loop.

let array1: Array<String | Number>;
for (let a of array1) {
    let ab: String = <String>a;
}

I am aware casting the whole array would work, but tbh i would feel much more happy if i could either cast or set the datatype of declaration variable "a" instead of casting the whole array, Is something like that possible? if i type let <String>a or let a:String it doesn't work.

2 Answers

Similarly, forEach iterations on DOM NodeList would be cast like:

const links: NodeListOf<HTMLElement> = document.querySelectorAll('a')
links.forEach(a => a.setAttribute('target', '_blank'))
Related