DevZoom7
DevZoom7

Reputation: 533

How to use toSorted() method in typescript

When I try to use the toSorted() method in Typescript like this:

const x = [1,2];
const y = x.toSorted();

I got this ts error:

Property 'toSorted' does not exist on type 'number[]'.ts(2339)

This also happen with toReverse() and toSplice()` methods

Upvotes: 53

Views: 32356

Answers (7)

auburn
auburn

Reputation: 31

If your tsconfig.json file looks something like this:

{
  "files": [],
  "references": [
    { "path": "./tsconfig.app.json" },
    { "path": "./tsconfig.node.json" }
  ]
}

where it has a references key that points to other files, you should add the compilerOptions.lib configuration described in other answers to the referenced files (e.g. tsconfig.app.json) instead of this file directly.

Upvotes: 0

Freewalker
Freewalker

Reputation: 7355

Updating Node version fixed this for me. toSorted is only available from Node 20+.

nvm install 21

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

Upvotes: 0

Roman T
Roman T

Reputation: 190

Update your project's TypeScript to version higher than 5.2 (e.g. yarn add typescript@lastest)

Optionally you can try adding into tsconfig.ts file "lib": ["ESNext.Array", "dom", "dom.iterable", "esnext"]

Upvotes: 3

ivanavitdev
ivanavitdev

Reputation: 2918

To resolve this, you'll want to update your tsconfig.json file.

Find the compilerOptions section and make sure the compilerOptions value includes ESNext.Array. It should look something like this:

{
  "compilerOptions": {
    "lib": ["ESNext.Array", "DOM", "ESNext", "etc..."]
  }
}

Upvotes: 42

Cornelius
Cornelius

Reputation: 51

I ran into the same issue, because my TS version was not up-to-date. Since updating the TS version was no solution for me at the moment, I used a workaround. For anyone else who is looking for the functionality of toReversed() (or similar new Array functions) without a TS-version update, the below might help.

I used the spread syntax to get a reversed copy of my array:

const array = [1, 2, 3, 4]

const reversedArray = [...array].reverse()

console.log(reversedArray)
// [ 4, 3, 2, 1 ]

console.log(array)
// [ 1, 2, 3, 4 ]

This website was my source, it includes more ways to sort (and copy) arrays.

Upvotes: 4

Alex Petrychuk
Alex Petrychuk

Reputation: 139

Find your tsconfig file and add "ES2023.Array" to "lib" value. I got a similar issue today (for "toSpliced") and solution works for me

Upvotes: 13

Nikita
Nikita

Reputation: 832

toSorted is a new feature Array.prototype.toSorted() and it exists in ES2023 https://www.sonarsource.com/blog/es2023-new-array-copying-methods-javascript/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted#browser_compatibility

This feature are really fresh and it is not supported each browser and code editor. Here you can find an example(JS) https://playcode.io/1521165 or you can use the latest version of TS(today 5.2.0-dev.20230701)

You can use methods of TypedArray with Array object. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

const x = new Uint8Array([1,2]);
const y = x.toSorted();

Or you can use a standard sort method

const numbersArray = [45, 2, 6, 207];
numbersArray.sort((a: number, b: number) => {
  return a - b;
});

Demo https://stackblitz.com/edit/typescript-d4dfdn?file=index.ts

Upvotes: 11

Related Questions