Originally posted on https://samueleresca.net
Typescript is a language created by Microsoft and released under an open-source (
Typescript solves a lot of problems, especially in the following areas:
- Prototypal inheritance:Typescript solves this problem by adding classes, modules, and iterfaces. This allows programmers to transfer their existing knowledge of OOP;
- Equality and type juggling:Typescript introduces type checking which can provide warnings at design and compile time to pick up potential unintended juggling;
- Management of modules: Typescript makes module loaders to the normal way of working and allows your modules to be compiled to suit the two most prevalent module loading styles without requiring changes to your code;
- Scope: Typescript warning you about implicit global variabiles;
GruntJS and Typescript run on NodeJS, You can install node from here.
Firstly, let's create a new folder, which will contain the project and type the following command:
It will create the package.json file which will contains all informations about installed packages and dependencies.
Here's the final package.json, it declares all devDependencies required by Typescript: https://gist.github.com/e47a5e2c7e14557a77df You can copy the devDependencies section inside your package.json and launch the following command to solves all typescript dependencies:
- Scripts/ts will contain all .ts files used by project;
- Scripts/compiled will contain the main.js which is the result of *.ts files compiling;
index.html simply includes the main.js file, here's the source code: https://gist.github.com/4c7faac11af2b65eeb70
Next step is configure grunt's tasks using the GruntFile.js. The following snippets shows grunt configurations: https://gist.github.com/8dbe86fa226a4ba2ec07 The 'typescript' section (line 20) tells grunt the path of typescript files and the destination file (main.js), it also specifies the folder to watch for changes(line 30). To run gunt tasks let's type
inside the project folder.
Here's the source code used or main.ts file, it will compiled into main.js file and included by index.html page: https://gist.github.com/1fce4d97db00faf6ddd2
Posted on by: