Bitcoin-com / bitbox-sdk

BITBOX SDK for Bitcoin Cash
https://developer.bitcoin.com/bitbox
MIT License
89 stars 62 forks source link

Typescript fix for Randombytes #175

Open rvdende opened 4 years ago

rvdende commented 4 years ago

Hi

In a new typescript project when using bitbox-sdk by npm i bitbox-sdk:

import { BITBOX } from 'bitbox-sdk'
let bitbox = new BITBOX();
bitbox.Mnemonic.generate()

When using tsc to compile the typescript it reports these errors:

C:\Users\rouan\Desktop\work\tempbitbox>tsc test.ts
node_modules/bitbox-sdk/lib/Crypto.ts:1:8 - error TS1259: Module '"C:/Users/rouan/Desktop/work/tempbitbox/node_modules/@types/randombytes/index"' can only be default-imported using the 'esModuleInterop' flag

1 import randomBytes from "randombytes"
         ~~~~~~~~~~~

  node_modules/@types/randombytes/index.d.ts:10:1
    10 export = randomBytes;
       ~~~~~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

node_modules/bitbox-sdk/lib/Mnemonic.ts:5:8 - error TS1259: Module '"C:/Users/rouan/Desktop/work/tempbitbox/node_modules/@types/randombytes/index"' can only be default-imported using the 'esModuleInterop' flag

5 import randomBytes from "randombytes"
         ~~~~~~~~~~~

  node_modules/@types/randombytes/index.d.ts:10:1
    10 export = randomBytes;
       ~~~~~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

Found 2 errors.

I found that this works around the issue.

christroutner commented 4 years ago

Thank you for the PR. I'm not a user of Typescript, so I'm not really sure how to evaluate this. I'll try to see if any of the other committers for this repo have the prerequisite knowledge to evaluate this PR.

SpicyPete commented 4 years ago

Thank you for bringing this to our attention. To get around this issue quickly I recommend adding "esModuleInterop": true to your TypeScript configuration file.
We will look into resolving all of the TS issues and improving the TS coverage of this library very soon.

rvdende commented 4 years ago

Thanks for the feedback. I'm quite sure I tried with "esModuleInterop": true and still had the same problem, so I figured it is worth fixing in this package so others won't get the same issue.

rvdende commented 4 years ago

Any chance to get this merged?

elderapo commented 4 years ago

Is there any blocker as why this hasn't been merged yet? esModuleInterop set to true breaks some other libraries and I'd like to avoid using it if possible.