Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 422
Michael Kolber Avatar asked Aug 12 '18 17:08

Michael Kolber


People also ask

How do you fix property does not exist on type?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names.

Does not exist on type string TypeScript?

The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string.

Does not exist on type string TS 2339?

To fix the error "TS2339: Property 'x' does not exist on type 'Y'" with TypeScript, we should make sure the properties are listed in the interface that's set as the type of the object. interface Images { main: string; [key: string]: string; } const getMainImageUrl = (images: Images): string => { return images.

Does not exist on type never TypeScript?

The error "Property does not exist on type 'never'" occurs when we try to access a property on a value of type never or when TypeScript gets confused when analyzing our code. To solve the error, use square brackets to access the property, e.g. employee['salary'] .


2 Answers

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"   } } 
like image 55
hgiasac Avatar answered Sep 18 '22 12:09

hgiasac


Add es2016.array.include to compilerOptions.lib

like image 44
Dimitar Nestorov Avatar answered Sep 18 '22 12:09

Dimitar Nestorov