Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode shows me "Type must have a [Symbol.iterator]() method (...) " but Type has it. #5142

Closed
xLama opened this issue Oct 6, 2015 · 1 comment
Labels
Question An issue which isn't directly actionable in code

Comments

@xLama
Copy link

xLama commented Oct 6, 2015

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
}   

@xLama xLama changed the title VSCode show me "Type must have a [Symbol.iterator]() method (...) " but Type has it. VSCode shows me "Type must have a [Symbol.iterator]() method (...) " but Type has it. Oct 6, 2015
@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Oct 6, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Oct 6, 2015

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>;
}

@mhegazy mhegazy closed this as completed Nov 13, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants