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?