我尝试做一个库的声明文件。
在 library.js
文件中有:
if (typeof module !== 'undefined' /* && !!module.exports*/) {
module.exports = Library;
}
我应该在我的 library.d.ts
文件中放入什么才能在我的代码中导入和使用这个库?
我希望能够做到:
import { Library } from 'library';
const instance = new Library();
最佳答案
正如@Nitzan 所指出的,您需要使用特殊的 export =
和 import Library = require
语法:
export = and import = require()
一个完整的例子:
node_modules/library/index.js
module.exports = function(arg) {
return 'Hello, ' + arg + '.';
}
library.d.ts
这个文件名在技术上并不重要,只有 .d.ts
扩展名。
declare module "library" {
export = function(arg: string): string;
}
source.ts
import Library = require('library');
Library('world') == 'Hello, world.';
关于typescript - 相当于 Typescript 声明文件中的 module.exports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41496915/