Home > database >  How to configure all exceptions to 'any' type
How to configure all exceptions to 'any' type

Time:10-22

Recently on my typescript project, all catch blocks suddenly started to give the following Typescript compíle error:enter image description here

I dont know if it was some update on the config file, but how can I configure Typescript to consider all catched exceptions as any type instead of unknown

CodePudding user response:

Did you upgrade TypeScript recently?

Starting in TypeScript 4.4, catch variables are now defaulted to the unknown type. This can be controlled with the --useUnknownInCatchVariables option.

You can cast it as any if you really want to, but as always, this is considered a bad practice in TypeScript.

} catch(error: any) {
  if (!error.response.data.error) return
  ...
}

More information about this can be found in the PR that implemented this feature.

  • Related