Using 'export =' instead of 'export default' in main typescript definition for fixing #1121 #1184
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
At the moment if you are using TS and importing web3 with:
import Web3 from 'web3';
=> TS definitions work but you getTypeError: web3_1.default is not a constructor
in runtime.import Web3 = require('web3');
orimport * as Web3 from 'web3';
=> the code works but TS definitions doesn't work and you end up witherror TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
in TS compilation.The cause for this is TS expects a ES6 module definitions inside the
index.js
file. Although it's returning the class itself through CommonJS (this is the standard node behaviour, normally preprocessors like Babel can work around this but in TS it's optional and can cause some problems with environments which are not using any loaders). With this fix the typescript definitions point out the right output type (which is the class not the module) therefore it can be imported properly by usingimport Web3 = require('web3');
. I've tried to make it work asimport * as Web3 from 'web3';
which is more appropriate but typescript doesn't allow classes as direct and throws outerror TS2309: An export assignment cannot be used in a module with other exported elements.
TL;DR TS doesn't allow exporting a class as the module hence creating various problems with a module compiled in CommonJS but imported as ES6. With this PR it's possible to use all the type definitions with the
import Web3 = require('web3');
import instruction just like mentioned in #1121.