bcoin-org / bcoin

Javascript bitcoin library for node.js and browsers
https://bcoin.io
Other
3.01k stars 811 forks source link

Typescript definitions missing #714

Open v3rbalgit opened 5 years ago

v3rbalgit commented 5 years ago

Hi. I was trying to take a look at this library, but it's really hard to work with when there are no typescript definitions on DefinitelyTyped or elsewhere for that matter. There is a nice documentation from JSDoc, but it would be awesome to have types for VS Code Intellisense and autocompletion. Most other libraries like bitcoinjs-lib or bitcore-lib have types. Is there a way to generate types from the source files or how would I go about using this library in VS Code with Intellisense? When I do

const bcoin = require('bcoin').set('testnet');

it doesn't provide any suggestions on the bcoin object. Thanks!

pinheadmz commented 5 years ago

If you're using latest master branch from GitHub, suggestions should work now: https://github.com/bcoin-org/bcoin/commit/0e93cbaec4f5f8f0b814c5fa75ac834aa1621cea

At least, hitting tab in REPL mode. Not sure about VS Code.

There's been discussions on our slack about converting to typescript but I think the task for a library this size is just too enormous.

v3rbalgit commented 5 years ago

I'm on the latest branch, but still can't get the suggestions to work. I tried loading the library in Webstorm, and there most suggestions work out of the box. Although not every instantiated object seem to bear expected methods. For example, in the code from the website, in guide on wallets and accounts, Webstorm will underline the open method in await wdb.open(); or create in const wallet = await wdb.create(); or fromHD in const jdWalletKey0 = WalletKey.fromHD(jdAccount, jdPrivateKey0, branch, index); etc. and warn about an unresolved method or function.

ss

Anyway, I don't own Webstorm, just wanted to try and check Intellisense for this library in another editor. I'm sure building types for a library this size would be a grueling task. But I feel like this is a very important library for JS developers. It has all the bells and whistles as the original C++ implementation and as such it is the best tool to start developing on bitcoin. I'm sure I'm not the only one who would appreciate it immensely. For what it's worth for the past 3 days I have been trying to make myself a sort of faux .d.ts files following the structure from the individual source files (starting with the blockchain module) but for the love of me, I can't get the modules to expose themselves the right way. The only way I can think of is to put everything in one big index.d.ts file. I'm not sure I should be doing this though, as I'm not that familiar with the library and I'm not a fulltime developer let alone a TS expert. All I'm looking for is to expose all the methods and properties of this library so that I can use it with ease and learn bitcoin programming along the way.

nodech commented 5 years ago

I am not sure if you have tried to configure jsconfig or something like that, I am not familiar with VSCode much. But after couple of experiments I think I was able to achieve what you wanted.

image

image

This is vim + tsserver setup with super simple jsconfig.json:

{
  "compilerOptions": {
        "allowJs": true,
        "module": "commonjs"
  },
    "include": [
        "./**/*.js"
    ]
}

Current downside being, require('bcoin') will give you wrapper that caches includes, so you only load whatever is necessary (that's why you will notice I am including whatever I need directly)

Solution: While this was a workaround on the problem, I believe we can describe jsdoc on lib/bcoin and list all the @propertys bcoin has, you are welcome to open PR listing correct properties on lib/bcoin.

v3rbalgit commented 5 years ago

This is so strange. I did exactly the same thing as you in VS Code but it doesn't work for me: bcoin-test This is what my tsconfig json file looks like: { "compilerOptions": { "module": "commonjs", "allowJs": true, "noEmit": true, }, "include": [ "./**/*.js" ] } The TS compiler raises no errors. So if it works in vim, and it (sort of) works in Webstorm, then there must be a problem with VS Code or something trivial I am missing. It looks like VS Code needs the .d.ts files and won't read the JSDoc syntax out of the box. Can someone with VS Code please chip in how to get this working? It is driving me insane!

nodech commented 5 years ago

I think you have tsconfig.json instead of jsconfig.json, that could be the issue ? and you are working with js file instead of ts

v3rbalgit commented 5 years ago

I tried jsconfig first but it has the same effect, the editor also complained about the allowJS setting then.

thomaseizinger commented 5 years ago

The next typescript release (3.7), with the final release on November 5th is expected to ship --allowJs in combination with --declaration.

If my understand is correct, this would allow us to setup a typescript project within bcoin, enable allowJs and have tsc automatically emit an initial set of declarations for the project!

JoePotentier commented 3 years ago

Bump.

pinheadmz commented 3 years ago

@JoePotentier this is very likely never going to happen. bcoin has no active maintainers. If this is a feature you really want, you may need to write it yourself. 🤷‍♂️