While working with older javascript code, I stumbled upon the following snippet:
// module1.js
class Class {
constructor() { console.log('hello') }
}
const exported = {
Class: Class,
}
module.exports = exported
This code is then referenced in a TypeScript file like this:
// module2.ts
import Module1 from './module1'
class Subclass extends Module1.Class {
...
However, Visual Studio Code throws an error and provides the following type hint:
(alias) const Module1: {
Class: typeof Class;
}
import Module1
----------------------------------------
Cannot find namespace 'Module1'. ts(2503)
Curiously, after adding parentheses around 'Module1' like so:
class Subclass extends (Module1).Class {
The error in Visual Studio Code disappears. What does this error mean, and what do those parentheses signify? I couldn't find any information on namespaces, import aliases, etc.
It's worth noting that there are no compilation errors when using tsc; the code functions correctly either way.
Some relevant sections from the tsconfig file (Node.js, TypeScript 4.8.2):
"target": "es2019",
"module": "commonjs",
"allowJs": true,
"checkJs": false,
"esModuleInterop": true,
Note: The issue persists even if the file is imported via
import Module1 = require('./module1')
, so it seems unrelated to esModuleInterop
.