TypeScript: Hello, World!

JavaScript runs on the client side such as browsers. It enables dynamic interactions for web pages. With the support of Node.js, JavaScript can also run on the server or desktop side, which makes it a programming language crossing both front and back ends.

TypeScript is a superset of JavaScript. It basically enables the capability of building large applications with JavaScript by enhancing it with additional features/supports such as types, interfaces, and enumeration.

Environment

  • macOS Catalina, Version 10.15.7.

Install Node.js via Linux binaries

Download the LTS version of Linux binaries from the official web site:

wget https://nodejs.org/dist/v14.17.5/node-v14.17.5-linux-x64.tar.xz

Then follow the offical instruction for the installation.

  1. Unzip the binary archive.

    VERSION=v14.17.5
    DISTRO=linux-x64
    sudo mkdir -p /usr/local/lib/nodejs
    sudo tar -xJvf node-$VERSION-$DISTRO.tar.xz -C /usr/local/lib/nodejs 
    
  2. Set the environment variable.

    Add the below to the end of ~/.profile.

    # Nodejs
    VERSION=v14.17.5
    DISTRO=linux-x64
    export PATH=/usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin:$PATH
    

    Refresh profile

    . ~/.profile
    
  3. Symlink executables into the default path. This allows to run them via sudo.

    sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/node /usr/bin/node
    sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/npm /usr/bin/npm
    sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/npx /usr/bin/npx
    

Install typescript

Node.js only recognizes JavaScript codes. To translate TypeScript codes to JavaScript codes, we need to install an interpreter typescript (all lowercase letters) which includes the tsc command.

sudo npm i -g typescript
  • npm is used to install packages for frontend developments. It’s already installed during the installation of Node.js (which may be a legacy release like Version 6.x, and can be updated to the current release like Version 7.x via npm i -g npm).
  • i means installation.
  • -g (--global) is to enable running tsc anywhere.

Hello, World!

Create a file hello.ts and add the following line.

console.log("Hello, World!")

To execute this program,

  1. Translate TypeScript codes to JavaScript codes.

    tsc hello.ts
    
  2. Execute JavaScript codes.

    node hello.js
    

Use one command

In fact, the above two steps can be combined into one command. This requires to install another package ts-node.

sudo npm i -g ts-node

It’s likey that a missing of some dependent packages will be reported such as:

npm WARN ts-node@10.2.1 requires a peer of @types/node@* but none is installed. You must install peer dependencies yourself.
npm WARN ts-node@10.2.1 requires a peer of typescript@>=2.7 but none is installed. You must install peer dependencies yourself.

This can be resolved by installing them respectively.

sudo npm install --save-dev "@types/node@*"
sudo npm install --save-dev "typescript@>=2.7"

Now we will be able to execute TypeScript codes directly.

ts-node hello.ts

Contents