Getting Started with TypeScript
Learn how to prepare your development environment, install TypeScript, create your first TypeScript program, and compile it into JavaScript.
What Do You Need to Get Started?
You can start learning TypeScript with a code editor, Node.js, npm, and the TypeScript compiler.
| Tool | Purpose |
|---|---|
| Visual Studio Code | Used to write and manage TypeScript files. |
| Node.js | Provides a JavaScript runtime for executing JavaScript programs. |
| npm | Manages JavaScript and TypeScript packages. |
| TypeScript Compiler | Checks TypeScript code and generates JavaScript. |
TypeScript Learning Roadmap
- Install Node.js and verify npm.
- Install the TypeScript compiler.
- Create a TypeScript file with the .ts extension.
- Compile the TypeScript file into JavaScript.
- Execute the generated JavaScript program.
- Practise variables, functions, interfaces, and classes.
1. Check Node.js and npm
Open a terminal or Command Prompt and check whether Node.js and npm are installed on your computer.
Example Commands
node --version
npm --version
Expected Result
v<installed Node.js version>
<installed npm version>
The actual version numbers depend on the versions installed on your computer. If either command is not recognised, install Node.js and open a new terminal before checking again.
2. Install TypeScript
Once Node.js and npm are available, you can install the TypeScript compiler globally using npm.
Installation Command
npm install -g typescript
Verify the Installation
tsc --version
Expected Result
Version number of the installed TypeScript compiler
tsc command runs the TypeScript compiler.
The --version option displays the installed
compiler version.
3. Create Your First TypeScript File
Create a file named app.ts in your project
folder. TypeScript source files use the
.ts extension.
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
Here, studentName is declared as a string
and age is declared as a number.
4. Compile TypeScript into JavaScript
Open the terminal in the folder containing
app.ts, then run the TypeScript compiler.
Compilation Command
tsc app.ts
Result
app.ts
app.js
If compilation succeeds, the compiler normally creates
an app.js file alongside the TypeScript
source file. The generated file contains JavaScript code.
5. Run the Generated JavaScript
After compiling the TypeScript file, use Node.js to execute the generated JavaScript file.
Command
node app.js
Output
Student Name: Rahul
Age: 22
Basic Project Structure
A simple TypeScript project can contain the source file and its generated JavaScript file.
TypeScriptDemo/
|
|-- app.ts
|
|-- app.js
The app.ts file contains the TypeScript source.
The app.js file is generated by compilation.
Larger projects may also include configuration files,
multiple source folders, tests, and package dependencies.
Important TypeScript Commands
| Command | Purpose |
|---|---|
node --version |
Displays the installed Node.js version. |
npm --version |
Displays the installed npm version. |
npm install -g typescript |
Installs the TypeScript compiler globally. |
tsc --version |
Displays the TypeScript compiler version. |
tsc app.ts |
Compiles app.ts into JavaScript when compilation succeeds. |
node app.js |
Executes the generated JavaScript using Node.js. |
Common Mistakes
-
Trying to run
tscbefore installing or configuring the TypeScript compiler. - Running commands in a folder that does not contain the expected source file.
-
Forgetting to save changes to
app.tsbefore compiling. -
Trying to execute
app.tswithnode app.jswhen the JavaScript file has not been generated. - Assuming every TypeScript error is a runtime error. Some are found during type checking or compilation.
- Forgetting to open a new terminal after installing tools if the command path has changed.
Summary
To get started with TypeScript, prepare the development environment, install the compiler, create a .ts file, compile it into JavaScript, and execute the generated JavaScript. These basic steps provide the foundation for learning TypeScript variables, data types, functions, interfaces, and advanced programming concepts.