TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

ES7 Array.prototype.includes() missing in TypeScript

Open tinganho opened this issue 11 years ago • 36 comments

Seems like the ES7 .includes() method is missing in TypeScript.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

tinganho avatar Mar 13 '15 08:03 tinganho

Where is there an implementation of this that can be used? It doesn't make much sense for us to add things to lib.d.ts that aren't readily available. You can always augment the definition of Array yourself if your environment does support this.

danquirk avatar Mar 14 '15 00:03 danquirk

@danquirk It has a wide implementation area, that's why it will be included in ES7.

I'm not sure what TypeScript's goal is? to include everything in ES6/ES7? I think people expect TypeScript to be a superset of Javascript, because that's what you have mentioned? Thus, implying that you compile everything and beyond BabelJS. And BabelJS compile ES6 and ES7 down to ES5 and ES3.

It would be good if TypeScript had one feature table also like BabelJS's http://babeljs.io/docs/compare/. It makes it easier for developer to understand which feature is available.

tinganho avatar Mar 14 '15 01:03 tinganho

Suggest fixing the typo in the title for searching purposes

rolyp avatar Mar 15 '15 07:03 rolyp

@rolyp fixed.

tinganho avatar Mar 15 '15 12:03 tinganho

ES6 is not widely or fully supported yet. Adding ES7 functionality to lib.d.ts seems premature unless this is actually usable in many places. If we put this in lib.d.ts right now then a bunch of people will dot off of arrays and see a handy includes function and attempt to use it. In the majority of environments when they actually run that code it won't work since includes isn't supported yet. How many people are actually using ES7 functions right now?

So then our options are to try to polyfill all the functionality of a moving spec (since if we do this for includes we should certainly do it for any other similar functions) or only make this usable for people using certain additional transpilers. We don't currently polyfill any other standard library type functions for you though. It doesn't seem super desirable to have our completion lists filled with a set of options that you need to wonder whether they work or not or whether they're natively supported or not.

danquirk avatar Mar 16 '15 19:03 danquirk

I guess ES6 and ES7 are not widely used yet. So isn't TypeScript. It seems like the large JS community is embracing ES6/ES7 to ES5 transpilers, especially BabelJS. The reason I choose to code in TS instead of ES6 is because TypeScript supports types and other nice features. But I've learnt the hard way — that some features are only available if the compile target is ES6, this is NOT good. Because of legacy issues and ES6 is not widely adopted — I think everything should be able to compile down to ES5.

I guess many people are using the new ES7 features in BabelJS. Why wouldn't they? And if you look at their downloads they are almost in par with or higher than TypeScript, in a very short time: http://npm-stat.com/charts.html?package=babel

tinganho avatar Mar 17 '15 10:03 tinganho

There's a big difference between language features and polyfills, though.

If you have polyfilled includes, you can augment the definition of Array to include it. The scenario is fully supported by TypeScript.

If you haven't, then it's almost certainly not going to work in a majority of runtimes, and it'd be a mistake to put in in lib.d.ts until that's not the case.

RyanCavanaugh avatar Mar 17 '15 15:03 RyanCavanaugh

How about now? It appears the majority of runtimes support it now: Chrome 47, Firefox 43, Opera 34, Safari 9, Android Webview, Firefox Mobile 43, Opera Mobile 34, Safari Mobile 9, Chrome for Android 47. Missing support: Internet Explorer, Edge, Android, IE Mobile.

For now, I'll augment my Array definition locally. Just thought perhaps it was time you guys revisit it.

OlsonDev avatar Mar 29 '16 00:03 OlsonDev

ES7 features will be in the ES7 lib.d.ts target

RyanCavanaugh avatar Mar 29 '16 01:03 RyanCavanaugh

Awesome. Is ES7 lib.d.ts due to ship with TS 2.0?

johnnyreilly avatar May 31 '16 14:05 johnnyreilly

Hi, I am using typescript next and cannot get Array.prototype.includes to work. I get the following errors:

Property 'includes' does not exist on type 'string[]'

For what it's worth I also am seeing errors around lib.es2016.d.ts and lib.es2017.d.ts not being in the compilation context which is probably why things are not working. Here is my tsconfig if that heps:

{ "compileOnSave": true, "compilerOptions": { "module": "es6", "target": "es6", "moduleResolution": "node", "allowSyntheticDefaultImports": true, "noImplicitAny": true, "sourceMap": true, "outDir": "ts-build", "jsx": "preserve" }, "exclude": [ "node_modules" ] }

dalexander01 avatar Aug 15 '16 18:08 dalexander01

Nevermind, I fixed this by adding lib:["es2016", "dom"] to the compiler options in tsconfig.json

dalexander01 avatar Aug 15 '16 18:08 dalexander01

I had to add "es2016.array.include" instead. Don't know if it is any different, but for information sake.

michaeljota avatar Dec 05 '16 18:12 michaeljota

With [email protected] and ["es6", "es2016", "dom"] it does not work for me (and neither with "es2016.array.include")?

sinedied avatar Jan 12 '17 14:01 sinedied

@sinedied I think this was released in final 2.1, so you will probably need +2.1 for this to work.

michaeljota avatar Jan 12 '17 18:01 michaeljota

Can confirm that Version 2.1.5 with "lib":["dom","es2017"] includes includes

cjke avatar Feb 03 '17 03:02 cjke

I still see this issue with TS 2.3.2 and "lib":["dom", "es2017"]

veeramarni avatar May 24 '17 15:05 veeramarni

I still see this issue with TS 2.3.2 and "lib":["dom", "es2017"]

please file a new issue and share a self-contained repro

mhegazy avatar May 24 '17 16:05 mhegazy

I still see this issue with TS 2.3.2 and "lib":["dom", "es2017"]

Make sure you put lib into compilerOptions.

shaunluttin avatar Jun 29 '17 22:06 shaunluttin

Working for me with TS ^2.3.2 in package.json and "lib":["dom", "es7"] in compilerOptions ( tsconfig.json)

samrae7 avatar Jul 20 '17 10:07 samrae7