Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Typescript can't use includes function for array?

I know that Typescript use ES6 or ES2015.

And I know that anArray.includes('ifExist'); only available in ES6.

But why can't I use it when I use Typescript? It says that anArray don't have a method includes.

My Array

anArray = [
    {
        'category': 'x',
        'data': []
    }, 
    {
        'category': 'y',
        'data': []
    }, 
    {
        'category': 'z',
        'data': []
    }
];

ifExist variable

ifExist = {
    'category': 'a',
    'data': []
};

If I use anArray.indexOf(isExist) < 0, I can get the result like anArray.includes(isExist).

like image 276
Michael Harley Avatar asked Feb 16 '26 03:02

Michael Harley


1 Answers

You need to specify target in your ts-config.json to ES2016 (or higher) in order to use Array.includes.

like image 118
Pritilender Avatar answered Feb 17 '26 19:02

Pritilender



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!