Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeScript definitions for TS 4.5 nodenext mode #15

Closed
wants to merge 1 commit into from

Conversation

FossPrime
Copy link

@FossPrime FossPrime commented Oct 25, 2021

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
Copy link
Author

Updated with a sandbox that highlights the problem.

@tex0l tex0l requested a review from arantes555 October 27, 2021 07:24
@FossPrime
Copy link
Author

FossPrime commented Oct 27, 2021

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 }

@FossPrime FossPrime changed the title Use CommonJs export for CommonJs package Fix TypeScript definitions for TS 4.5 nodenext mode Oct 27, 2021
@arantes555
Copy link

arantes555 commented Oct 28, 2021

@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
Copy link
Author

FossPrime commented Nov 17, 2021

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

@FossPrime FossPrime closed this Nov 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants