TS2339:类型'string'上不存在属性'includes' [英] TS2339: Property 'includes' does not exist on type 'string'

查看:1989
本文介绍了TS2339:类型'string'上不存在属性'includes'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了关于字符串数组而不是实际字符串的错误。我有一个TypeScript文件,行

I have seen this error mentioned in regards to string arrays but not actual strings. I have a TypeScript file with the line

if (!bus.lineInfo.PublishedLineName.includes(input)) {

这给我一个错误

TS2339: Property 'includes' does not exist on type 'string'.

bus 是一个实现<的变量code> bus interface:

bus is a variable that implements the bus interface:

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 被声明为字符串 String.prototype.includes() 是一个根据MDN的函数,那么为什么TypeScript编译器会抱怨缺少的属性/方法?

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?

推荐答案

您应该在tsconfig.json中添加es2016或es7 lib complierOptions。默认TypeScript不支持某些es6 polyfill函数

You should add es2016 or es7 lib complierOptions in tsconfig.json. Default TypeScript doesn't support some es6 polyfill functions

{
  "compilerOptions": {
    ...
    "lib": [
       "dom",
       "es7"
    ]
  }
}

如果您不再需要支持ES5,请将构建目标更改为es2016

Or change build target to es2016 if you no longer want to support ES5 anymore

{
  "compilerOptions": {
    ...
    "target" "es2016"
  }
}

这篇关于TS2339:类型'string'上不存在属性'includes'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆