Search code examples
node.jstypescriptlambdaerror-handlingarchitect

How can I declare global Error type in typescript NodeJs Architect project?


Context

Hi, I have a NodeJs+Ts+Architect setup for building and deploying lambda functions. Architect uses typescript plugin to compile typescript. I am trying to use Error class to throw errors.

However, Typescript is picking up Error type from /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/lib.es5.d.ts.

In the image below, please note the constructor signature only accepts message field. And the error interface does not have an options object either. Please look at Browser Error Class or NodeJs Error Class to see the signatures.

enter image description here

Node Error has the following constructor signature and Error interface.

enter image description here

Problem

  • Getting TS Error for trying to use constructor signature of Node Error as Typescript is reading Error type from lib.es5.d.ts which accepts only 1 argument enter image description here

Possible Solutions which I know

  • Declare global Error type ( Need help here. Since Architect is compiling TS using its plugin, I am not able to declare and override Error interface )
  • Use your own Error class

I hope the question made sense. Would appreciate if there is a nicer way to solve this, but I am not getting ample discussions on Architect+Ts+NodeJs.


Solution

  • The cause option was introduced in ES2022. You would need to use at least that version for your lib types.

    Change the lib version in your tsconfig.json to:

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