28

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)) {

This gives me an error of

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

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 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?

Michael Kolber
  • 1,309
  • 1
  • 14
  • 23

2 Answers2

46

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

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

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

{
  "compilerOptions": {
    ...
    "target" "es2016"
  }
}
hgiasac
  • 2,183
  • 16
  • 14
3

Add es2016.array.include to compilerOptions.lib

Dimitar Nestorov
  • 2,411
  • 24
  • 29