为什么Typescript不能使用包含函数的数组?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么Typescript不能使用包含函数的数组?相关的知识,希望对你有一定的参考价值。

我知道Typescript使用的是ES6或ES2015。

而且我知道anArray.includes('ifExist');仅适用于ES6。

但是当我使用Typescript时为什么不能使用它呢?它说anArray没有方法includes

我的阵列

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

ifExist变量

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

如果我使用anArray.indexOf(isExist) < 0,我可以得到像anArray.includes(isExist)的结果。

答案

你需要在你的target中指定ts-config.jsonES2016(或更高)才能使用Array.includes

以上是关于为什么Typescript不能使用包含函数的数组?的主要内容,如果未能解决你的问题,请参考以下文章