Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

Calling properly TypeScript code from JavaScript

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.

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.

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

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).

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:

  • what will be the JavaScript shape of the TypeScript API that extensively uses generics, class hierarchies, interfaces?
  • are there any issues with bundling, minification, AMD?
  • 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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Consuming TypeScript from JavaScript is the same as consuming TypeScript from TypeScript, or JavaScript from JavaScript for that matter. For example, let's say you have a function in TypeScript:

function f(n: number) { return 'the number is ' + n; }

To call this function from TypeScript, you would write

var x = f(42);

To call this function from JavaScript, you would write

var x = f(42);

Let's say you have a class in TypeScript:

class MyClass { /* ... */ }

To use this class from TypeScript, you would write

var c = new MyClass();

To use this class from JavaScript, you would write

var c = new MyClass();

In other words, it's exactly the same. No guidance has been given because none is needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...