TS2551: Property toSpliced does not exist on type Array – Typescript

by
Ali Hasan
react-typescript

Quick Fix: To resolve this issue, upgrade to a version of TypeScript that supports ESNext or set the target to ESNext in your project settings.

The Solutions:

Solution 1: Set Target to ESNext

To use `toSpliced`, you need to set the `target` option to `ESNext` in your TypeScript compiler options. This is because `toSpliced` is a new feature that is not yet part of the ECMAScript standard, but is available in some JavaScript engines.

You can set the `target` option in your `tsconfig.json` file, or by passing the `–target` flag to the TypeScript compiler.

Once you have set the `target` option to `ESNext`, you should be able to use `toSpliced` without getting an error.

Here’s an example of how to set the `target` option to `ESNext` in your `tsconfig.json` file:

{
  "compilerOptions": {
    "target": "ESNext"
  }
}

Or, here’s an example of how to set the `target` option to `ESNext` using the `–target` flag:

tsc --target ESNext

After making these changes, you should be able to use `toSpliced` without getting an error.

Solution 1: Update typescript version and set target

To resolve the error “TS2551: Property toSpliced does not exist on type Array”, Update your TypeScript version to 5.2 or later and set the “target” compiler option in your tsconfig.json file to “ES2023” or “ES2023.Array”. Here’s an example tsconfig.json configuration:

{
  "compilerOptions": {
    "target": "ES2023",
    "lib": ["ES2023.Array"]
  }
}

This will enable the latest ECMAScript features, including the toSpliced() method, and should resolve the error.

Q&A

What’s the method to use .toSpliced()?

Set target to ESNext and use a recent version of TypeScript.

What’s the target to use .toSpliced()?

Use ES2023 or ES2023.Array in lib.