When using Typescript with pg-promise, I am facing an issue where I can't import the classes and enums as I normally would. Typically, when working with a library, I import a type, use it, and everything functions properly. However, in the snippet below, I am attempting to create a "mode" for my transactions by importing TransactionMode
and isolationLevel
, but they are showing up as undefined at runtime (despite no errors during design time and compile time). It's worth noting that esModuleInterop
is set to true
in my tsconfig.json
.
import pgPromise, { isolationLevel, TransactionMode } from "pg-promise";
const pgp = pgPromise();
// This workaround seems to resolve the issue, even though it's not clear why it's needed.
// const { TransactionMode, isolationLevel } = pgp.txMode;
// Issue arises here where TransactionMode and isolationLevel are imported but undefined
const mode = new TransactionMode({ tiLevel: isolationLevel.serializable });
(...)
This adds unnecessary complexity to my code, rather than being able to directly utilize the names.