2

I'm declaring a module in typescript:

declare module 'myweb' {
  export default class MyWeb {
    constructor(url: string);
  }
}

When I import it by import MyWeb from 'myweb'; I get error TypeError: myweb_1.default is not a constructor.

It gets transpiled to:

const myweb_1 = require("myweb");
...
new myweb_1.default(url);

It seems right to me.

There are also other exported elements in module so I can't use export =

Any ideas? Thanks.

EDIT:

The javascript itself is a library I can't change, but the code is:

var MyWeb = function MyWeb() {
    var _this = this;
   processParams(this, arguments);
}

module.exports = MyWeb;
8
  • If you also have an implementation, could you please post the implementation of myweb too. That would help us find the type declaration to match it. Commented Mar 4, 2020 at 5:07
  • Hi, pls see the edit in the question above. Thanks Commented Mar 4, 2020 at 7:19
  • The JavaScript library that you showed does not export anything. That could be the problem. You said there "are other exported elements in module." Can you please show where, if at all, the module does an export of MyWeb. Commented Mar 4, 2020 at 23:00
  • You are right, I was missing it here, I have added it to the last line. The javascript library is exporting module.exports = MyWeb Commented Mar 4, 2020 at 23:31
  • Since it is using module.exports = MyWeb, the module is exporting one and only one element. What makes you think there are other exported elements in the module? Commented Mar 4, 2020 at 23:33

1 Answer 1

2

Regarding the run-time error, here is a start for you from the official documentation on modules about how to handle export =.

When exporting a module using export =, TypeScript-specific import module = require("module") must be used to import the module.

As a result, your import needs to be this:

import MyWeb = require('myweb');

Regarding the type definitions, it is hard to provide guidance without seeing more about the module's structure. For instance, what do you mean when you say, "There are also other exported elements in module..."?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.