Skip to content

TypeScript Basics

TypeScript is a strongly typed, object-oriented, and compiled superset of JavaScript developed by Microsoft. It enhances JavaScript by adding static typing, interfaces, and better tooling support, making it ideal for large-scale applications.

โœ… Type Safety โ€“ Catches errors at compile time, reducing runtime issues.
โœ… Improved Readability & Maintainability โ€“ Makes large codebases easier to manage.
โœ… Supports Modern JavaScript Features โ€“ Fully supports ES6+ features.
โœ… Better IDE Support โ€“ Provides better IntelliSense and autocompletion.


To install TypeScript globally, use npm (Node Package Manager):

Terminal window
npm install -g typescript

To check the installation:

Terminal window
tsc --version

You can compile a TypeScript file (.ts) into JavaScript (.js) using:

Terminal window
tsc filename.ts

Unlike JavaScript, TypeScript allows defining types explicitly:

let name: string = "Alice"; // String type
let age: number = 30; // Number type
let isActive: boolean = true; // Boolean type

TypeScript can infer types automatically:

let city = "New York"; // Automatically inferred as a string

TypeScript includes all JavaScript types, plus additional types:

TypeExample
String"Hello, TypeScript"
Number42, 3.14
Booleantrue, false
Array[1, 2, 3] or Array<number>
Tuple[string, number] โ†’ ["Alice", 25]
Enumenum Color { Red, Green, Blue }
Anylet data: any = "Hello";
Voidfunction logMessage(): void {}
Neverfunction throwError(): never { throw new Error("Error"); }
let names: string[] = ["Alice", "Bob", "Charlie"];
let user: [string, number] = ["Alice", 25]; // Tuple
enum Color { Red, Green, Blue };
let favoriteColor: Color = Color.Green;

Functions in TypeScript allow type annotations for parameters and return values:

function add(a: number, b: number): number {
return a + b;
}
console.log(add(5, 3)); // Output: 8
const multiply = (a: number, b: number): number => a * b;
function greet(name: string, age?: number) {
return `Hello, ${name}!`;
}
console.log(greet("Alice")); // Output: Hello, Alice!

Interfaces define the structure of an object:

interface User {
name: string;
age: number;
isAdmin?: boolean; // Optional property
}
let user: User = { name: "Alice", age: 30 };
interface MathOperation {
(a: number, b: number): number;
}
const sum: MathOperation = (x, y) => x + y;

TypeScript supports OOP principles like classes, inheritance, and access modifiers:

class Person {
private name: string; // Private property
protected age: number; // Accessible in derived classes
public city: string; // Public property
constructor(name: string, age: number, city: string) {
this.name = name;
this.age = age;
this.city = city;
}
getDetails(): string {
return `${this.name} is ${this.age} years old from ${this.city}.`;
}
}
let person = new Person("Alice", 30, "New York");
console.log(person.getDetails());

typescript class Employee extends Person { constructor(name: string, age: number, city: string, public position: string) { super(name, age, city); } }

let employee = new Employee(โ€œBobโ€, 28, โ€œSan Franciscoโ€, โ€œDeveloperโ€); console.log(employee.getDetails()); ๐Ÿ”น TypeScript and the DOM Manipulating the DOM with TypeScript is similar to JavaScript but with strict type checking:

const button = document.getElementById("btn") as HTMLButtonElement;
button.addEventListener("click", () => {
alert("Button Clicked!");
});

Generics allow creating flexible and reusable components:

function identity<T>(value: T): T {
return value;
}
console.log(identity<number>(42)); // Output: 42
console.log(identity<string>("Hello")); // Output: Hello
FeatureTypeScriptJavaScript
TypingStatic (Strong)Dynamic (Weak)
Error DetectionCompile-timeRuntime
InterfacesYesNo
OOP SupportAdvancedLimited
CompilationRequires tscRuns directly in browsers

1๏ธโƒฃ Initialize a project

Terminal window
npm init -y
npm install typescript --save-dev
npx tsc --init

2๏ธโƒฃ Create a TypeScript file (index.ts)

const message: string = "Hello, TypeScript!";
console.log(message);

3๏ธโƒฃ Compile TypeScript to JavaScript

Terminal window
tsc index.ts

4๏ธโƒฃ Run JavaScript output

Terminal window
node index.js

TypeScript enhances JavaScript by adding static typing, interfaces, classes, and advanced OOP features. It is widely used for frontend frameworks (Angular, React) and backend development (Node.js, NestJS).

๐Ÿš€ Next Steps? Explore TypeScript with React, Node.js, or Angular!