Skip to content
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

ES7 Array.prototype.includes() missing in TypeScript #2340

Closed
tinganho opened this issue Mar 13, 2015 · 36 comments
Closed

ES7 Array.prototype.includes() missing in TypeScript #2340

tinganho opened this issue Mar 13, 2015 · 36 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@tinganho
Copy link
Contributor

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 tinganho changed the title ES6 Array.prototype.inclues() missing in TypeScript ES7 Array.prototype.inclues() missing in TypeScript Mar 13, 2015
@danquirk
Copy link
Member

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.

@tinganho
Copy link
Contributor Author

@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.

@rolyp
Copy link

rolyp commented Mar 15, 2015

Suggest fixing the typo in the title for searching purposes

@tinganho tinganho changed the title ES7 Array.prototype.inclues() missing in TypeScript ES7 Array.prototype.includes() missing in TypeScript Mar 15, 2015
@tinganho
Copy link
Contributor Author

@rolyp fixed.

@danquirk
Copy link
Member

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.

@tinganho
Copy link
Contributor Author

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

@RyanCavanaugh
Copy link
Member

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.

@danquirk danquirk added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Mar 17, 2015
@OlsonDev
Copy link

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.

@RyanCavanaugh
Copy link
Member

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

@johnnyreilly
Copy link

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

@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed Declined The issue was declined as something which matches the TypeScript vision labels May 31, 2016
@mhegazy mhegazy added this to the TypeScript 2.0 milestone May 31, 2016
@dalexander01
Copy link

dalexander01 commented Aug 15, 2016

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
Copy link

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

@michaeljota
Copy link

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

@sinedied
Copy link

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

@michaeljota
Copy link

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

@cjke
Copy link

cjke commented Feb 3, 2017

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

@veeramarni
Copy link

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

@mhegazy
Copy link
Contributor

mhegazy commented May 24, 2017

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

@shaunluttin
Copy link

shaunluttin commented Jun 29, 2017

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

Make sure you put lib into compilerOptions.

@samrae7
Copy link

samrae7 commented Jul 20, 2017

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

@pendar747
Copy link

pendar747 commented Aug 26, 2017

I also have the same problems as @samrae7 with the following configuration:

{
    "compilerOptions": {
        "module": "commonjs",
        "removeComments": true,
        "lib": [
            "dom",
            "es2017"
        ],
        "sourceMap": true,
        "outDir": "../dist",
        "target": "es6",
        "watch": true,
        "allowJs": true,
        "typeRoots": [ "node_modules/@types" ]
    },
    "exclude": [
        "node_modules"
    ],
    "compileOnSave": true
}

P.S. Using version 2.4.2 of typescript

@michaeljota
Copy link

Includes is part of es2016, and you don't seem to have it in lib. 😄. Just add it there, it should work.

@gpluta
Copy link

gpluta commented Sep 9, 2017

What you need is "es2016.array.include" in your "lib" config.

@jgerstle
Copy link

I was able to get typescript to compile the array.includes by changing my lib from "lib": ["dom", "es2015"] to "lib": ["dom", "es2016"]. However I get the following in ie11: ERROR TypeError: Object doesn't support property or method 'includes'. Do I need to add a polyfill in addition? I thought typescript handles that my target is es5.

@michaeljota
Copy link

Typescript does not includes any polyfills, you have to include them yourself.

@geoffdavis92
Copy link

@jgerstle @pendar747 This comment gets TS working with Array.includes on v2.6.2

@NullVoxPopuli
Copy link

I'm still having issues with this:

http://kangax.github.io/compat-table/es2016plus/

using tsc 2.7.2

@mhegazy
Copy link
Contributor

mhegazy commented Feb 27, 2018

@NullVoxPopuli please see #2340 (comment)

@0vidiu
Copy link

0vidiu commented Mar 13, 2018

For what it's worth, I had to restart VSCode to force it to reload tsconfig.json after updating the lib array. That stopped it from bugging be.

@michaeljota
Copy link

@0vidiu in deed, VSCode take its time to reload the tsconfig changes, but it shouldn't need a restart.

@0vidiu
Copy link

0vidiu commented Mar 13, 2018

@michaeljota Maybe I was just impatient :)

@mateusduraes
Copy link

mateusduraes commented Mar 20, 2018

It worked for me.
I've change the lib property to (tsconfig.json)

"lib": [
      "dom",
      "es2017"
    ],

My typescript version is 2.4.2, now Array.prototype.include is included

@fvsch
Copy link

fvsch commented Apr 8, 2018

"lib": ["es2017", "dom"] and a VSCode restart needed for me too.

@troy351
Copy link

troy351 commented Apr 9, 2018

thanks @fvsch , remember to restart VSCode

@connorjclark
Copy link
Contributor

Yup... restarting vscode was necessary.

@ColCh
Copy link

ColCh commented Apr 22, 2018

#2340 (comment) or just select restart TS server in VSCode command menu

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests