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?
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.
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.
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.
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'] .
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" } }
Add es2016.array.include
to compilerOptions.lib
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With