From the Blogosphere
To let other TypeScript libraries use your library, you need to create a .d.ts file to declare all your public APIs
By: AppDynamics Blog
Nov. 2, 2015 04:00 PM
By Raphael Feng
This one will focus on more details of the benefits and one missing feature in TypeScript compiler we suggest to implement.
TypeScript's main benefits:
Class and Module Support
You can define a class as
TypeScript compiler will transcompile it to
A practical example is if we use the wrong data type in our browser agent beacon, we now get compiling errors. Before migrating to Typescript, they could only be found by testing against the back-end.
ECMAScript 6 Feature Support
With TypeScript, you can start using many ES6 features although it may not be supported in your target browser. TypeScript compile can compile the ts files into "ES3", "ES5" or "ES6".
Some of the ES6 features are very handy like:
would be compiled to
Refer to TypeScript ES6 Compatibility Table for more ES6 features you can use.
Clear API Definition
Running tsc (the TypeScript compiler) with the -out option, the compiler will concatenate all the directly or indirectly referred files into one js file in the order they are referred.
So we can easily tailor our library into multiple versions. For example, from the same code base, we can generate specific versions of browser agent for desktop browser and mobile browser with specific features for different devices. We just need to create a main entry file for each version with the files for specific features referred in it.
Syntax Similarity to Our Backend Languages (Java, Scala)
Refer http://www.slideshare.net/razvanc/quick-typescript-vs-scala-sample for a quick syntax comparison between TypeScript and Scala.
One Missing Feature Suggested
One of them is to merge the same module into the same function rather than multiple functions.
generates below code with compiling error "cannot find name ‘foo'".
foo function defined within the first anonymous function call for module A is not visible in the second anonymous function call, so you have to export it as:
generates below code without error:
The problem here is now A.foo is not only visible to module A. Anyone can call it and modify it now.
There is no module level visible concept which should be similar to Java's "package-private" when there is no modifier for Java classes or members.
This could be solved by generating:
The problem of merging into one function is a potential name conflict between the same module in two files. But the compiler can report error in this case, and if two people are working independently on the same module in two files, it would be better to create two different sub modules. Merging into one function could be a feasible way support module level visibility.
As I write this article, I notice the /* @internal */ annotation in the ts compiler source code; it's an experimental option released with typescript 1.5.0-alpha to strip the declarations marked as @internal.
It helps to only include the declarations without @internal (which serves as your external APIs) when generating the .d.ts file from your code. And if your consumer are using TypeScript too, this prevents it from using your internal members.
Generating the .d.ts file for:
Latest AJAXWorld RIA Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
SYS-CON Featured Whitepapers
Most Read This Week