如何在现有的 JavaScript 系统中慢慢迁移到/迁移到 TypeScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13349579/
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
How to slowly move to / migrate to TypeScript in an existing JavaScript system
提问by Greg Balajewicz
We have an already existing JavaScript system.
我们有一个已经存在的 JavaScript 系统。
What we want to do:We would like to start to integrate TypeScript into the current system; We cannot just move everything to TypeScript. We want to slowly start writing some of the new modules in TypeScript.
我们想要做什么:我们想开始将 TypeScript 集成到当前系统中;我们不能把所有东西都移到 TypeScript 上。我们想慢慢开始用 TypeScript 编写一些新模块。
What we tried:We use a pattern for organizing our JS code simillar to the MODULE TypeScript construct.
我们的尝试:我们使用类似于 MODULE TypeScript 构造的模式来组织我们的 JS 代码。
We tried to rewrite a simple class/object in TypeScript and were successful but we had trouble accessing JS functions defined in our code, in other files.
我们尝试在 TypeScript 中重写一个简单的类/对象并成功,但我们无法访问在我们的代码中定义的 JS 函数,在其他文件中。
Problems Encountered:We had to create dummy interfaces, and dummy functions using those interfaces etc.
遇到的问题:我们必须创建虚拟接口,以及使用这些接口的虚拟函数等。
So the question: can anyone comment, what would be the best approach to slowly integrate TypeScript into an existing JavaScript system.
所以问题是:任何人都可以评论一下,将 TypeScript 慢慢集成到现有 JavaScript 系统中的最佳方法是什么。
回答by Fenton
It sounds like you have a sensible plan. Here are my observations!
听起来你有一个明智的计划。以下是我的观察!
It is best to make sure you TypeScript depends on your JavaScript and not the other way around where possible. This is because it is very easy to refactor TypeScript using the Visual Studio tools, but it won't refactor JavaScript that calls your TypeScript.
这是最好的,以确保您的打字稿取决于你的JavaScript,而不是周围的其他方法在可能的情况。这是因为使用 Visual Studio 工具重构 TypeScript 非常容易,但它不会重构调用 TypeScript 的 JavaScript。
You will have to write definition files for the parts of your JavaScript you need to call from TypeScript. You will need to balance the cost of writing a definition with the cost of simply converting the JavaScript to TypeScript.
您必须为需要从 TypeScript 调用的 JavaScript 部分编写定义文件。您需要平衡编写定义的成本和简单地将 JavaScript 转换为 TypeScript 的成本。
If you are only calling one function, just write the definition for that one function - don't write a definition until you need it. This will keep the cost of calling your old code lower.
如果您只调用一个函数,只需为该函数编写定义 - 在需要之前不要编写定义。这将降低调用旧代码的成本。
You could also temporarily use the any
type to get away with calling anything on your JavaScript code. When you convert the file to TypeScript you will get better type checking. This is an "it depends" decision point. Rather than spending ages writing a definition, you could save the time at the cost of type checking.
您还可以暂时使用该any
类型来避免调用 JavaScript 代码上的任何内容。当您将文件转换为 TypeScript 时,您将获得更好的类型检查。这是一个“视情况而定”的决策点。无需花费大量时间编写定义,您可以以类型检查为代价来节省时间。
For example...
例如...
declare var MyExistingClass: any;
You can now call...
你现在可以打电话...
var example = new MyExistingClass.Anything();
example.anythingYouLike();
You have to decide as a team whether this is acceptable or if you want to write definitions:
您必须作为一个团队来决定这是否可以接受或是否要编写定义:
declare class MyExistingClass {
anythingYouLike(): void;
}
I hope this helps.
我希望这有帮助。
回答by duncan
This is something we have recently faced moving an HTML5 game engine of about 100,000 lines of JavaScript to TypeScript. We necessarily had to do it in stages, starting by just renaming the files from .js to .ts and gradually proceeding from there. The full description is here for anyone that is interested:
这是我们最近面临的将大约 100,000 行 JavaScript 的 HTML5 游戏引擎迁移到 TypeScript 的问题。我们必须分阶段进行,首先将文件从 .js 重命名为 .ts,然后逐渐从那里开始。任何有兴趣的人都可以在这里找到完整的描述:
http://hardcodeded.blogspot.jp/2013/02/mostly-painlessly-migrating-3d-game.html
http://hardcoded.blogspot.jp/2013/02/mostly-painless-migrating-3d-game.html