Land the job you want — prepare
with Real interviews Q&A
Curated interview questions, company-wise guides and coding rounds. Practice mock interviews, improve with feedback, and track your progress.
TypeScript is a strongly typed programming language built on top of JavaScript.
TypeScript adds features like:
- Static typing
- Interfaces
- Generics
- Better tooling and autocomplete
let username: string = "AK";
let age: number = 25;
let isLoggedIn: boolean = true;TypeScript helps developers catch errors early during development.
Benefits of TypeScript- Better code quality
- Type safety
- Easy maintenance
- Improved IntelliSense
- Scalable for large applications
Types define what kind of value a variable can store.
let username: string = "AK";
let age: number = 25;
let isLoggedIn: boolean = true;The any type disables TypeScript type checking.
let value: any = "Hello";
value = 10;
value = true;
// any disables type checkingunknown is a safer alternative to any.
let value: unknown = "Hello";
if (typeof value === "string") {
console.log(value.toUpperCase());
}
// unknown is safer than anyArrays store multiple values of the same type.
let numbers: number[] = [1, 2, 3];
let users: Array<string> = ["AK", "John"];
console.log(numbers);
console.log(users);A tuple is a fixed-length array with specific types.
let user: [string, number] = ["AK", 25];
console.log(user[0]); // AK
console.log(user[1]); // 25enum is used to define a set of named constants.
enum Role {
Admin,
User,
Guest
}
let currentRole: Role = Role.Admin;
console.log(currentRole);Type aliases create custom reusable types.
type User = {
name: string;
age: number;
};
const user: User = {
name: "AK",
age: 25
};Interfaces define the structure of an object.
interface Person {
name: string;
age: number;
}
const person: Person = {
name: "John",
age: 30
};TypeScript allows you to define parameter and return types.
function greet(name: string): void {
console.log("Hello " + name);
}
greet("AK");Generics allow reusable components that work with multiple types.
function identity<T>(value: T): T {
return value;
}
console.log(identity<string>("Hello"));
console.log(identity<number>(100));Classes are blueprints for creating objects.
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
console.log("Hello " + this.name);
}
}
const p1 = new Person("AK");
p1.greet();Inheritance allows one class to inherit properties and methods from another class.
class Animal {
speak() {
console.log("Animal speaks");
}
}
class Dog extends Animal {
bark() {
console.log("Dog barks");
}
}
const d = new Dog();
d.speak();
d.bark();Intersection types combine multiple types into one.
type Admin = {
name: string;
};
type Employee = {
id: number;
};
type AdminEmployee = Admin & Employee;
const user: AdminEmployee = {
name: "AK",
id: 101
};Literal types allow exact values as types.
type Status = "success" | "error" | "loading";
let currentStatus: Status = "success";
// currentStatus = "done"; ❌ ErrorUnion types allow multiple possible types.
function printId(id: number | string) {
console.log(id);
}
printId(101);
printId("A101");Optional chaining safely accesses nested properties.
function getValue(value: string | null) {
console.log(value?.toUpperCase());
}
getValue("hello");
getValue(null);Optional properties are properties that may or may not exist.
interface User {
name: string;
age?: number;
}
const user1: User = {
name: "AK"
};
const user2: User = {
name: "John",
age: 30
};Type assertion tells TypeScript the specific type of a value.
const button = document.getElementById("btn") as HTMLButtonElement;
button.addEventListener("click", () => {
console.log("Clicked");
});Generic interfaces allow reusable typed structures.
interface ApiResponse<T> {
data: T;
success: boolean;
}
const response: ApiResponse<string> = {
data: "User fetched",
success: true
};