日本語

Fixing TS2550 for toSorted and findLast in TypeScript

​​TypeScript​Troubleshooting,Vite

When you write new array methods such as toSorted or findLast, you may see error TS2550: Property 'toSorted' does not exist on type 'number[]'. Do you need to change your target library? This error appears when the type definitions (lib) selected in tsconfig.json do not include the method. Raising target or lib to the year named in the message (such as es2023) makes it go away.

The same error appears when you use toSorted to sort a copy, findLast to search from the end, or with to make a copy with one element replaced, and the editor shows a red squiggle or npm run build stops. The fixes in this article were each checked by running tsc after the change and confirming that the error disappeared.

Note

Test environment: Ubuntu 24.04, Node.js 22.23.5, npm 10.9.9, TypeScript 7.0.2, create-vite 9.3.4 (Vite 8.3.8). I did not test the editor UI, versions of TypeScript before 7, or running on real older browsers or older Node.js.

Reproducing TS2550 with toSorted

I ran npm init -y in an empty folder and installed TypeScript 7.0.2. The tsconfig.json is below. It does not write lib, and target is es2022.

json
{
  "compilerOptions": {
    "target": "es2022",
    "module": "esnext",
    "moduleResolution": "bundler",
    "strict": true,
    "noEmit": true
  },
  "include": ["src"]
}

I wrote code in src/scores.ts that creates a new array of scores sorted from highest to lowest. src/page.ts shows the result in the page title.

ts
// src/scores.ts
const scores = [72, 95, 88];

export const ranking = scores.toSorted((a, b) => b - a);
ts
// src/page.ts
import { ranking } from "./scores";

document.title = `1位: ${ranking[0]}点`;

Running npx tsc produced three errors, and the exit code was 1.

console
$ npx tsc
src/scores.ts(3,31): error TS2550: Property 'toSorted' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
src/scores.ts(3,41): error TS7006: Parameter 'a' implicitly has an 'any' type.
src/scores.ts(3,44): error TS7006: Parameter 'b' implicitly has an 'any' type.

The first is TS2550. It means: "Property 'toSorted' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later." toSorted was added in ES2023, and while target stays at es2022, TypeScript does not load that type definition.

The other two, TS7006, follow from TS2550. Because toSorted does not exist in the types, the comparison function's a and b have no type and become implicit any. Fixing TS2550 also clears these.

If you don't write lib, raise target

The target description in the tsconfig reference says that changing target also changes the default value of lib. If your tsconfig.json does not write lib, changing target to es2023 is enough.

json
"target": "es2023",

After changing this one line, npx tsc printed nothing and exited with 0. The document in page.ts also no longer errors. When lib is omitted, the defaults include the browser type definitions (dom) (lib description).

If you write lib, fix lib

If your tsconfig.json writes lib, the situation is different. I tested with a tsconfig.json that adds "lib": ["es2022", "dom"].

json
{
  "compilerOptions": {
    "target": "es2022",
    "lib": ["es2022", "dom"],
    "module": "esnext",
    "moduleResolution": "bundler",
    "strict": true,
    "noEmit": true
  },
  "include": ["src"]
}

npx tsc gives the same three errors. Changing only target to es2023 still leaves all three errors, because when you write lib, the lib you wrote is used, not the default derived from target.

To fix it, change lib as the message says. With "lib": ["es2023", "dom"], npx tsc exits with 0 even though target stays at es2022.

Warning

If you remove dom while changing lib, document will no longer be found. With "lib": ["es2023"], TS2550 disappeared, but another error appeared.

console
$ npx tsc
src/page.ts(3,1): error TS2584: Cannot find name 'document'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.

If your code runs in a browser, keep dom in lib.

You can also add only the array method type definitions from ES2023. With "lib": ["es2022", "es2023.array", "dom"], npx tsc exits with 0. es2023 is es2022 plus finer units such as es2023.array (I checked this in the contents of lib.es2023.d.ts in TypeScript 7.0.2).

All the array methods added in ES2023 give the same error

Besides toSorted, findLast, findLastIndex, toReversed, toSpliced, and with also gave TS2550 while target was es2022. All the messages say 'es2023' or later.

ts
const tasks = ["design", "build", "test", "release"];

export const lastShort = tasks.findLast((t) => t.length <= 4);
export const lastShortIndex = tasks.findLastIndex((t) => t.length <= 4);
export const reversed = tasks.toReversed();
export const spliced = tasks.toSpliced(1, 1);
export const replaced = tasks.with(0, "plan");
console
$ npx tsc
src/array2023.ts(3,32): error TS2550: Property 'findLast' does not exist on type 'string[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
src/array2023.ts(3,42): error TS7006: Parameter 't' implicitly has an 'any' type.
src/array2023.ts(4,37): error TS2550: Property 'findLastIndex' does not exist on type 'string[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
src/array2023.ts(4,52): error TS7006: Parameter 't' implicitly has an 'any' type.
src/array2023.ts(5,31): error TS2550: Property 'toReversed' does not exist on type 'string[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
src/array2023.ts(6,30): error TS2550: Property 'toSpliced' does not exist on type 'string[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
src/array2023.ts(7,31): error TS2550: Property 'with' does not exist on type 'string[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.

Running npx tsc --target es2023 exited with 0. The table in the lib description also lists array.with, array.findLast, array.toSorted, and others under ES2023.

A misspelled name gives TS2339

TS2550 happens when the type definitions contain the method but the current lib does not load it. If the name itself is misspelled, the error number changes. I tested by writing toSortd instead of toSorted.

console
$ npx tsc
src/typo.ts(3,31): error TS2339: Property 'toSortd' does not exist on type 'number[]'.
src/typo.ts(3,40): error TS7006: Parameter 'a' implicitly has an 'any' type.
src/typo.ts(3,43): error TS7006: Parameter 'b' implicitly has an 'any' type.

When target was es2022, the error was TS2339 with no hint to change lib. Running with --target es2023 changed it to TS2551, which suggested the correct name.

console
$ npx tsc --target es2023
src/typo.ts(3,31): error TS2551: Property 'toSortd' does not exist on type 'number[]'. Did you mean 'toSorted'?
src/typo.ts(3,40): error TS7006: Parameter 'a' implicitly has an 'any' type.
src/typo.ts(3,43): error TS7006: Parameter 'b' implicitly has an 'any' type.

If the error is TS2339 or TS2551 and there is no Do you need to change your target library? hint, suspect a misspelled name rather than lib.

Array.fromAsync needs esnext

Array.fromAsync creates a new array from async iterables, arrays, and similar sources. Even with target set to es2023, it gave TS2550, and the value the message asked for was esnext.

ts
async function* numbers() {
  yield 1;
  yield 2;
}

export const list = await Array.fromAsync(numbers());
console
$ npx tsc --target es2023
src/fromAsync.ts(6,33): error TS2550: Property 'fromAsync' does not exist on type 'ArrayConstructor'. Do you need to change your target library? Try changing the 'lib' compiler option to 'esnext' or later.

In the TypeScript 7.0.2 type definitions, fromAsync appears only in lib.esnext.array.d.ts. It does not appear in the files from lib.es2015 through lib.es2025. Running with --lib es2023,esnext.array or with --target esnext both exited with 0.

However, the target description says that ESNext refers to the newest version that your TypeScript supports, and that its meaning changes between TypeScript versions, so it should be used with care. The lib description also says that the contents of ESNext change as the JavaScript specification advances. If you only need fromAsync, adding esnext.array instead of esnext narrows what is loaded.

Vite projects also stop on fromAsync

The npm create vite@latest command in the Vite official guide's Getting Started page used create-vite 9.3.4 on the day I tested. The tsconfig.json of its vanilla-ts template looked like this:

console
$ grep -n -e "\"target\"" -e "\"lib\"" tsconfig.json
3:    "target": "es2023",
5:    "lib": ["ES2023", "DOM"],

Methods up to ES2023 pass as they are. With scores.ts placed in src/ as above, npx tsc exited with 0.

On the other hand, when I put fromAsync.ts in place and ran npm run build, it stopped at the tsc step before the build. (The web on lines starting with > is the name of the project I made for testing. During testing I ran it with a sed pipe to replace the build-time display, but I left that out of the command here.)

console
$ npm run build

> web@0.0.0 build
> tsc && vite build

src/fromAsync.ts(6,33): error TS2550: Property 'fromAsync' does not exist on type 'ArrayConstructor'. Do you need to change your target library? Try changing the 'lib' compiler option to 'esnext' or later.

This template writes lib, so the fix is in lib. With "lib": ["ES2023", "ESNext.Array", "DOM"], tsc passed and vite build finished.

Raising lib does not make code run on older runtimes

lib is a setting that chooses the type definitions used for type checking. It does not rewrite code for older runtimes or fill in missing methods. I set target to es2020, lib to es2023 and dom, and emitted JavaScript. toSorted remained as is.

ts
// src/run.ts
const scores = [72, 95, 88];
const ranking = scores.toSorted((a, b) => b - a);
console.log(scores, ranking);
console
$ npx tsc --target es2020 --lib es2023,dom --noEmit false --rootDir src --outDir out && cat out/run.js
"use strict";
const scores = [72, 95, 88];
const ranking = scores.toSorted((a, b) => b - a);
console.log(scores, ranking);

Node.js 22 has toSorted, so node out/run.js printed [ 72, 95, 88 ] [ 95, 88, 72 ]. The original array is unchanged, and a new sorted array was created.

To imitate a runtime without toSorted, I removed Array.prototype.toSorted in Node.js 22 and then loaded the same file. It failed with a runtime error (the path is replaced with /path/to/app).

text
TypeError: scores.toSorted is not a function
    at Object.<anonymous> (/path/to/app/out/run.js:3:24)

The lib description also lists, as a reason to change lib, that the runtime has the API (for example, through a polyfill). Raise lib only when you know that the runtime where the code runs has the method. If the runtime lacks the method, the code fails with a runtime error like the one above, even if you cleared the type error with lib.

​Posted by 86