我尝试做一个库的声明文件。
在library.js文件中,有:
if (typeof module !== 'undefined' /* && !!module.exports*/) {
module.exports = Library;
}
Run Code Online (Sandbox Code Playgroud)
我应该在我的library.d.ts文件中放入什么才能在我的代码中导入和使用这个库?
我希望能够做到:
import { Library } from 'library';
const instance = new Library();
Run Code Online (Sandbox Code Playgroud)
正如@Nitzan 所指出的,您需要使用特殊export =和import Library = require语法:
一个完整的例子:
node_modules/library/index.js
module.exports = function(arg) {
return 'Hello, ' + arg + '.';
}
Run Code Online (Sandbox Code Playgroud)
library.d.ts 这个文件名在技术上无关紧要,只有.d.ts扩展名。
declare module "library" {
export = function(arg: string): string;
}
Run Code Online (Sandbox Code Playgroud)
source.ts
import Library = require('library');
Library('world') == 'Hello, world.';
Run Code Online (Sandbox Code Playgroud)
在以下情况下必须使用此语法export =:
import Library = require("library");
Run Code Online (Sandbox Code Playgroud)
更多相关信息请参见:export = 和 import = require()
| 归档时间: |
|
| 查看次数: |
7818 次 |
| 最近记录: |