直接从 TypeScript 调用 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12710962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling JavaScript directly from TypeScript
提问by mvbaffa
I have just downloaded the TypeScript documentation. I have a some JavaScript classes and I would like to create and use these class in a TypeScript test application
我刚刚下载了 TypeScript 文档。我有一些 JavaScript 类,我想在 TypeScript 测试应用程序中创建和使用这些类
How can I call a JavaScript function of an included JavaScript from TypeScript. I do not want to create a "d.ts" file. Just to create my classes, call its methods, access its properties.
如何从 TypeScript 调用包含的 JavaScript 的 JavaScript 函数。我不想创建“d.ts”文件。只是为了创建我的类,调用它的方法,访问它的属性。
How do I do that?
我怎么做?
I am trying to use kendoUI with TypeScript.
我正在尝试将 kendoUI 与 TypeScript 一起使用。
For instance to show a window I have to do:
例如要显示一个窗口,我必须这样做:
- Have a HTML to represent the content of a window. I have a div with an id logonDialog. This div
is initially hidden; - I create the window: logonDlg.kendoWindow(logOnParams);
- Then using jQuery I show the div: using
logonDlg.show();
- 有一个 HTML 来表示一个窗口的内容。我有一个带有 id logonDialog 的 div。这个 div
最初是隐藏的; - 我创建窗口:logonDlg.kendoWindow(logOnParams);
- 然后使用jQuery我显示div:使用
logonDlg.show();
Example
例子
var logonDlg = $("logonDialog");
if (!logonDlg.data("kendoWindow")) {
logonDlg.kendoWindow(logOnParams);
logonDlg.show();
}
It is working OK. The JS is generated as I want but I receive an error since The property 'kendoWindow' does not exist on value of type 'JQuery'.
它工作正常。JS 是按我的意愿生成的,但我收到一个错误,因为“JQuery”类型的值上不存在“kendoWindow”属性。
How can I disable this kind of error. I could not make, what Ryan said, to work.
我怎样才能禁用这种错误。正如瑞安所说,我无法工作。
采纳答案by joshuapoehls
You just do it. TypeScript won't stop you. You will see warnings in the compiler output but tsc
will generate your JS file just fine.
你就去做吧。TypeScript 不会阻止你。您将在编译器输出中看到警告,但tsc
会生成您的 JS 文件就好了。
回答by Ryan Cavanaugh
If you want to stop the errors without doing much else extra work, you can 'declare' the objects from your JS code:
如果你想在不做很多其他额外工作的情况下停止错误,你可以从你的 JS 代码中“声明”对象:
declare var w; // implicit type here is 'any'
// (later, anywhere in your file...)
var x = new w(); // you can do whatever you want with w now without getting errors
w.x = 4; // etc.
回答by mvbaffa
There is a better solution. Just cast the jQuery logonDlg to any like this:
有一个更好的解决方案。只需将 jQuery logonDlg 转换为任何这样的:
(<any>logonDlg).kendoWindow(logOnParams);
The code will be a bit different but will work the same.
代码会有所不同,但工作原理相同。
- Without the cast th generated code is like this: logonDlg.kendoWindow(logOnParams);
- With the cast will be like this: (logonDlg).kendoWindow(logOnParams);
- 没有强制转换生成的代码是这样的:logonDlg.kendoWindow(logOnParams);
- 用演员表会是这样:(logonDlg).kendoWindow(logOnParams);
Both work OK.
两者都工作正常。
Regards
问候
回答by CodingWithSpike
In this particular case you could just include the Kendo UI TypeScript definitionsthat Telerik provides.
在这种特殊情况下,您可以只包含Telerik 提供的Kendo UI TypeScript 定义。
(I realize this question was asked back when the TS definitions probably didn't exist yet, but wanted to add this detail for people who come across this question in the future.)
(我意识到当 TS 定义可能还不存在时,这个问题被问回了,但想为将来遇到这个问题的人添加这个细节。)