从JavaScript调用正确的TypeScript代码 [英] Calling properly TypeScript code from JavaScript

查看:114
本文介绍了从JavaScript调用正确的TypeScript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的大型企业项目中,我们遇到的情况似乎没有在互联网上的文章和帖子中得到很好的描述。

On our big enterprise project we faced a situation that seems not to be very well described in the articles and the posts available in the Internet.

我们需要整合我们现有的JavaScript基础结构代码,支持SPA,其中包含由TypeScript上的其他团队开发的代码。对于许多政治限制和可用的开发资源,我们无法彻底改变方法(即只选择一种语言)。我们完全理解,将两个基础架构集成在一起并不是一个好主意,这些基础架构是用不同语言编写的。

We need to integrate our existing JavaScript infrastructural code that supports SPA with the code that is being developed by the other team on TypeScript. We can’t drastically change the approach (i.e. just pick a single language) for many political limitations and the development resources available. And we fully understand that’s probably it’s not a good idea to integrate two pieces of infrastructure together that are written on different languages.

我们还需要评估影响和最佳实践从JavaScript代码调用TypeScript。

Still we need to evaluate the impact and best practices of calling TypeScript from JavaScript code.

TypeScript基本上被编译成JavaScript的理由似乎很模糊,因为关于如何正确的主题的信息没有可靠的来源使用手写JavaScript编译的JavaScript以及隐藏的警告(或替代方案)。

The justification that TypeScript is essentially compiled into JavaScript seems to be obscure because there are no trustful sources on information on the topic on how to properly consume that compiled JavaScript from handwritten JavaScript and what are the hidden caveats (or alternatives).

当TypeScript代码需要调用JavaScript时,情况似乎相反很好描述。

It also seems that the opposite situation when TypeScript code needs to call JavaScript is surprisingly very well described.

关于这个主题的任何深刻的想法?

Any profound thoughts on the topic?

UPD

具体来说,这是我们现在寻求答案的问题列表:

Specifically here is the list of questions we are seeking the answers now:


  • 什么将是TypeScript API的JavaScript形状广泛使用泛型,类层次结构,接口?

  • 捆绑,缩小,AMD有什么问题吗?

  • 是否可以拥有基本功能用TypeScript编写的Angular控制器和从中继承功能的另一个JavaScript Angular控制器?会有什么警告?

实际上我们认为我们尚未提出所有问题。他们在经过几个小时的思考后才出现。

Actually we think that we haven't surfaced all the questions yet. They've emerged just after a couple of hours of thinking on that topic.

推荐答案

简单地说,如果你必须整合/使用在您自己的项目中使用JavaScript编写的库,使用JavaScript,您将使用已编译的JavaScript API!

Simply said, if you have to integrate/use a library which is written in Typescript in your own project which uses JavaScript, you will use the compiled JavaScript API!

你基本上扔掉了TypeScript带来的一切优于纯JavaScript。

You basically throw away everything which TypeScript brings in terms of benefit over pure JavaScript.

这意味着您不必关心TypeScript的任何特定内容,例如泛型等。您只需要使用TypeScript的编译输出图书馆...

Meaning you don't have to care about anything specific to TypeScript, like generics etc. You only have to work with the compiled output of the TypeScript library...

举个例子,请访问​​ http:// www .typescriptlang.org / Playground
选择演练:泛型。在右侧,您应该看到已编译的JavaScript。它没有任何泛型或任何特殊的东西,它仍然是纯JavaScript。那就是你要处理的......

To give you an example, go to http://www.typescriptlang.org/Playground Select "Walkthrough: Generics". On the right you should see the compiled JavaScript. It has no generics or anything special, it still is pure JavaScript. That's what you have to deal with...

你的具体问题:


  • 什么是广泛使用泛型,类层次结构,接口的TypeScript API的JavaScript形状? 见上文。它将是简单的旧Javascript。没有区别。

  • 捆绑,缩小,AMD有什么问题吗? 不,因为编译后的TypeScript是纯JavaScript和可以缩小等...

  • 是否可以使用TypeScript编写基本的Angular控制器,以及从中继承其功能的另一个JavaScript Angular控制器?会有什么警告?
    当然,您可以使用已编译的JavaScript执行任何操作。

  • what will be the JavaScript shape of the TypeScript API that extensively uses generics, class hierarchies, interfaces? See above. It will be plain good old Javascript. No difference for you.
  • are there any issues with bundling, minification, AMD? No, because compiled TypeScript is plain JavaScript and can be minified etc...
  • is it possible to have the basic Angular controller written in TypeScript and the other JavaScript Angular controller that inherits the functionality from it? What will be the caveats? Yes of course you can do whatever you want with the compiled JavaScript.

使用Typescirpt编译的JavaScript的唯一缺点是,你丢弃了TypeScript可以给你的强大功能,比如... types ...
如果其他团队已经在那条路线上,您可能还想考虑在TypeScript中编写您的部分;)

The only disadvantage of using the compiled JavaScript from Typescirpt is, that you throw away the awesome features TypeScript can give you, like... types... If the other team is already on that route, you may want to consider to write your part in TypeScript as well ;)

这篇关于从JavaScript调用正确的TypeScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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