是否有工具将JavaScript文件转换为TypeScript [英] Is there a tool to convert JavaScript files to TypeScript

查看:2960
本文介绍了是否有工具将JavaScript文件转换为TypeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在TypeScript问世了,对我来说这是一个令人兴奋的消息,但是如何将所有现有的JavaScript文件转换为TypeScript。

Now TypeScript came out, it is an exciting news for me, but how can I convert all the existing JavaScript files to TypeScript.

推荐答案

我担心这会很成问题。 TypeScript是JavaScript的超集。这意味着所有有效的JavaScript代码也是有效的TypeScript代码。这是错的。以下代码在JavaScript中是正确的,但在TypeScript中创建错误:

I fear this will be very problematic. TypeScript is a superset of JavaScript. This implies that all valid JavaScript code is also valid TypeScript code. Which is just wrong. The following code is correct in JavaScript, but creates an error in TypeScript:

var data={x:5, y:6};
data.z=5;

您可以通过将数据声明为环境来获取JavaScript的动态行为

You can get the dynamic behaviour of JavaScript by declaring data as "ambient"

var data:any={x:5, y:6};
data.z=5;

现在这也适用于TypeScript。不过,您可以将.js文件的扩展名更改为.ts,并将此文件传递给TypeScript编译器。这让我很困惑,我在freenode上的TypeScript IRC频道中提出了这个问题。事实证明,Typescript编译器检查其输入是否有效,并且在发现某些内容时不会做任何事情。直观地说这听起来不错。您应该能够通过更新文件扩展名将所有JavaScript转换为Typescript并且很好。不幸的是,这不能按预期工作。假设你有这个代码:

Now this will work in TypeScript, too. Nevertheless you can change the extension of a .js file to .ts and pass this file to the TypeScript compiler. This really confused me and I asked the question in the TypeScript IRC channel on freenode. It turned out that the Typescript compiler checks its input for valid JavaScript and just doesn't do anything in case it finds something. Intuitively this sounds good. You should be able to "convert" all your JavaScript to Typescript by merely updating the file extensions and be good to go. Unfortunately this doesn't work as expected. Let's assume you've got this code:

var func=function(n){alert(n)};

var data={x:5};
data.z=6;

编辑:
我刚刚尝试编译它并且它没有'工作。对不起,我好像错了:TypeScript编译器甚至不接受纯JavaScript。我会尝试找出问题所在。

您已将文件扩展名更改为.ts并在其他代码中使用函数func。起初一切似乎都很好,但随后出现了一个丑陋的虫子。您决定某些静态类型检查可能会帮助您并将代码更改为:

You've changed the file extension to .ts and use the function func in your other code. At first everything seems fine but then an ugly bug turns up. You decide that some static type checking would probably help you and you change the code to:

var func=function(n:string){alert(n)};

var data={x:5};
data.z=6;

但现在这不再是有效的JavaScript代码,会导致编译器生成大量错误消息。在所有数据都没有成员z之后。 ...
如果默认类型总是任何,则可以避免这种行为,但是类型推断将完全没用。

But now this is no longer valid JavaScript code and will cause the compiler to generate lots of error messages. After all data has no member "z". ... This behaviour could be avoided if the default type would always "any" but then type inference would be utterly useless.

那么如何你解决了这个问题吗?
答案是:声明文件。
您可以在项目中按原样使用JavaScript代码,并在几个.d.ts文件的帮助下仍然可以进行静态类型检查。它们通知编译器在JavaScript文件中声明了哪些变量,方法和类,包括它们的类型。因此,它们允许编译时类型检查和梦幻般的智能感知。我的示例的.d.ts文件将是

So how do you solve this problem ? The answer is: declaration files. You can use your JavaScript code "as is" in the project and still get static type checking with the help of a few .d.ts files. They inform the compiler what variables, methods and "classes" are declared in a JavaScript file, including their types. Therefore they allow for compile-time type checking and fantastic intellisense. A .d.ts file for my example would be

declare var data:any;
declare function func(n:string);

将其保存为myjsfile.d.ts并将其导入打字稿

Save this as "myjsfile.d.ts" and import it to typescript with

///<reference path="myjsfile.d.ts"/>

请确保在编译的打字稿上方的html文件中包含JavaScript脚本。
这是有趣的链接。您可能对将JavaScript转换为TypeScript部分感兴趣。

be sure to include the JavaScript script in your html file above the compiled typescript. Here's an interesting link. You might be interested in the section "Turning JavaScript into TypeScript".

这篇关于是否有工具将JavaScript文件转换为TypeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆