Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Fix the types of the BN and RLP re-exports #270

Merged
merged 2 commits into from
Aug 3, 2020
Merged

Conversation

alcuadrado
Copy link
Member

@alcuadrado alcuadrado commented Aug 1, 2020

This PR fixes #267.

Here's an explanation of what I think was wrong, how I fixed, and how it can be simplified in the future.

  1. BN and RLP are distributed as CJS modules, and use the module.exports = pattern. This is ok in CJS, but doesn't make sense in ESM. module.exports is not the same as export default.

  2. TS has a special syntax to do that, export = obj.

  3. BN and RLP typings use that syntax.

  4. To import modules using that syntax, you have to use import A = require('A') in ts.

  5. This module was using import * as A from 'A' instead, which means something like "import the module namespace" in ESM. Not that module namespace is not a class in ESM, so ts gets confused if you try to initialize it.

  6. This worked anyway, as we are just reexporting it, so no error was triggered.

  7. When a TS project tries to use it, ts complains about this error that went unnoticed here.

  8. This make this CSJ/ESM compatibility mess easier, TS introduced the esModuleInterop flag, which let you use import A as 'A' to import CSJ modules. This breaks the ESM semantics a bit, but it's a great trade off.

  9. As I wrote in a TODO, we are gonna enable that flag in a future version of the ts config package, so this change can be simplified in the future.

@alcuadrado alcuadrado mentioned this pull request Aug 1, 2020
@coveralls
Copy link

coveralls commented Aug 1, 2020

Coverage Status

Coverage remained the same at 99.711% when pulling fa5801f on fix-ts-reexports into ba3e344 on master.

@ryanio
Copy link
Contributor

ryanio commented Aug 1, 2020

thanks for the explanation :)

As I wrote in a TODO, we are gonna enable that flag in a future version of the ts config package, so this change can be simplified in the future.

could we just add esModuleInterop to this repo's tsconfig and keep the export the same? that would skip the need for adding a future TODO. or is it better to use this import/require syntax

@alcuadrado
Copy link
Member Author

I tried your suggestion. It didn't work. I'm not sure why tbh

Copy link
Contributor

@ryanio ryanio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that's weird, ok this LGTM for now then!

@holgerd77 holgerd77 merged commit 126a895 into master Aug 3, 2020
@holgerd77 holgerd77 deleted the fix-ts-reexports branch August 3, 2020 19:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BN re-export failure
4 participants