Javatpoint Logo
Javatpoint Logo

TypeScript First Program

In this section, we will learn how to write a program in TypeScript, how to compile it, and how to run it. Also, we will see how to compiles the program and shows the error, if any.

Let's write a program in the text editor, save it, compile it, run it, and displays the output to the console. To do this, perform the following steps.

Step-1 Open the Text Editor and write/copy the following code.

Step-2 Save the above file as ".ts" extension.

Step-3 Compile the TypeScript code. To compile the source code, open the command prompt, then goes to the file directory location where we saved the above file such as if we save on desktop, then type:- cd Desktop/folder_name and type the following command in the Terminal Window:- tsc filename.ts and press Enter.

TypeScript First Program

It will generate JavaScript file with ".js" extension at the same location where the TypeScript source file exists. Following ".js" file is the output of TypeScript (.ts) file.

TypeScript First Program

NOTE- If we will directly run ".ts" file on the web browser, it will throw an error message. But after compilation of ".ts" file, we will get ".js" file which can be executed on any browser.

Step-4 Now, to run the above JavaScript file, type the following command in the terminal window: node filename.js and press Enter. We will get the final output as:

TypeScript First Program

Now, we will write the program in TypeScript, compile it, and see the error, if found.

Step 1 Open the Text Editor and write/copy the following code.

Step-2 Save the above file as ".ts" extension.

Step-3 Compile the TypeScript code. To compile the source code, open the command prompt, then goes to the file directory location where we saved the above file, then type the following command in the Terminal Window:- tsc filename.ts and press Enter.

This TypeScript source file will generate an error which can be shown in the following image.

TypeScript First Program

NOTE: The above program shows an error becacuse we were taking the variable a and b as of number type, but we were passing the variable "a" as the string and variable "b" as the number.

Next TopicTypeScript Types




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA