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

查看:75
本文介绍了从 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.

我们需要将支持 SPA 的现有 JavaScript 基础结构代码与其他团队在 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 的项目中集成/使用一个用 Typescript 编写的库,你将使用编译后的 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!

就纯 JavaScript 的优势而言,您基本上抛弃了 TypeScript 带来的一切.

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 做任何您想做的事情.

使用从 Typescirpt 编译的 JavaScript 的唯一缺点是,你丢弃了 TypeScript 可以给你的很棒的功能,比如......类型......如果其他团队已经在这条路线上,您可能还需要考虑用 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天全站免登陆