Getting Started with TypeScript - A Beginner's Guide

What is TypeScript?

TypeScript is an open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, meaning all JavaScript code is also valid TypeScript code. TypeScript adds static typing to JavaScript, which helps catch errors at compile-time rather than runtime.

Setting up Your Environment

To get started with TypeScript, you need to install Node.js and npm if you haven't already. Once that's done, you can install TypeScript globally using npm:

npm install -g typescript

Creating Your First TypeScript File

Create a file with a .ts extension, for example, hello.ts. Add the following TypeScript code to it:

function sayHello(name: string) {
console.log(`Hello, ${name}!`);

Compiling TypeScript

Compile your TypeScript code to JavaScript using the TypeScript compiler, tsc:

tsc hello.ts

This will generate a hello.js file.

Running Your TypeScript Code

Create an HTML file, for example, index.html, and include the generated JavaScript file:

<!DOCTYPE html>
<title>TypeScript Example</title>
<script src="hello.js"></script>

Open index.html in a web browser to see the result.


Congratulations! You've taken your first steps into TypeScript. This is just the beginning, and there's a lot more to explore. TypeScript can help you write more maintainable and less error-prone JavaScript code, making it a valuable tool for both beginners and experienced developers.