Skip to content

TS2495: Type 'ArrayLike<T>' is not an array type or string type. #20852

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

Closed
electricessence opened this issue Dec 21, 2017 · 7 comments
Closed

Comments

@electricessence
Copy link

electricessence commented Dec 21, 2017

for(let x of <ArrayLike<T>>source) { }

Error: TS2495: Type 'ArrayLike<T>' is not an array type or string type.

Why is this a thing? A for loop using ArrayLike<T> as a source should work for any ES target.

TS 2.6.1

@Jessidhia
Copy link

Jessidhia commented Dec 22, 2017

It definitely should not work for any ES target. It may happen to work if you are using typescript with downlevel iteration disabled, but that is a deliberate spec violation in the name of compatibility with old, non-polyfilled browsers.

The right hand side of for of is not ArrayLike, it's Iterable, from --lib es2015.iterable.

image


EDIT: which means the message probably should be changed to say that the type is not Iterable 🤔

@electricessence
Copy link
Author

electricessence commented Dec 22, 2017

@Kovensky
Isn't "ArrayLike" mean that it can be indexed (like an array), and that .length indicates the boundary of the indexes?
for of should still be constructed normally and work normally without error. Allow for some level of runtime failure.

If you look at ArrayLike<T>'s declaration it's pretty straight forward read only properties.

@Jessidhia
Copy link

Jessidhia commented Dec 22, 2017

yes, but for of does not care about .length or numerical properties.

for of only cares about the [Symbol.iterator]() property, and that it returns an iterator. Actual Arrays have a [Symbol.iterator]() that will return their elements in order; an arbitrary object that is ArrayLike potentially doesn't, such as the object I used in the TypeError screenshot.

If you do want to iterate over any ArrayLike, you can pass it as the argument to Array.from(), which converts both ArrayLike and other iterables into an ordinary Array (which is, again, itself iterable).

@electricessence
Copy link
Author

Sorry still trying to wrap my head around why this is a thing.

Since the underlying JS code will work, just as it does with an array. So instead of an efficient for loop being generated under the hood, I now have to call Array.from()?

Maybe it's just my opinion, but this seems overly strict. And I normally prefer strict. :|

@electricessence
Copy link
Author

electricessence commented Dec 22, 2017

for(let x of <ArrayLike<T>>source) { }

Can you provide a type alternative to ArrayLike<T> that will work? Given that source is potentially exactly that: an integer indexed (readonly) object?

@DanielRosenwasser
Copy link
Member

Looks like a duplicate of #2862.

@electricessence
Copy link
Author

Seems dupe-enough, but I still feel left uneasy because ArrayLike<T> should be enough to treat it like an array. :/ So now I need to use what type instead? Iterable<T>? Which is still not correct if what I really want is a simple for(var _i=0;... underneath. :/

@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants