typescript 类型“string[]”上不存在属性“includes”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40545329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:28:20  来源:igfitidea点击:

Property 'includes' does not exist on type 'string[]'

typescript

提问by ZeeAzmat

Getting the error

得到错误

Property 'includes' does not exist on type 'string[]'

类型“string[]”上不存在属性“includes”

in node_modules/ng2-breadcrumb/app/components/breadcrumbService.ts I am trying to implement breadcrumb functionality in an angular2 app.

在 node_modules/ng2-breadcrumb/app/components/breadcrumbService.ts 我试图在 angular2 应用程序中实现面包屑功能。

回答by mrkvon

Add "ES2017"to your "lib"array in tsconfig.json:

添加"ES2017"到您的"lib"数组中tsconfig.json

{
  "compilerOptions": {
    ...
    "lib": ["es6", "dom", "es2017"],
    ...
    "target": "es5",
    ...
  }
}

This should work since TypeScript 2.1.

这应该从 TypeScript 2.1 开始工作。

A related issue.

一个相关的问题

Explanation

解释

The includesmethod on Arrayis supported since ES7 (ES2016). The above will add a missing library file to compilation.

includes上方法Array自ES7(ES2016)被支撑。以上将添加一个缺少的库文件进行编译。

The TypeScript compiler options are documented here.

此处记录TypeScript 编译器选项。

Lib es2016or es7may be sufficient instead of es2017(not tested).

Libes2016es7可能足以代替es2017(未测试)。

回答by ovabrandon

If you don't want to change to es2016, just use arr.indexOf(valueToCheck) !== -1.

如果你不想改成 es2016,直接用arr.indexOf(valueToCheck) !== -1.

回答by BlackGlory

Changing the compiler target to "es2016" in tsconfig.jsshould solve this issue.

将编译器目标更改为“es2016”tsconfig.js应该可以解决这个问题。