How to Install TypeScript and write your first program
Tags: webdev, typescript, programming, learning
Published: 2024-07-13 06:05:00

TypeScript has become a popular choice for many developers due to its strong typing and excellent tooling support. In this guide, we'll walk you through installing TypeScript and writing your first TypeScript program.

#### Step 1: Install Node.js and npm
Before installing TypeScript, you need to have Node.js and npm (Node Package Manager) installed. You can download and install them from the [official Node.js website](https://nodejs.org/).

To check if Node.js and npm are installed, open your terminal and run:
```bash
node -v
npm -v
```
You should see the version numbers for both Node.js and npm.

#### Step 2: Install TypeScript
Once Node.js and npm are installed, you can install TypeScript globally using npm. Open your terminal and run:
```bash
npm install -g typescript
```
This command installs TypeScript globally on your machine, allowing you to use the `tsc` command to compile TypeScript files.

To verify the installation, run:
```bash
tsc -v
```
You should see the TypeScript version number.

#### Step 3: Create Your First TypeScript Program
1. **Create a New Directory**: Create a new directory for your TypeScript project and navigate into it:
   ```bash
   mkdir my-typescript-project
   cd my-typescript-project
   ```

2. **Initialize a New Node.js Project**: Initialize a new Node.js project to create a `package.json` file:
   ```bash
   npm init -y
   ```

3. **Create a TypeScript File**: Create a new file named `index.ts` manually or run this command: 
   ```bash
   touch index.ts
   ```

4. **Write Your First TypeScript Code**: Open `index.ts` in your preferred text editor and add the following code:
   ```typescript
   const welcome:string= "Hello World!"
   ```

#### Step 4: Compile TypeScript to JavaScript
To compile your TypeScript code to JavaScript, use the TypeScript compiler (`tsc`). In your terminal, run:
```bash
tsc index.ts
```
This command generates a `index.js` file in the same directory.

#### Step 5: Run the Compiled JavaScript
Now, you can run the compiled JavaScript file using Node.js:
```bash
node index.js
```
You should see the output:
```
Hello, World!
```


Follow these steps to set up TypeScript, configure file paths, and write your first TypeScript program.


1. **Create a TypeScript Configuration File**
   - Create a `tsconfig.json` file by running:
     ```bash
     tsc --init
     ```
   - This command generates a `tsconfig.json` file.

2. **Configure File Paths**
   - Open the `tsconfig.json` file and set the `rootDir` and `outDir` options:
     ```json
     {
       "compilerOptions": {
         "rootDir": "./module/src",
         "outDir": "./module/dist"
       }
     }
     ```

3. **Create a TypeScript File**
   - Create a file named `hello.ts` inside the `module/src` directory and add the following code:
     ```typescript
     const welcome: string = "Hello World!";
     console.log(welcome);
     ```

4. **Compile TypeScript to JavaScript**
   - To convert the TypeScript code to JavaScript, run:
     ```bash
     tsc
     ```
   - This will generate a `hello.js` file inside the `module/dist` directory.

5. **Run the Compiled JavaScript File**
   - Use Node.js to run the compiled JavaScript file:
     ```bash
     node module/dist/hello.js
     ```
   - You should see the output:
     ```
     Hello, World!
     ```

By following these steps, you've successfully installed TypeScript, configured file paths, and written and executed your first TypeScript program.