在 TypeScript 中使用 Raphael JavaScript 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12769582/
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
Use Raphael JavaScript library in TypeScript
提问by daniel
Is it possible to use JavaScript libraries in TypeScript?
是否可以在 TypeScript 中使用 JavaScript 库?
For example I want to use Raphaelin TypeScript and added the JS files in my /scripts folderand added them to _references.js.
例如,我想在 TypeScript 中使用Raphael并将 JS 文件/scripts folder添加到my并将它们添加到_references.js.
But when I want to declare in my TS file:
但是当我想在我的 TS 文件中声明时:
var r = Raphael(10,50,640,480);
Intellisense always says:
智能感知总是说:
Raphael does not exist in the current scope.
Raphael does not exist in the current scope.
and the TS file does not compile.
并且 TS 文件无法编译。
回答by Boris Yankov
This line is an ambient declaration:
这一行是一个环境声明:
declare var Raphael: any;
While it works, you get no real typing.
虽然它有效,但你没有真正的打字。
The project DefinitelyTypedalready has definitions for Raphael.
项目DefinitelyTyped已经有Raphael 的定义。
Download the raphael.d.tsfile.
下载raphael.d.ts文件。
And use it like this:
并像这样使用它:
/// <reference path="../Definitions/raphael.d.ts" />
回答by kunjee
I support the previous answer, declare Raphael as var with type any and it will work. But if you seriously like to take benefit of type script, then create declaration file for Raphael js. So, that will provide intellisense whenever you use Raphael js. Here is linkfor Jquery declaration file which is available in the samples provided by typescript site. Just have a look it will help. Please let me know if any further details required.
我支持之前的答案,将 Raphael 声明为类型为 any 的 var 并且它会起作用。但是如果你真的喜欢利用类型脚本,那么为 Raphael js 创建声明文件。因此,每当您使用 Raphael js 时,它都会提供智能感知。这是Jquery 声明文件的链接,可在 typescript 站点提供的示例中找到。只要看看它会有所帮助。如果需要任何进一步的细节,请告诉我。
回答by Mike Keesey
As previously, noted, you can just declare Raphael as a var of "any" type, but if you want IntelliSense and compile-time checking, you'll need a declaration file. I've gone ahead and taken an initial stab at this here: https://bitbucket.org/keesey/raphaelts
如前所述,您可以将 Raphael 声明为“任何”类型的 var,但如果您想要 IntelliSense 和编译时检查,则需要一个声明文件。我已经在这里进行了初步尝试:https: //bitbucket.org/keesey/raphaelts

