TypeScript is a free and open-source, high-level programming language that builds upon JavaScript with static typing and optional annotations. Due to its additional features, it is a widely used language, especially in large-scale projects that require concrete structures to prevent any future errors because of vagueness in coding.
Therefore, this Answer will show you how to install and run TypeScript in VS Code.
Implement the following steps to start your journey into TypeScript coding.
Start by opening your VS Code. If you have not installed it yet, jump to the official VS Code website to install the version that your machine is compatible with.
After this, create a new text file by clicking the option given on the prompt or clicking the file tab and choosing a new text file. Another way is to use the "control + N" shortcut key on Windows/Linux machines or "command + N" on Mac.
Save your file by pressing the "control + S" shortcut key for Windows/Linux and "command + S" for Mac. Alternatively, click the file tab and save your new file. While saving the file, rename your file to your liking but remember to add ".ts" at the end.
Note: You cannot have any spaces in the filename.
Before you can run our code, you need to install TypeScript on your machine. For this, you need to open our terminal inside VS Code. You can again use our shortcut keys. Press "control + shift + ~" on Windows/Linux devices or "command + option+ ~" on Mac. Alternatively, click "terminal" and then "new terminal". When this is done, you need to install TypeScript. Find the commands to install TypeScript below.
For Windows: Install Node.js from their official website then:
npm install -g typescript
For Linux: Use the following commands:
sudo apt install nodejssudo apt install npm// after both are installed, run the third commandnpm install -g typescript
For Mac: Use the following commands:
// install homebrew if you have not done so before/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"// run the following after homebrew is installedbrew install nodenpm install -g typescript
Now, you can write our first lines of code.
After you have written your code, you need to save our file. Then go to your terminal and run the tsc <filename>.ts
command to create your Node.js counterpart file. Then you can run node <filename>.js
to run your transpiled file.
Note: Remember to direct your terminal to the correct directory where your file is saved.
Congratulations! You have successfully run our first TypeScript file. Now it's time to explore more. The below-given code is a simplistic implementation of a calculator. Start with this and improve your TypeScript coding skills.
function addition(x: number, y: number) : number {return x+y}function subtraction(x: number, y: number) : number {return x-y}function multiplication(x: number, y: number) : number {return x*y}function division(x: number, y: number) : number {return x/y}var number1 : number = 5var number2 : number = 6console.log("Add : " + addition(number1, number2))console.log("Subtract: " + subtraction(number1, number2))console.log("Multiply: " + multiplication(number1, number2))console.log("Divide: ", + division(number1, number2))
You can explore more about TypeScript by visiting the official documentation.
Free Resources