Question:
How to fix TS2551: Property toSpliced does not exist on type Array

Problem

According to:


>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced


the method .toSpliced() is now available in all major JS platforms. But as you can see here:


>https://www.typescriptlang.org/play?ts=5.3.0-beta#code/MYewdgzgLgBAhgJwTAvDA2gcgB6YDQyYCe+hAXpgLoCwAUKJLIggEyrxIB0UIAygA4AbAJbAApgBMAFAEYCMgJQwA9Mow5SmCjVpA


Even with TS 5.3.0-beta its type is not defined in TypeScript.


Solution

To use toSpliced, you need to set >target to ESNext (for now, until there's an ES2023 setting). Your playground targets ES2017, which wouldn't support toSpliced or anything else from ES2018 onward. You also need to be using a fairly recent version of TypeScript (for instance, >5.1.6 doesn't support it, but >5.2.2 does; I don't know precisely where in there it was added).


Suggested blogs:

>Why Typescript does not allow a union type in an array?

>Narrow SomeType vs SomeType[]

>Create a function that convert a dynamic Json to a formula in Typescript

>How to destroy a custom object within the use Effect hook in TypeScript?

>How to type the values of an object into a spreadsheet in TypeScript?

>Type key of record in a self-referential manner in TypeScript?

>How to get the last cell with data for a given column in TypeScript?

>Ignore requests by interceptors based on request content in TypeScript?

>Create data with Typescript Sequelize model without passing in an id?

>How to delete duplicate names from Array in Typescript?


Nisha Patel

Nisha Patel

Submit
0 Answers