Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

new Error(message, { cause }) is rejected by TypeScript

ErrorObject literal may only specify known properties, and 'cause' does not exist in type

Short answer

The cause option belongs to the ES2022 error typings. If tsc rejects new Error(msg, { cause }) or err.cause, your lib setting is older than ES2022. Set target to ES2022 or newer, or add es2022 to an explicit lib list, and restart the type checker or watcher.

  • Applies to TypeScript 5.9, Node.js 22, ts-node
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which target and lib the compiler is really using, including values inherited from a base config.

    npx tsc --showConfig | grep -i -E '"(target|lib)"'
  2. 2Match what you see, then jump to its fix

    1. 1lib is set (for example ["es2020"]) and the error shows on the Error constructorAn explicit lib list stops below ES2022Fix
    2. 2No lib is set and target is ES5, ES2015 or similar, or unsettarget is old, so the default lib is oldFix
    3. 3Reading err.cause in a catch block reports that cause does not existThe declarations in es2022.error add cause to every error typeFix

What does the error look like?

  • Object literal may only specify known properties, and 'cause' does not exist in type
  • Expected 0-1 arguments, but got 2.
  • Property 'cause' does not exist on type 'Error'.

Cause 1 of 3

An explicit lib list stops below ES2022

1Check

The ErrorOptions type with cause lives in the es2022.error library file. A lib list that does not include it, directly or through a newer entry, hides it.

grep -n '"lib"' tsconfig.json

2Fix

Add ES2022 or newer to lib, keeping the other entries you need.

{
  "compilerOptions": {
    "lib": ["ES2022"]
  }
}

3Verify

Type-check the file that uses cause.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 3

target is old, so the default lib is old

1Check

Changing target also changes the default value of lib, and TypeScript includes APIs matching the target. The default target is ES5 unless module is node20 or nodenext.

npx tsc --showConfig | grep -i target

2Fix

Raise the target to ES2022 or newer, or keep your target and set lib as in the first cause.

{
  "compilerOptions": {
    "target": "ES2022"
  }
}

3Verify

Type-check again. With ts-node or nodemon, restart the process so it reads the new config.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 3

The declarations in es2022.error add cause to every error type

1Check

The same library file declares cause on the Error interface, typed unknown, and gives the Error constructors an optional options argument.

grep -rn "cause" node_modules/typescript/lib/lib.es2022.error.d.ts

2Fix

After lib or target includes ES2022, read the value as unknown and narrow it before use.

try {
  run();
} catch (e) {
  if (e instanceof Error && e.cause instanceof Error) {
    console.error(e.cause.message);
  }
}

3Verify

Type-check the catch block.

npx tsc --noEmit

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Pick target and lib from the oldest Node.js version you deploy to, and write both down in tsconfig.
  • Restart ts-node, nodemon or the editor's TypeScript server after changing tsconfig.
  • Wrap errors with new Error(msg, { cause }) so the original stack is kept.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

cdk synth says --app is required

--app is required either in command-line, in cdk.json or in ~/.cdk.json

The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.

3 causes, each with a fix

Back end

Mocha or Node cannot find ts-node/register

Error: Cannot find module 'ts-node/register'

Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.