Wednesday, October 11, 2017

Getting started with TypeScript under MacOS.

These are mainly notes for me, but I thought that it would be useful for someone else as well. I have a strong dislike for Javascript because the code seems to be sloppy and non-maintainable. When you come from the background of physics, the fact that you cannot do integer math will bother you. I have a new driver that I want to make portable projects between Windows/Linux/MacOS in a way that is approachable and maintainable.

I have been moving my MATLAB to Python, and for many things that are not related specific to mathematics I have had a more difficult time finding a portable language. Due to the fact that Windows support is always a challenge when you do not use it, I settled on Typescript. In the same way that Microsoft address all of my gripes about Java by making C#, Typescript solves most of my complains about Javascipt. Typescript creates Javascript, so that the code can run on the seemingly ubiquitous Javascript.

In order to use Typescript, we need a program called tsc at a minimum. A Typescript IDE would be helpful too (I used Visual Studio Code). In order to install TypeScript, I used macports, so start by installing macports.

Once you have macports, install package manager for node.js called npm:
port install npm5
Now that you have npm, you need to install the typescript support:
npm install -g typescript
Next you should test the installation of the typescript compiler, tsc
tsc --version
At this point, one can compile a program. It is a two step process where you use tsc to turn a typescript file into javascript, and then use node to execute the compiled javascript. I created a file called test.ts as in:
{
    class Startup {
        public static main(): number {
            console.log('Hello World');
            return 0;
        }
    }
    Startup.main();
}
You then can compile the test.ts file:
tsc test.ts
The result will be the generation of file test.js which now can be executed as
node test.js
The result is "Hello World"

No comments:

Post a Comment