我正在 typescript 中声明一个模块:
declare module 'myweb' {
export default class MyWeb {
constructor(url: string);
}
}
当我通过 import MyWeb from 'myweb';
导入它时,出现错误 TypeError: myweb_1.default is not a constructor
。
它被转译为:
const myweb_1 = require("myweb");
...
new myweb_1.default(url);
我觉得这似乎是对的。
模块中还有其他导出的元素,因此我无法使用export =
有什么想法吗?谢谢。
编辑:
javascript本身是一个我无法更改的库,但代码是:
var MyWeb = function MyWeb() {
var _this = this;
processParams(this, arguments);
}
module.exports = MyWeb;
最佳答案
关于运行时错误,这里为您从 official documentation on modules 开始关于如何处理 export =
。
When exporting a module using
export =
, TypeScript-specificimport module = require("module")
must be used to import the module.
因此,您的导入
需要是这样的:
import MyWeb = require('myweb');
关于类型定义,如果不了解更多有关模块结构的信息,就很难提供指导。例如,当您说“模块中还有其他导出的元素...”时,您的意思是什么?
关于typescript - 使用自己的 Typescript 类型时,默认值不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60519136/