1

我正在尝试将一些 js 代码迁移到打字稿。具体来说,我在使用 AlertifyJs 时遇到了麻烦。现在 Alertify 似乎有很多东西。

  1. fabien-d已停止的项目
  2. 拿起并继续 alertifyjs dot org
  3. 由 MohammadYounes alertifyjs dot com 获取

现在我可以在 DefinedlyTyped 上找到 (1) 的 d.ts,但其他的都找不到。我想使用(3)!

我想以某种方式包装或直接调用alertify.confirm("you go", "girl"); 来自 ts。

我怎样才能做到这一点?

谢谢

PS:在vs2015中编码

编辑:我想我会为你漂亮的书呆子提供一段我得到的代码片段

///<reference path="../typings/jquery/jquery.d.ts"/>
///<reference path="../typings/toastr/toastr.d.ts"/>

module mymodule.Web {
export class CommonTools {

    constructor() {
        toastr.options.closeButton = false;
    }

    showInfo(title: string, message: string) {
        toastr.info(message, title);
    }
    showConfirm(title: string, message: string) {
     //**code goes here**
    }
}
}
4

4 回答 4

3

最简单(也是最不安全或优雅)的解决方案可能是将其添加到您的打字稿的全局范围内。

declare var alertify: any;

像这样,打字稿会知道这个alertify变量。

AlertifyJSStatic如果您更精确,您可以使用您想要使用的那些方法引入您自己的(例如)接口,并将其作为alertify变量类型。

interface AlertifyJSStatic {
  confirm(a: string, b:string);
}

declare var alertify: AlertifyJSStatic;

有时,快速添加自己的需求比迷失在d.ts库(和版本)中更容易。

于 2016-07-26T06:59:37.833 回答
0

*我看到有一个公认的答案,但这是我的解决方案。

对于我的 Angular 项目,使用 npm 安装后,在 TS 文件的导入部分中,复制以下内容...

import * as alertifyJs from 'alertifyjs';

从那里开始,用法就如您所愿。

alertifyJs.confirm('alertify is working');

希望这可以帮助。干杯

于 2017-11-10T17:48:11.037 回答
0

如果您有库的全局安装,请像这样声明它

declare global {
  interface Window {
    alertify: {
      success: Function;
      [key: string]: Function; // for all others
    };
  }
}

现在安全使用

window.alertify.success(...)
于 2019-02-21T10:07:01.910 回答
0

根据Zoltán Tamási的回答,我创建了一个界面,帮助我解决了 TypeScript 中的编译错误:

// globals/alertify.type.js

interface AlertifyJsStatic {
    alert (title: any, message: any, onok?: string);
    confirm (title: any, message: any, onok?: any, oncancel?: any);
    prompt (title: any, message: any, value?: string, onok?: any, oncancel?: any);
    notify (message: string, type?: string, wait?: number, callback?: any);
}

declare var alertify: AlertifyJsStatic;

我通过Triple-Slash 指令在任何使用的地方引用它

/// <reference path="globals/alertify.type.ts" />
于 2021-10-25T12:36:22.400 回答