Suggestion: syntax to iterate in an array or an object and for slices

Topics: Language Specification
Oct 1, 2012 at 9:48 PM

I'm not sure it fits in the direction you want the language to take but here's some things I would really enjoy:


Something like the C# foreach or the coffeescript "for value in object" / "for index, value in array" / "for key, value of object"


A short slice syntax like the python one or the coffeescript one: array[:-1]

Oct 1, 2012 at 10:33 PM

In ES5, you can use the array forEach method:

[0, 1, 4].forEach(n => console.log(n * 3));

Note that 'n' is contextually typed to be of type 'number'. Is this what you're looking for?

Oct 1, 2012 at 10:56 PM

Thanks, well it works but it's not really as convenient as a language keyword, (like forEach method in LINQ vs foreach keyword in C#).

I konw it's just syntactic sugar but it's a lot of keystrokes saved at the end of the day :), expecially if the (index, value) in array and (key, value) in object are available.


I might be a bit off, I'm not sure how close to javascript you want TS to stay (of course as a superset of JS, implementing new languages features is harder than with a new languague)

Oct 12, 2012 at 1:50 PM

Looks like my (for x of y) suggestion is actually in Harmony, certainly better explained:

in the "for of iteration" section.


Of course all of these features are nice and it'd be great to be able to use them on ES3 engines thanks to typescript.