What is tsickle?
Tsickle is a TypeScript to Closure Compiler transformer that helps in generating Closure-compatible JavaScript from TypeScript code. It is primarily used to bridge the gap between TypeScript and Google's Closure Compiler, enabling advanced optimizations and type-checking.
What are tsickle's main functionalities?
Type Annotations to JSDoc
Tsickle converts TypeScript type annotations into JSDoc comments, which can be understood by the Closure Compiler for type-checking and optimizations.
/* TypeScript Code */
function add(a: number, b: number): number {
return a + b;
}
/* Transformed JavaScript Code */
/**
* @param {number} a
* @param {number} b
* @return {number}
*/
function add(a, b) {
return a + b;
}
Closure Compiler Compatibility
Tsickle transforms TypeScript classes and other constructs into a form that is compatible with the Closure Compiler, enabling advanced optimizations.
/* TypeScript Code */
class Example {
private value: string;
constructor(value: string) {
this.value = value;
}
}
/* Transformed JavaScript Code */
/**
* @constructor
* @param {string} value
*/
var Example = function(value) {
/** @private {string} */
this.value = value;
};
Type Checking with Closure Compiler
By converting TypeScript types to JSDoc, Tsickle allows the Closure Compiler to perform type-checking on the generated JavaScript code.
/* TypeScript Code */
function greet(name: string): string {
return 'Hello, ' + name;
}
/* Transformed JavaScript Code */
/**
* @param {string} name
* @return {string}
*/
function greet(name) {
return 'Hello, ' + name;
}
Other packages similar to tsickle
babel
Babel is a JavaScript compiler that allows you to use next-generation JavaScript, today. It can transform TypeScript code into JavaScript, but it does not provide the same level of integration with the Closure Compiler as Tsickle.
typescript
The TypeScript compiler (tsc) itself can compile TypeScript to JavaScript. While it does not specifically target Closure Compiler compatibility, it is a robust and widely-used tool for TypeScript development.
google-closure-compiler
Google Closure Compiler is a tool for making JavaScript download and run faster. While it can be used in conjunction with Tsickle, it does not itself transform TypeScript code.
Tsickle - TypeScript to Closure Annotator
Tsickle processes TypeScript and adds Closure Compiler
-compatible JSDoc annotations. This allows using TypeScript to transpile your sources, and then
Closure Compiler to bundle and optimize them, while taking advantage of type information in Closure
Compiler.
Installation
- execute
npm i
to install the dependencies,
Gulp tasks
gulp watch
executes the unit tests in watch mode (use gulp test.unit
for a single run),gulp test.e2e
executes the e2e tests,gulp test.check-format
checks the source code formatting using clang-format
,gulp test
runs unit tests, e2e tests and checks the source code formatting.