Easy Tutorial
❮ Ts Function Ts Type ❯

TypeScript Basic Syntax

A TypeScript program is composed of the following parts:

First TypeScript Program

We can use the following TypeScript program to output "Hello World":

tutorialpro.ts File Code:

const hello: string = "Hello World!";
console.log(hello);

The above code is first compiled using the tsc command:

tsc tutorialpro.ts

This results in the following JavaScript code:

tutorialpro.js File Code:

var hello = "Hello World!";
console.log(hello);

Finally, we use the node command to execute the JavaScript code:

$ node tutorialpro.js
Hello World

The entire process is illustrated in the diagram below:

We can compile multiple TypeScript files simultaneously:

tsc file1.ts file2.ts file3.ts

Common tsc compilation parameters are shown in the table below:

No. Compilation Parameter Description
1. --help Displays help information
2. --module Loads extension modules
3. --target Sets the ECMA version
4. --declaration Generates an additional .d.ts file. The command tsc ts-hw.ts --declaration will generate ts-hw.d.ts and ts-hw.js files.
5. --removeComments Removes comments from the file
6. --out Compiles multiple files and merges them into a single output file
7. --sourcemap Generates a sourcemap (.map) file. A sourcemap is an information file that stores the mapping between the source code and the compiled code.
8. --module noImplicitAny Raises an error on expressions and declarations with an implied any type
9. --watch Runs the compiler in watch mode. It watches the output files and recompiles them when they change.

TypeScript Reserved Keywords

TypeScript reserved keywords are shown in the table below:

| break | as | catch | switch | | case | if | throw | else | | var | number | string | get | | module | type | instanceof | typeof | | public | private | enum | export | | finally | for | while | void | | null | super | this | new | | in | return | true | false | | any | extends | static | let | | package | implements | interface | function | | do | try | yield | const | | continue | | | |

Whitespace and Line Breaks

TypeScript ignores spaces, tabs, and line breaks that appear in the program.

Spaces and tabs are generally used to indent code for readability and understanding.

TypeScript is Case-Sensitive

TypeScript distinguishes between uppercase and lowercase characters.

Semicolons Are Optional

Each line of instruction is a statement, and you can use semicolons or not. Semicolons are optional in TypeScript, but it is recommended to use them.

The following codes are both valid:

console.log("tutorialpro");
console.log("Google");

If statements are written on the same line, semicolons must be used to separate them, otherwise an error will occur, such as:

console.log("tutorialpro"); console.log("Google");

TypeScript Comments

Commenting is a good practice, although many programmers dislike comments, it is still advisable to write textual explanations for each segment of code.

Comments can improve the readability of the program.

Comments can include information about the program, such as the author of the code, descriptions of functions, etc.

The compiler ignores comments.

TypeScript Supports Two Types of Comments

Comment Example:

// This is a single-line comment

/* 
  This is a multi-line comment 
  This is a multi-line comment 
  This is a multi-line comment 
*/

TypeScript and Object-Oriented Programming

Object-oriented programming is a method of understanding and abstracting the real world.

TypeScript is an object-oriented programming language.

Object-oriented programming mainly involves two concepts: objects and classes.

In the diagram below, girl and boy are classes, while each individual is an object of that class:

TypeScript Object-Oriented Programming Example:

class Site { 
   name():void { 
      console.log("tutorialpro") 
   } 
} 
var obj = new Site(); 
obj.name();

The above example defines a class Site, which has a method name(). This method outputs the string "tutorialpro" on the terminal.

The new keyword creates an object of the class, which then calls the name() method.

The compiled JavaScript code is as follows:

var Site = /** @class */ (function () {
    function Site() {
    }
    Site.prototype.name = function () {
        console.log("tutorialpro");
    };
    return Site;
}());
var obj = new Site();
obj.name();

Executing the above JavaScript code will produce the following output:

tutorialpro
❮ Ts Function Ts Type ❯