I can see that error with this code:
function* numIterator(num: number) {
var n = parseInt(num.toString()).toString();
var length = n.length;
for (var i = length - 1; i >= 0 ; --i){
yield n.charAt(i);
}
}
Number.prototype[Symbol.iterator] = function() {
return numIterator(this.valueOf());
}
var num = 2387;
for (var n of num){ // Error in VSCode but It works fine in Chrome
console.log(n) // 7, 8, 3, 2
}
You are augmenting the Number
interface without telling the type system about it, so it does not know that a number now has an iterator. you need to augment the declaration of interface Number
to add the iterator declaration, so adding something like this to your file should get rid of the error:
interface Number {
[Symbol.iterator](): IterableIterator<string>;
}
Most helpful comment
You are augmenting the
Number
interface without telling the type system about it, so it does not know that a number now has an iterator. you need to augment the declaration of interfaceNumber
to add the iterator declaration, so adding something like this to your file should get rid of the error: