ide / await-lock

Mutex locks for async functions
MIT License
89 stars 12 forks source link

AwaitLock is not a constructor when importing in Typescript #32

Open kasbah opened 1 year ago

kasbah commented 1 year ago

This is similar to #5 but I'm not using require but Typescript with moduleResolution: "node". https://github.com/kasbah/await-lock-bug-repro

git clone https://github.com/kasbah/await-lock-bug-repro && cd await-lock-bug-repro
yarn
yarn tsc
node example.js
TypeError: AwaitLock is not a constructor
    at file:///home/kaspar/projects/await-lock/repro/example.js:2:14
    at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
    at async DefaultModuleLoader.import (node:internal/modules/esm/loader:228:24)
    at async loadESM (node:internal/process/esm_loader:40:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12)
kasbah commented 1 year ago

It may be related to https://stackoverflow.com/questions/63189533/how-can-i-solve-error-this-expression-is-not-constructable and the hint from TS docs:

Note that ES6 modules cannot directly export class objects.

kasbah commented 1 year ago

A sort of workaround is to set target to ES5 in compiler options.

...
  "compilerOptions": {
    "noEmitOnError": false,
    "target": "ES5",
...

This does give a compilation error

node_modules/await-lock/build/AwaitLock.d.ts:5:5 - error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.

5     #private;
      ~~~~~~~~

But the emitted code actually works.