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
Property 'includes' does not exist on type 'string[]'
提问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 开始工作。
一个相关的问题。
Explanation
解释
The includes
method on Array
is 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.
Lib es2016
or es7
may be sufficient instead of es2017
(not tested).
Libes2016
或es7
可能足以代替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.js
should solve this issue.
将编译器目标更改为“es2016”tsconfig.js
应该可以解决这个问题。