Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting typescript to allow iterator helpers

Tags:

typescript

I'm compiling some typescript code using typescript 5.8.3. At one point in my code, I'm using an iterator helper on the values of a map, as in myMap.values().map(...). This seems to be causing a failure to compile, with the error "Property 'map' does not exist on type 'MapIterator...".

Reading the release notes for Typescript 5.6 makes me think that versions of typescript greater than 5.6 should know that the iterators returned by Map.proptype methods such as values() have iterator helper methods like map().

Am I misunderstanding those release notes? Or is there something I need to do in my tsconfig or in my typescript code to get typescript to treat the iterators returned by Map.prototype.values() and similar as iterators that have the iterator helper methods?

like image 341
rocdaddy Avatar asked Aug 28 '26 14:08

rocdaddy


1 Answers

Iterator helper methods are part of ES2025. If you write TypeScript code using these methods, you should target a runtime of ES2025 or later. As far as I know, TypeScript has not yet (as of 2025-08-31) introduced an es2025 --target, but you can use the esnext target for the latest features:

// @target: esnext
declare const myMap: Map<string, number>;
myMap.values().map(x => x + 3); // okay

Playground link to code

There are also ways of leaving --target alone and curating your --lib option to include the declarations you care about. (I don't generally recommend doing this unless you want such granular control.) For now they are at esnext.iterator but I expect to see them in es2025.iterator when that comes out:

// @lib: esnext.iterator
declare const myMap: Map<string, number>;
myMap.values().map(x => x + 3); // okay

Playground link to code

like image 110
jcalz Avatar answered Sep 04 '26 15:09

jcalz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!