typescript - 使用自己的 Typescript 类型时,默认值不是构造函数

标签 typescript typescript-typings

我正在 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-specific import module = require("module") must be used to import the module.

因此,您的导入需要是这样的:

import MyWeb = require('myweb');

关于类型定义,如果不了解更多有关模块结构的信息,就很难提供指导。例如,当您说“模块中还有其他导出的元素...”时,您的意思是什么?

关于typescript - 使用自己的 Typescript 类型时,默认值不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60519136/

相关文章:

typescript - 在定义文件 (*d.ts) 中导入类

typescript 对象类型到数组类型

Polymer v3 的 typescript 支持

reactjs - typescript 和响应文件上传(打字)

javascript - 从一个组件更改变量或从另一个组件调用函数

typescript - 如何强制两种类型在 TypeScript 中不兼容?

angular - 我如何使用 catchError() 并仍然返回带有 rxJs 6.0 的类型化 Observable?

typescript - Pick<> 在函数返回类型检查中不起作用?

arrays - 从对象数组中获取值

typescript - VS Code 中类似 WebStorm 的 TypeScript 方法覆盖