是否有将 JavaScript 文件转换为 TypeScript 的工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14412164/
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
Is there a tool to convert JavaScript files to TypeScript
提问by Gisway
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 文件转换为 TypeScript。
采纳答案by lhk
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:
我担心这会很成问题。TypeScript 是 JavaScript 的超集。这意味着所有有效的 JavaScript 代码也是有效的 TypeScript 代码。这是错误的。以下代码在 JavaScript 中是正确的,但在 TypeScript 中会产生错误:
var data={x:5, y:6};
data.z=5;
You can get the dynamic behaviour of JavaScript by declaring data as "ambient"
您可以通过将数据声明为“环境”来获取 JavaScript 的动态行为
var data:any={x:5, y:6};
data.z=5;
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:
现在这也适用于 TypeScript。不过,您可以将 .js 文件的扩展名更改为 .ts 并将此文件传递给 TypeScript 编译器。这真的让我很困惑,我在 freenode 上的 TypeScript IRC 频道中提出了这个问题。事实证明,Typescript 编译器会检查其输入中是否存在有效的 JavaScript,并且在发现某些内容时不执行任何操作。直觉上这听起来不错。您应该能够通过仅更新文件扩展名来将所有 JavaScript“转换”为 Typescript,并且一切顺利。不幸的是,这不能按预期工作。让我们假设你有这个代码:
var func=function(n){alert(n)};
var data={x:5};
data.z=6;
Edit: I've just tried to compile this and it doesn't work. I'm sorry, I seem to have been mistaken: The TypeScript compiler doesn't even accept pure JavaScript. I'll try to find out what went wrong.
编辑:我刚刚尝试编译它,但它不起作用。抱歉,我好像弄错了:TypeScript 编译器甚至不接受纯 JavaScript。我会尽力找出问题所在。
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:
您已将文件扩展名更改为 .ts 并在其他代码中使用函数 func 。起初一切似乎都很好,但随后出现了一个丑陋的错误。您决定某些静态类型检查可能会对您有所帮助,因此您将代码更改为:
var func=function(n:string){alert(n)};
var data={x:5};
data.z=6;
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 代码,并且会导致编译器生成大量错误消息。毕竟数据没有成员“z”。...如果默认类型始终为“any”,则可以避免这种行为,但类型推断将完全无用。
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
那么你如何解决这个问题呢?答案是:声明文件。您可以在项目中“按原样”使用 JavaScript 代码,并且仍然可以借助一些 .d.ts 文件进行静态类型检查。它们通知编译器在 JavaScript 文件中声明了哪些变量、方法和“类”,包括它们的类型。因此,它们允许编译时类型检查和出色的智能感知。我的示例的 .d.ts 文件将是
declare var data:any;
declare function func(n:string);
Save this as "myjsfile.d.ts" and import it to typescript with
将其另存为“myjsfile.d.ts”并将其导入到打字稿中
///<reference path="myjsfile.d.ts"/>
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 脚本包含在已编译的打字稿上方的 html 文件中。这是一个有趣的链接。您可能对“将 JavaScript 转换为 TypeScript”部分感兴趣。
回答by Shkredov S.
Update: Tool is described in details in this post
更新:这篇文章中详细描述了工具
ReSharper starting with version 9 (EAP builds available) has functionality to automatically convert JavaScript code into TypeScript. You can start with renaming .js file into .ts and see R# suggestions like this one:
从版本 9(EAP 版本可用)开始的 ReSharper具有将 JavaScript 代码自动转换为 TypeScript 的功能。您可以从将 .js 文件重命名为 .ts 开始,然后查看 R# 建议如下:


Haven't tried it on real world examples. But it is capable of converting large number of JavaScript patterns.
还没有在现实世界的例子中尝试过。但它能够转换大量的 JavaScript 模式。
回答by 7zark7
From terminal (mac):
从终端(mac):
for f in *.js; do mv $f `basename $f .js`.ts; done;
Renames all .js files to .ts
将所有 .js 文件重命名为 .ts
This is somewhat tongue-in-cheek - but as Benjamin points out above JavaScript files are valid TypeScript. If you want to add types, classes, interfaces, etc to your JavaScript - you will yourself need to design and change your code using these concepts- there is little/no automatic conversion for this.
这有点诙谐 - 但正如 Benjamin 在上面指出的那样,JavaScript 文件是有效的 TypeScript。如果您想向 JavaScript 添加类型、类、接口等 - 您自己需要使用这些概念设计和更改代码- 很少/没有自动转换。
With GNU findutils (most *nixes) + Bash, just do:
使用 GNU findutils(大多数 *nixes)+ Bash,只需执行以下操作:
find -type f -name '*.js' -exec bash -c 'mv "" "${1%???}.ts"' bash {} \;

