seald / nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typescript Error: 'typeof import ("xxxxxxxxxxxx/node_modules/@seald-io/nedb/index") has no construct signatures'

develmts opened this issue · comments

Describe the bug
It seems that typescript it's unable to find the constructor ( or constructor declaration) for the Datastore

To Reproduce
Gist

Expected behavior
To be able to Transpile it

Actual behavior
Doesn't transpile cleanly

Logs

src/db.ts:17:28 - error TS2351: This expression is not constructable.
 Type 'typeof import("xxxxxxxxxxxxx/node_modules/@seald-io/nedb/index")' has no construct signatures.

    this.aColl[coll] = new Datastore({ filename: dbfile, autoload: true });
                              ~~~~~~~~~

Found 1 error in src/db.ts:17

Environment info

  • Typescript
  • Version 5.0.3
  • Windows 7 ULtimeate 6.1 build 7601

Additional context
Maybe has something to do with de fact that index.js just reexports /lib/datastore ..... only an idea , I'm not consider myself an expert

I am sorry but we are not seeing this behaviour. How do you import the library ?

exactly as gist shows

I am sorry, i just tried your gist, and I don't get any error...

Ok, I manage to reproduce after a few tweaks to your gist. Looking into it !

@develmts looks like the problem is coming from the "moduleResolution": "nodenext" line in the tsconfig.json : changing that to "moduleResolution": "node" makes it work, as a workaround.

I will look into making it work for this case.

experiencing this here, too

@arantes555 this is because you're declaring a namespace and the datastore by the same name: Nedb.

Edit: Okay, so my thought was to put the class into the namespace: new Nedb.DataStore(...) but that won't work with the current structure of exports of vanilla js.

Can I ask the silly question? Is there value in re-writing Nedb purely in typescript?

@RobMayer I believe @tex0l is already working on a major rewrite in TS to avoid these kind of problems, and have a cleaner codebase overall.

sweet. How can I help?

@RobMayer Sorry, I was mistaken, actually he is only rewriting the import/exports to ES Modules.

This is unfortunate. I need to use NodeNext as my module resolution, because I need top -level awaits. Is there another potential workaround? Can I work around it with a custom d.ts file perhaps?

I've submitted a PR that I believe fixes this, but I've not seen any feedback or action taken.

hackey workaround in the meantime, assuming esModuleInterop is true

import { default as Nedb } from "@seald-io/nedb";
const { default: Store } = Nedb;

const myDB = new Store({...});

No, it turns out that work-around doesn't work... frustrating.

commented

Hi all,
I hoped to fix all these issues with the rewrite in TS I am working on, but it takes longer than expected, making it backwards-compatible and having it properly typed in the internals is a nightmare.
I'll take a look at these issues shortly and will try to provide you guys with a patch.

are you looking to rewrite the entire thing in TS or rewrite the declaration file?

my offer to assist still stands if it'd be useful.

commented

right now, as you are all stuck with issues with the declaration file, my goal is to provide a patch as quickly as possible, as soon as I can take a look.

in parallel, I'm working on a full rewrite in TS.

That's very awesome of you - as I said: open offer to help in any way possible.