Typescript 从类型脚本调用alertifyjs
我正在尝试将一些js代码迁移到typescript。 特别是我在AlertifyJs方面遇到了麻烦。现在Alertify似乎有很多东西Typescript 从类型脚本调用alertifyjs,typescript,alertifyjs,Typescript,Alertifyjs,我正在尝试将一些js代码迁移到typescript。 特别是我在AlertifyJs方面遇到了麻烦。现在Alertify似乎有很多东西 法比安·d的a 拿起并继续alertifyjs.org 由MohammadYounes alertifyjs.com收集 现在我可以在DefinitelyTyped上找到(1)的d.T,但其他的没有。 我想用(3) 我想以某种方式包装或直接打电话给alertify.confirm(“你去吧”,“女孩”)来自ts 我怎样才能做到这一点 谢谢 PS:vs2015中的
///
///
模块mymodule.Web{
导出类通用工具{
构造函数(){
toastr.options.closeButton=false;
}
showInfo(标题:字符串,消息:字符串){
toastr.info(消息、标题);
}
showConfirm(标题:string,消息:string){
//**代码在这里**
}
}
}
最简单(也是最不安全或优雅)的解决方案可能是将其添加到typescript的全局范围的某个位置
declare var alertify: any;
这样,typescript将知道alertify
变量
interface AlertifyJSStatic {
confirm(a: string, b:string);
}
declare var alertify: AlertifyJSStatic;
如果您更精确,您可以使用您想要使用的那些方法引入自己的(例如)AlertifyJSStatic
接口,并将其作为alertify
变量的类型
interface AlertifyJSStatic {
confirm(a: string, b:string);
}
declare var alertify: AlertifyJSStatic;
有时,快速添加您自己的需求比迷失在
d.ts
库(和版本)中更容易。*我看到了一个公认的答案,但这是我的解决方案
对于我的angular项目,使用npm安装后,在TS文件的导入部分,复制以下内容
import * as alertifyJs from 'alertifyjs';
从这里开始,用法就如您所愿
alertifyJs.confirm('alertify is working');
希望这有帮助。干杯如果您有该库的全局安装,请按以下方式声明
declare global {
interface Window {
alertify: {
success: Function;
[key: string]: Function; // for all others
};
}
}
现在安全使用
window.alertify.success(...)
真是太棒了,非常感谢你。清晰、简洁、简单的回答。我现在想买啤酒!