I am currently working on code that utilizes better-sqlite3 and my goal is to convert it to typescript.
The original javascript code includes the following relevant sections:
import Database from "better-sqlite3";
/**
* @param {string} filename
* @return {Database}
*/
function openDb(filename)
{
let db;
db = new Database(/* some arguments */);
// some initialization
return db;
}
Once I transferred the code into a .ts file and added types, this is how it appeared:
import Database from "better-sqlite3";
function openDb(filename: string) : Database
{
let db : Database;
db = new Database(/* some arguments */);
// some initialization
return db;
}
An error
TS2709: Cannot use namespace 'Database' as a type.
occurs in all instances where the Database
symbol is used as a type.
If I utilize typescript 3.8's import type
, then the error flips: I can now use the symbol as a type, but not as a constructor.
Is there any way to import this symbol in a manner that typescript recognizes it BOTH as a constructor AND as a type? Is there a solution that will allow me to convey in code the intuitive thought process of "I invoke new Something()
to create a Something
and then I return that Something
", rather than the complicated flow of thought "I invoke new Something()
to create a SomethingUnderADifferentName
and then I return that SomethingUnderADifferentName
"?