seald / nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser
MIT License
351 stars 32 forks source link

Fix TypeScript definitions for TS 4.5 nodenext mode #15

Closed FossPrime closed 2 years ago

FossPrime commented 2 years ago

The previous definition only worked because of an optional compatibility layer that makes CommonJS look like native ESM.

Workaround

sed -i '13s/export default Nedb/export = Nedb/' ./node_modules/@seald-io/nedb/index.d.ts
FossPrime commented 2 years ago

Updated with a sandbox that highlights the problem.

FossPrime commented 2 years ago

Found another workaround

import { default as Datastore } from '@seald-io/nedb' // currently works
// wont work, Datastore is an Object with a Datastore property
// But our types restrict access to that
// import Datastore from '@seald-io/nedb'

I still hold that this PR is the best solution to the issue. It causes the index.js default to work as default under TS.

Alternative solution, that makes Nedb available to TS ESM users, but exposes a non existent Datastore property to all CJS users.

export default Nedb;
export { Nedb as Datastore }
arantes555 commented 2 years ago

@rayfoss I am sorry but I am not very clear on what the problem is.

I just tried a quick demo of using NeDB with TypeScript, and it seems to work fine with import Datastore from '@seald-io/nedb', with the current typings, as soon as I add the needed "esModuleInterop": true in tsconfig, because the package is in CJS format.

I tried reading through your sandbox, but between ts-node, nodemon, http stuff, ..., i could not really understand what does not work.

EDIT: Also, it does not work with your change if I do not put the esModuleInterop.

FossPrime commented 2 years ago

TypeStrong is kicking the can on ESM support... probably till 4.6 or 4.7, or maybe even 5 :(