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

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

pendar747 avatar Aug 26 '17 13:08 pendar747

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

michaeljota avatar Aug 26 '17 15:08 michaeljota

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

gpluta avatar Sep 09 '17 20:09 gpluta

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.

jgerstle avatar Oct 29 '17 15:10 jgerstle

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

michaeljota avatar Oct 29 '17 21:10 michaeljota

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

geoffdavis92 avatar Dec 03 '17 22:12 geoffdavis92

I'm still having issues with this:

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

using tsc 2.7.2

NullVoxPopuli avatar Feb 27 '18 18:02 NullVoxPopuli

@NullVoxPopuli please see https://github.com/Microsoft/TypeScript/issues/2340#issuecomment-316664621

mhegazy avatar Feb 27 '18 22:02 mhegazy

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.

0vidiu avatar Mar 13 '18 22:03 0vidiu

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

michaeljota avatar Mar 13 '18 22:03 michaeljota

@michaeljota Maybe I was just impatient :)

0vidiu avatar Mar 13 '18 22:03 0vidiu

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

mateusduraes avatar Mar 20 '18 16:03 mateusduraes

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

fvsch avatar Apr 08 '18 21:04 fvsch

thanks @fvsch , remember to restart VSCode

troy351 avatar Apr 09 '18 03:04 troy351

Yup... restarting vscode was necessary.

connorjclark avatar Apr 19 '18 06:04 connorjclark

https://github.com/Microsoft/TypeScript/issues/2340#issuecomment-382621228 or just select restart TS server in VSCode command menu

ColCh avatar Apr 22 '18 08:04 ColCh