【发布时间】:2019-09-26 23:07:23
【问题描述】:
有一个nodejs模块是这样的
module.exports = {
init(arg1) {
// ..
return this;
},
// ..
};
我正在尝试编写定义文件。看起来是这样的
declare namespace ModuleName {
function init(smth: string): ModuleName;
}
export = ModuleName;
export as namespace ModuleName;
}
它几乎可以工作。当我导入它时
const a = require('ModuleName');
VSCode 理解 a.init 是一个接受一个字符串参数的函数。但我无法理解结果也有可以调用的方法init。即
const b = a.init('smth');
b.init('smth2');
在这种情况下编写类型定义的正确方法是什么?
【问题讨论】:
-
你必须通过
require而不是import来导入东西吗? -
标签: typescript typescript-typings