我有一个简单的外部项目(https://www.npmjs.com/package/undo-manager),我想创建一个环境声明(这显然是错误的):
declare module "undo-manager" {
interface UndoManager {
setCallback(f:Function):void;
undo():void;
redo():void;
hasUndo():boolean;
hasRedo():boolean;
add(cmd:Command):void;
}
interface Command {
redo():any;
undo():any;
}
interface UndoManagerFactory {
new(): UndoManager;
}
}
我的问题是如何更改声明,以便我可以像这样使用撤消管理器
var UndoManager = require('undo-manager');
// some more imports may be needed to get Command, but how?
undoManager = new UndoManager();
function addCmd(cmd: Command) {
undoManager.add(cmd);
}
addCmd({
redo: () => doSomething(),
undo: () => undoSomething()
});
答案 0 :(得分:2)
定义文件应为
declare module "undo-manager" {
module UndoManager {
export interface Command {
redo(): any;
undo(): any;
}
}
class UndoManager {
setCallback(f: Function): void;
undo(): void;
redo(): void;
hasUndo(): boolean;
hasRedo(): boolean;
add(cmd: UndoManager.Command): void;
}
export = UndoManager
}
以这种方式使用
import * as UndoManager from 'undo-manager'
const um = new UndoManager()
function addCmd(cmd: UndoManager.Command) {
um.add(cmd)
}
两条评论:
1- UndoManager
中可用的公共方法似乎多于定义文件
2-既然我们在这里,你可以将这个定义文件贡献给DefinitelyTyped(或者更好的是让代码作者维护它),这将是很好的。