// Old code when having "type": "commonjs" in package.json and "module": "commonjs" in tsconfig.json
import { UndirectedGraph } from 'graphology'Now changing to "type": "module" in package.json causes following error report:
import { UndirectedGraph } from 'graphology';
^^^^^^^^^^^^^^^
SyntaxError: Named export 'UndirectedGraph' not found. The requested module 'graphology' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'graphology';
const { UndirectedGraph } = pkg;Doing this suggested fix makes UndirectedGraph be any type. However the type cannot be imported like
import type { UndirectedGraph } from 'graphology'
import pkg from 'graphology';
const { UndirectedGraph } = pkg; // ERROR! Cannot shadow variableSo we're left with importing the type as some other name
import type { UndirectedGraph as UndirectedGraphType} from 'graphology'
import pkg from 'graphology';
const { UndirectedGraph } = pkg;
const graph: UndirectedGraphType = new UndirectedGraph() // ERROR! Unsafe cast anySo to have the type come to the imported actual runtime variable we need to do something like:
import type { UndirectedGraph as UndirectedGraphType} from 'graphology'
import pkg from 'graphology';
const { UndirectedGraph }: { UndirectedGraph: typeof UndirectedGraphType } = pkg;
const graph: UndirectedGraphType = new UndirectedGraph()Is this really the only way of replacing this one line import in the CommonJS world when transitioning to ESM?