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

Mic*_*ley 0 typescript ecmascript-6

我知道 Typescript 使用 ES6 或 ES2015。

而且我知道这anArray.includes('ifExist');仅在 ES6 中可用。

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

我的数组

anArray = [
    {
        'category': 'x',
        'data': []
    }, 
    {
        'category': 'y',
        'data': []
    }, 
    {
        'category': 'z',
        'data': []
    }
];
Run Code Online (Sandbox Code Playgroud)

ifExist 变量

ifExist = {
    'category': 'a',
    'data': []
};
Run Code Online (Sandbox Code Playgroud)

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

小智 5

您需要targetts-config.jsonto ES2016(或更高)中指定才能使用Array.includes.