-
Notifications
You must be signed in to change notification settings - Fork 5k
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
TypeScript ESM - Web3 has no construct signatures #5543
Comments
A temporary patch. This NOT work for CommonJS users! diff --git a/package.json b/package.json
index 114dcb5aedd67d4841b1bb68ab80e273f1d05871..e1713914fd51de471b09770a6ea644a07e4aff9c 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,7 @@
"API"
],
"author": "ethereum.org",
- "types": "types/index.d.ts",
+ "types": "types/index.d.mts",
"scripts": {
"compile": "tsc -b tsconfig.json",
"dtslint": "dtslint --localTs ../../node_modules/typescript/lib types",
diff --git a/types/index.d.ts b/types/index.d.mts
similarity index 100%
rename from types/index.d.ts
rename to types/index.d.mts |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment. |
I encountered the same problem. Looks like it is caused by Web3 type exported as named export from .d.ts and actual Web3 module is exported as default. So adding |
As a temporary workaround for people using the library, without having to edit content in node_modules or other, one can declare custom types in project with something like this: declare module "web3-eth-contract" {
import {Contract} from "web3-eth-contract/types/index.js";
export * from "web3-eth-contract/types/index.js";
export default Contract;
} Putting the above in a file (for example {
"compilerOptions": {
"typeRoots": [
"./node_modules/@types",
"./types"
]
},
"files": [
"types/web3-eth-contract.d.ts"
]
} (the This is unlikely to break if/when the above PR gets accepted and published, allow reproducible behavior for now, and can be easily removed later. |
@alex-mccreary Thanks, |
@shrpne
I would highlight that, the And I would like also to point out that unfortunately Thanks, |
Well, looks like my PR indeed does not affect the author's issue. My issue was the same but about separate packages: Thanks for merging |
For anyone who might still be facing this issue, please try to use // @ts-ignore before the line when you call the constructor on the imported Web3 object. So, try to use as in the following snippet: import Web3 from "web3";
// @ts-ignore
const web3 = new Web3("http://localhost:8546");
console.log(web3); And if the above solution does not work. Kindly provide a full but minimal code to replicate the issue, and share the repository on GitHub or on one of the cloud editors (like https://codesandbox.io/s/summer-sea-5rvxxg?file=/src/index.ts.) |
Hello @Jack-Works Actually, in all cases, as mentioned in #5543 (comment), this issue would not be present in the next version (4.x) and it is already fixed there.
|
the migration guide is empty... |
Is there an existing issue for this?
Current Behavior
Following the Usage with TypeScript documentation results in error for ESM projects.
This expression is not constructable. Type 'typeof import("c:/Git/web3-esm-test/node_modules/web3/types/index")' has no construct signatures.ts(2351)
Expected Behavior
The new instance of Web3 is created.
Steps to Reproduce
package.json
tsconfig.json
test.ts
Web3.js Version
1.8.0
Environment
Anything Else?
No response
The text was updated successfully, but these errors were encountered: