Error TS2550: Property ‘includes’ Does Not Exist on Type ‘String’ in TypeScript

In this tutorial, I will explain how to fix the error TypeScriptExamples.ts:4:14 – error TS2550: Property ‘includes’ does not exist on type ‘string’. Do you need to change your target library? Try changing the ‘lib’ compiler option to ‘es2015’ or later.

Error TS2550: Property ‘includes’ Does Not Exist on Type ‘String’ in TypeScript

When working with TypeScript, you might encounter the following error:

TypeScriptExamples.ts:4:14 - error TS2550: Property 'includes' does not exist on type 'string'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

This error occurs when TypeScript does not recognize the includes() method on a string variable. The includes() method was introduced in ES6 (ES2015), and if your TypeScript compiler is set to an older ECMAScript version, it won’t recognize the method.

Example Code That Causes the Error

I got this error while checking if a string contains a substring in TypeScript.

let cityName: string = "New York City";
let substring: string = "York";

if (cityName.includes(substring)) {
    console.log(`${substring} is found in ${cityName}`);
} else {
    console.log(`${substring} is not found in ${cityName}`);
}

When you compile this code using an older TypeScript configuration, you might see the TS2550 error because includes() is not recognized.

You can follow the screenshot below for the complete error message:

error ts2550 property includes does not exist on type string

Why Does This Error Occur?

This error is caused when TypeScript is compiled with a target ECMAScript version lower than ES2015. The includes() method was introduced in ES6 (ES2015), so if your compiler target is set to ES5 or earlier, TypeScript does not recognize includes() as a valid method on strings.

Solutions to Fix the Error

Here are the possible solutions to resolve this error:

1. Update Your TypeScript Configuration (tsconfig.json file)

To support the includes() method, you need to update the lib setting in your tsconfig.json file to include ES2015 or later. Do the following:

  1. Open your tsconfig.json file (or create one if it doesn’t exist).
  2. Locate the compilerOptions section.
  3. Ensure that the lib setting includes ES2015 or later.

Updated tsconfig.json File:

{
  "compilerOptions": {
    "target": "es2016",
    "lib": ["ESNext", "DOM"],
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true
  }
}

2. Restart the TypeScript Compiler

If you updated tsconfig.json but the error persists, restart the TypeScript compiler:

tsc --build

Or, if using VS Code, restart the TypeScript server:

  • Press Ctrl + Shift + P
  • Search for “TypeScript: Restart TS Server”
  • Click it to restart the TypeScript server

3. Upgrade Your TypeScript Version

Ensure you’re using an up-to-date version of TypeScript. Run the following command to check your TypeScript version:

tsc -v

If it’s outdated, upgrade it using:

npm install -g typescript

4. Use indexOf() Instead of includes() (Alternative Fix for Older Versions)

If you’re working in an environment where you cannot update TypeScript or change tsconfig.json, you can use indexOf() instead of includes():

if (cityName.indexOf(substring) !== -1) {
    console.log(`${substring} is found in ${cityName}`);
} else {
    console.log(`${substring} is not found in ${cityName}`);
}

This works in older JavaScript environments because indexOf() has been supported for a long time.

Conclusion

The TS2550 error occurs when TypeScript does not recognize includes() on a string, usually due to an outdated tsconfig.json configuration. To fix it:

  1. Ensure lib is set to ES2015 or later in tsconfig.json.
  2. Restart the TypeScript compiler or server.
  3. Upgrade TypeScript to the latest version.
  4. Use indexOf() as an alternative if you cannot update TypeScript.

By following these steps, you can resolve the error and use includes() without any issues.

You may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.