To install and set up TypeScript in Visual Studio Code (VS Code), follow these steps:
Step 1: Install Node.js and npm
TypeScript is typically installed via npm
(Node Package Manager), so you need to install Node.js first if you don’t already have it.
- Go to the Node.js website and download the latest stable version.
- After installing, verify that both Node.js and npm are installed correctly by running these commands in your terminal:
node -v # Shows the Node.js version
npm -v # Shows the npm version
Step 2: Install TypeScript Globally
To install TypeScript globally on your machine, run the following command in your terminal:
npm install -g typescript
This installs the TypeScript compiler (tsc
) globally, so you can use it from any project.
To verify the installation, run:
tsc -v # Shows the TypeScript version
Step 3: Install TypeScript in a Specific Project (Optional)
If you want to install TypeScript for a specific project rather than globally, navigate to your project folder and run:
npm install --save-dev typescript
This will add TypeScript as a development dependency in your project.
Step 4: Set Up a TypeScript Project
- Initialize a TypeScript configuration file (
tsconfig.json
) in your project directory by running:
tsc --init
This creates a tsconfig.json
file that defines the TypeScript compiler options. You can modify the options in this file to suit your project’s needs.
- Example of
tsconfig.json
:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
target
: Specifies which JavaScript version you want to compile down to (e.g., ES5, ES6).module
: Defines the module system (e.g.,commonjs
,esnext
).strict
: Enables strict type checking.include
: Specifies the files or folders to include in the compilation.exclude
: Specifies the files or folders to exclude.
Step 5: Write TypeScript Code in VS Code
- Create a
.ts
file in your project (e.g.,index.ts
). - Write TypeScript code, for example:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("World"));
Step 6: Compile TypeScript to JavaScript
To compile your TypeScript code to JavaScript, open the terminal and run the tsc
command:
tsc
This command compiles all .ts
files in the project (based on tsconfig.json
) into JavaScript files. If no errors are found, the compiled .js
files will be generated.
Step 7: Run the Compiled JavaScript
You can run the compiled JavaScript using Node.js or in a browser, depending on your project setup. If you’re using Node.js, run:
node index.js
Step 8: Enable TypeScript Support in VS Code
By default, Visual Studio Code has built-in support for TypeScript, but you can enhance the experience by installing the TypeScript language extension:
- Open Extensions Panel: In VS Code, press
Ctrl+Shift+X
to open the Extensions panel. - Search for “TypeScript” and make sure the TypeScript and JavaScript Language Features extension is installed (it’s usually installed by default).
- Install Additional Extensions (Optional): You might also want to install some additional extensions, such as:
- ESLint: For linting and code formatting.
- Prettier: For automatic code formatting.
Step 9: Configure Auto-Compilation in VS Code (Optional)
To have VS Code automatically compile TypeScript files whenever you save them, follow these steps:
- Open the Command Palette by pressing
Ctrl+Shift+P
. - Search for and select
Tasks: Configure Default Build Task
. - Select
tsc: build - tsconfig.json
from the list.
Now, whenever you run the build task (Ctrl+Shift+B
), it will automatically compile your TypeScript files.
Summary of Steps:
- Install Node.js (and npm).
- Install TypeScript globally (
npm install -g typescript
). - (Optional) Install TypeScript in your project (
npm install --save-dev typescript
). - Create a
tsconfig.json
file (tsc --init
). - Write your TypeScript code in
.ts
files in VS Code. - Compile your TypeScript code to JavaScript using the
tsc
command. - Run the compiled JavaScript in Node.js or a browser.
- Optionally, configure VS Code to auto-compile TypeScript on save.