typescript TS2339:“字符串”类型上不存在属性“包含”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51811239/
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
TS2339: Property 'includes' does not exist on type 'string'
提问by Michael Kolber
I have seen this error mentioned in regards to string arrays but not actual strings. I have a TypeScript file with the line
我已经看到在字符串数组中提到的这个错误,但没有看到实际的字符串。我有一个带有该行的 TypeScript 文件
if (!bus.lineInfo.PublishedLineName.includes(input)) {
This gives me an error of
这给了我一个错误
TS2339: Property 'includes' does not exist on type 'string'.
bus
is a variable that implements the bus
interface:
bus
是一个实现bus
接口的变量:
interface bus {
"lineInfo": {
"PublishedLineName": string,
"DestinationName": string, // The headsign of the bus
"Color": string,
"TextColor": boolean | string // false if this is "FFFFFF", otherwise it's the color
},
"warnings": boolean | busWarnings
"marker"?: google.maps.Marker,
"result"?: JQuery // The search result that appears in the sidebar
}
lineInfo.PublishedLineName
is declared as a string
, and String.prototype.includes()
is a function according to MDN, so why does the TypeScript compiler complain about the missing property/method?
lineInfo.PublishedLineName
被声明为 a string
,并且String.prototype.includes()
根据 MDN 是一个函数,那么为什么 TypeScript 编译器会抱怨缺少属性/方法?
回答by hgiasac
You should add es2016 or es7 lib
complierOptions in tsconfig.json. Default TypeScript doesn't support some es6 polyfill functions
您应该在 tsconfig.json 中添加 es2016 或 es7lib
编译器选项。默认 TypeScript 不支持某些 es6 polyfill 函数
{
"compilerOptions": {
...
"lib": [
"dom",
"es7"
]
}
}
Or change build target to es2016 if you no longer want to support ES5 anymore
或者如果你不想再支持 ES5,将构建目标更改为 es2016
{
"compilerOptions": {
...
"target" "es2016"
}
}