Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'includes' does not exists on type 'string[]' in TypeScript [duplicate]

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.
like image 462
H. Pauwelyn Avatar asked Sep 20 '26 02:09

H. Pauwelyn


1 Answers

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)

like image 165
Titian Cernicova-Dragomir Avatar answered Sep 21 '26 23:09

Titian Cernicova-Dragomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!