Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
How to get the `code` property of an error in a `try/catch` using TypeScript 4.4?

I'm trying to get to the code property of an error caught by try/catch like so:

  try {
    const { foo } = await import('./foo');
    // [...]
  } catch (error) {
    if (error instanceof Error) {
      if (error.code === 'MODULE_NOT_FOUND') {
         // [...]
      }
    }
  }

but this gives me the following compilation error:

TSError: ⨯ Unable to compile TypeScript:
src/server.ts:44:17 - error TS2339: Property 'code' does not exist on type 'Error'.

I installed @types/node@16.x.x and my tsconfig.json looks like:

{
  "extends": "@tsconfig/node16/tsconfig.json",
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

but I noticed that Visual Studio Code gets the definition for the Error class from:

C:\Users\kg\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\node_modules\typescript\lib\lib.es5.d.ts

How do I make TypeScript see that there's a code property in Error without having to define my own type or interface?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda