TypeScript Environment Setup
Learn how to prepare your computer for TypeScript development, create a project folder, configure your editor, and compile your first TypeScript program.
What Do You Need?
| Tool | Purpose |
|---|---|
| Node.js | Runs JavaScript outside the browser. |
| npm | Installs TypeScript and other packages. |
| Visual Studio Code | Helps you write and manage source code. |
| TypeScript Compiler | Checks TypeScript code and generates JavaScript. |
TypeScript Workflow Diagram
Follow the diagram to understand how TypeScript code moves from writing the program to running it.
Step 1
Write CodeCreate your file in the editor.
app.tsStep 2
Check and CompileThe TypeScript compiler checks types and generates JavaScript.
tscStep 3
Generate JavaScriptThe compiler produces JavaScript output.
app.jsStep 4
Run the ProgramExecute JavaScript in a compatible runtime.
Node.js1. Create a Project Folder
Create a dedicated folder for your TypeScript practice. Open Command Prompt or a terminal and run these commands.
Example Commands
mkdir TypeScriptDemo
cd TypeScriptDemo
Folder Structure
TypeScriptDemo/
app.ts
app.js
The TypeScript source file is named
app.ts. The JavaScript file
app.js is normally generated after compilation.
2. Set Up Visual Studio Code
Visual Studio Code is an editor that provides syntax highlighting, code navigation, and TypeScript language support.
- Open Visual Studio Code.
- Select File → Open Folder.
-
Select your
TypeScriptDemofolder. -
Create a new file called
app.ts. - Write your TypeScript code and save the file.
3. Create Your First TypeScript Program
Open app.ts and enter the following code.
Example: app.ts
let studentName: string = "Rahul";
let age: number = 22;
console.log("Student Name: " + studentName);
console.log("Age: " + age);
Expected Output
Student Name: Rahul
Age: 22
The variable studentName is a string,
while age is a number.
4. Compile TypeScript
Run the compiler from the folder containing
app.ts.
Command
npx tsc app.ts
Result
app.ts
app.js
If TypeScript is installed as a project dependency and
compilation succeeds, the compiler normally creates
app.js.
5. Execute the Generated JavaScript
Use Node.js to execute the generated JavaScript file.
Command
node app.js
Expected Output
Student Name: Rahul
Age: 22
Environment Setup Checklist
Before moving to the next lesson, verify that you can complete each task.
Common Mistakes
- Writing code in the wrong folder.
-
Forgetting to save
app.ts. -
Running
node app.jsbefore generating the JavaScript file. -
Confusing the
.tssource file with the.jsoutput file. -
Trying to run
npx tscwithout installing TypeScript in the project or making the compiler available.
Summary
A TypeScript development environment includes a code editor, Node.js, npm, and the TypeScript compiler. After preparing the environment, create a TypeScript source file, compile it into JavaScript, and execute the generated file. The workflow diagram above summarises the process.