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

TouchList is missing Symbol.iterator, can't use with 'for..of' #31826

Open
fatcerberus opened this issue Jun 8, 2019 · 12 comments
Open

TouchList is missing Symbol.iterator, can't use with 'for..of' #31826

fatcerberus opened this issue Jun 8, 2019 · 12 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@fatcerberus
Copy link

TypeScript Version: 3.6.0-dev.20190608

Search Terms: touchlist iterator

Code

let touches = new TouchList();
for (const touch of touches) {

}

Expected behavior:
Code compiles successfully.

Actual behavior:
Error on line 2:
Type 'TouchList' must have a '[Symbol.iterator]()' method that returns an iterator. ts(2488)

Playground Link:
N/A because the Playground complains due to for...of requiring --downlevelIteration--but there's no way to enable it.

Related Issues:
#2695, labeled help wanted but closed

@jakebailey
Copy link
Member

This is an old issue, but is this actually an issue given we have:

interface TouchList {
[Symbol.iterator](): IterableIterator<Touch>;
}

This was introduced by #24669, but that PR is a full year older than this issue, so I'm not sure what's going on here such that this error ever appeared.

(@fatcerberus I think you're still around, so I'm not gonna close this in case you recall what was actually going on and I'm just reproducing it wrong.)

@fatcerberus
Copy link
Author

@jakebailey I don’t recall; it might be that I didn’t have dom.iterable in my tsconfig or something (it was only recently that I found out that even existed).

It also seems weird that the playground required downlevelIteration for the for..of loop, for that matter… maybe in 2019 it only supported ES5 emit or something.

@jakebailey
Copy link
Member

Ah, gotcha, yeah, one has to specify DOM.Iterable, it seems (the playground must). I guess I'm not well-versed enough to understand why that's not included by default, or if we should be hinting to users that "maybe you forgot this". @sandersn may know better than me, I'm just going over old issues looking for already fixed stuff!

@sandersn
Copy link
Member

I can't even repro this locally. What's the tsconfig look like? I tried target: "es5" and lib: ["dom", "es5"].

@jakebailey
Copy link
Member

jakebailey commented Jul 20, 2023

All I have is:

// @filename: tsconfig.json
{
    "compilerOptions": {
        "lib": ["es2016", "dom"],
        "target": "ES2015",
        "noEmit": true,
        "types": []
    }
}

// @filename: test.ts
let touches = new TouchList();
for (const touch of touches) {

}

image

@fatcerberus
Copy link
Author

I don’t think I have the project I was testing this in anymore, so I haven’t tested locally yet, but it’s still reproducible in the playground at least:
https://www.typescriptlang.org/play?downlevelIteration=true&target=1&module=0#code/CYUwxgNghgTiAEYD2A7AzgF3hpBXMAFiGgFzwAqehAMgJaYDcAUAGZIzwAUy6WtWSFtipE0ASngBvAL5MgA

@sandersn
Copy link
Member

Argh, I wrote for (const touch in touches). in

@sandersn
Copy link
Member

Anyway, there's a fixed list of constructors (Promise, Symbol, etc) in isES2015OrLaterConstructorName and in getCannotFindNameDiagnosticForName, some other values, that trigger a specific error. I didn't see anything smarter than that.

I don't know why dom.iterable is broken out from dom either. It was from before my time helping with DOM types.

@jakebailey
Copy link
Member

Concretely, my questions are just:

  • Is this issue closable, given it works when you include DOM.iterable?
  • What's the deal with DOM.Iterable anyway?
    • Probably, it's because the DOM types are not split apart by ES versions as there's no mechanism to do so?

@sandersn
Copy link
Member

  1. Yes, I guess so. We could do better on missing lib errors, but I'm not sure how (cheaply, at least).
  2. I don't understand why iterable is the dividing line (or, a dividing line) though.

@fatcerberus
Copy link
Author

I always assumed the reason dom.iterable was split out is because it involves Symbol.iterator, which can’t (to my knowledge) by polyfilled correctly in ES5.

@jakebailey
Copy link
Member

That's my guess, yeah, it's just that unlike setting say, es6 or es2017 or something, it doesn't actually get enabled automatically...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants