User Review( votes)
TypeScript is an open-source programming language developed and maintained by Microsoft.
The easiest way to install TSC is to install it as a Node.js package using NPM i.e. Node Package Manager at the command line.
Download and install Node.js first if it is not already installed.
Make sure to include Node.js runtime and npm package manager.
Now to install TSC, open the terminal and type the below command
npm install -g typescript
g makes sure that it is installed globally and not just in the directory from which we are running the command.
There are different editors available for TypeScript
Let us see how to configure TypeScript for Visual Studio Code.
Download link for Visual Studio Code (if not already installed)
Open Visual Studio Code and select the working folder or
in Node.js command prompt, create the directory and open the visual studio code from there
From file explorer in Visual Studio Code, create the new .ts file
To compile the TypeScript code, open the integrated Terminal in Visual Studio Code and type
To run it use the node command
We can also modify the TypeScript compiler’s default behavior by adding tsconfig.json.
Here we have added tsconfig.json file in the project with the following code
Check below link for different compilerOptions
To configure the build, execute Run Build Task…
As we have created the tsconfig.json file earlier, we will be presented with the following option
Select tsc: build to run the build task.
If tsc: watch is selected the compiler will watch for the changes in TypeScript files and runs the transpiler whenever the changes are saved.
To set the build or watch task as default, select Configure Default Build Task from the Terminal menu.
On selecting tsc: build task, tasks.json file is added inside .vscode
This sets build task as default, so now when we run the Run Build Task, we are not prompted to select a task.
This completes the basic environment setup for using TypeScript with Visual Studio Code.
Hope it helps..