ES7 Array.prototype.includes() missing in TypeScript
Seems like the ES7 .includes() method is missing in TypeScript.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
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 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.
Suggest fixing the typo in the title for searching purposes
@rolyp fixed.
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.
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
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.
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.
ES7 features will be in the ES7 lib.d.ts target
Awesome. Is ES7 lib.d.ts due to ship with TS 2.0?
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" ] }
Nevermind, I fixed this by adding lib:["es2016", "dom"] to the compiler options in tsconfig.json
I had to add "es2016.array.include" instead. Don't know if it is any different, but for information sake.
With [email protected] and ["es6", "es2016", "dom"] it does not work for me (and neither with "es2016.array.include")?
@sinedied I think this was released in final 2.1, so you will probably need +2.1 for this to work.
Can confirm that Version 2.1.5 with "lib":["dom","es2017"] includes includes
I still see this issue with TS 2.3.2 and "lib":["dom", "es2017"]
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
I still see this issue with TS 2.3.2 and "lib":["dom", "es2017"]
Make sure you put lib into compilerOptions.
Working for me with TS ^2.3.2 in package.json and "lib":["dom", "es7"] in compilerOptions ( tsconfig.json)