Simple Installation and Setup of TypeScript

Simple Installation and Setup of TypeScript

Getting Started with TypeScript in Simple Terms - Lesson 2

ยท

2 min read

Installing TypeScript is quite straightforward. However, you need to make sure that Node.js and npm are installed and active on your system beforehand. To install TypeScript, run the following command in your command line:

$ npm install -g typescript

Well, after the installation is complete, to ensure that TypeScript has been successfully installed, you can execute the following command:

$ tsc -v

The above command shows us the TypeScript version. Well, the installation was as straightforward as that.

First TypeScript Program

We create a file with the .ts extension and write the following code in it:

greeter.ts

function greeter(person) {
    return "Hello, " + person;
}

let user = "Seyed Ahmad";

document.body.textContent = greeter(user);

Well, in the first session, I explained that TypeScript code needs to be compiled into JavaScript code by the TypeScript compiler. So, now we should perform the same task as follows in the command line:

tsc greeter.ts

This above command will create a file with the same name and in the same directory with a .js extension, which contains the compiled code.

Now, we need to include the .js file in our HTML page. Let's create an HTML file with the following content:

<!DOCTYPE html>
<html>
    <head><title>TypeScript Greeter</title></head>
    <body>
        <script src="greeter.js"></script>
    </body>
</html>

Now, the final step is to open this file in a web browser. The output of the above code will be as follows:

Hello, Seyed Ahmad

If you've obtained this output, congratulations, you've successfully executed your first TypeScript program! ๐Ÿ˜Š

Well, that concludes our session for now. In the upcoming sessions, we'll become more familiar with this language and its commands. Be sure to ask your questions and share your comments in the comments section.

resources:

https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html

ย