3

From this question and the accepted answer, I know that there is an methode to check if an array includes an object in JavaScript by using this line of code:

> ['joe', 'jane', 'mary'].includes('jane');
true 

However if I use the same code in TypeScript I've got this error:

Property includes does not exist on type string[].

  1. Why gives the compiler this error? I'm expecting that everything that is available in JS, is available in TS.
  2. How could I solve this error without changing the code? It must be working on ES5.
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • @Baboo_: It's not a duplicate because my question goes about **TypeScript** and not about JavaScript like the link you provided! – H. Pauwelyn Mar 06 '19 at 13:59
  • I missed copied, the link you provided is the one I wanted to provide ;) – Baboo Mar 06 '19 at 14:04

1 Answers1

6

Includes is defined in the lib.es2016.array.include.d.ts since it is part of the es2016 standard. You can include this lib in your tsconfig but you have to provide your own polyfil for the method:

{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "dom",
            "scripthost",
            "es5",
            "es2016.array.include"
        ]
    }
}

You can also provide the polyfill by assigning Array.prototype.include if your runtime does not provide this method on array (you can get a polyfill from here for example)

Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357