Skip to content

TypeScript Complete Concepts Guide

This reference consolidates the TypeScript concepts discussed into a single study guide.

The provided TypeScript file demonstrates:

Category Concepts
Basic Types string, number, boolean, bigint, symbol
Collections Arrays, Tuples
Advanced Types any, unknown, never, void, unions, intersections, literals
Object Modeling type aliases, interfaces, interface extension
Functions typed functions, optional/default/rest parameters, arrow functions
OOP classes, inheritance, access modifiers, readonly, static, abstract classes
Generics generic functions, interfaces, classes
Utility Types Record, Partial, Required, Pick, Omit
Async Promise, async/await
Modern TS optional chaining, nullish coalescing, type assertions
ES Collections Map, Set
Syntax destructuring, spread
Modules import/export
Advanced decorators, namespaces, type guards
classDiagram
class Animal
Animal : +name
Animal : +sound()

class Dog
Dog : +bark()

Animal <|-- Dog

class Shape
<<abstract>> Shape
Shape : +area()

class Circle
Circle : +area()

Shape <|-- Circle
/******************************************************
* 1. Primitive Types
******************************************************/
let empName: string = "John";
let age: number = 30;
let isActive: boolean = true;
let salary: bigint = 100000n;
let uniqueId: symbol = Symbol("id");
console.log(empName, age, isActive, salary, uniqueId);
/******************************************************
* 2. Arrays
******************************************************/
let numbers: number[] = [10, 20, 30];
let names: Array<string> = ["A", "B", "C"];
console.log(numbers);
console.log(names);
/******************************************************
* 3. Tuple
******************************************************/
let employee: [number, string] = [101, "David"];
console.log(employee);
/******************************************************
* 4. Enum
******************************************************/
enum Status {
ACTIVE,
INACTIVE,
PENDING
}
console.log(Status.ACTIVE);
/******************************************************
* 5. Any
******************************************************/
let anything: any = 100;
anything = "Hello";
anything = true;
/******************************************************
* 6. Unknown
******************************************************/
let value: unknown = "TypeScript";
if (typeof value === "string") {
console.log(value.toUpperCase());
}
/******************************************************
* 7. Void
******************************************************/
function printMessage(msg: string): void {
console.log(msg);
}
printMessage("Hello");
/******************************************************
* 8. Never
******************************************************/
function throwError(message: string): never {
throw new Error(message);
}
// throwError("Something went wrong");
/******************************************************
* 9. Union Types
******************************************************/
let result: string | number;
result = 100;
result = "Pass";
/******************************************************
* 10. Intersection Types
******************************************************/
type Employee = {
id: number;
name: string;
};
type Department = {
department: string;
};
type EmployeeDetails = Employee & Department;
const emp: EmployeeDetails = {
id: 1,
name: "John",
department: "IT"
};
console.log(emp);
/******************************************************
* 11. Literal Types
******************************************************/
let direction: "left" | "right";
direction = "left";
/******************************************************
* 12. Type Alias
******************************************************/
type User = {
id: number;
name: string;
};
const user: User = {
id: 1,
name: "Alex"
};
/******************************************************
* 13. Interface
******************************************************/
interface Person {
id: number;
name: string;
}
const p1: Person = {
id: 101,
name: "Bob"
};
/******************************************************
* 14. Interface Extension
******************************************************/
interface EmployeeInfo extends Person {
salary: number;
}
const e1: EmployeeInfo = {
id: 1,
name: "John",
salary: 50000
};
/******************************************************
* 15. Function
******************************************************/
function add(a: number, b: number): number {
return a + b;
}
console.log(add(10, 20));
/******************************************************
* 16. Optional Parameter
******************************************************/
function greet(name: string, age?: number) {
console.log(name, age);
}
greet("John");
/******************************************************
* 17. Default Parameter
******************************************************/
function multiply(a: number, b: number = 2) {
return a * b;
}
console.log(multiply(5));
/******************************************************
* 18. Rest Parameter
******************************************************/
function total(...nums: number[]) {
return nums.reduce((a, b) => a + b);
}
console.log(total(10, 20, 30));
/******************************************************
* 19. Arrow Function
******************************************************/
const square = (x: number): number => x * x;
console.log(square(10));
/******************************************************
* 20. Class
******************************************************/
class Animal {
constructor(public name: string) {}
sound() {
console.log("Animal Sound");
}
}
const animal = new Animal("Dog");
animal.sound();
/******************************************************
* 21. Inheritance
******************************************************/
class Dog extends Animal {
bark() {
console.log("Barking...");
}
}
const dog = new Dog("Tommy");
dog.sound();
dog.bark();
/******************************************************
* 22. Access Modifiers
******************************************************/
class Student {
public name: string;
private marks: number;
protected school: string;
constructor(name: string, marks: number, school: string) {
this.name = name;
this.marks = marks;
this.school = school;
}
getMarks() {
return this.marks;
}
}
/******************************************************
* 23. Readonly
******************************************************/
class Car {
readonly model: string;
constructor(model: string) {
this.model = model;
}
}
/******************************************************
* 24. Static
******************************************************/
class MathUtil {
static PI = 3.14;
static area(radius: number) {
return this.PI * radius * radius;
}
}
console.log(MathUtil.area(5));
/******************************************************
* 25. Abstract Class
******************************************************/
abstract class Shape {
abstract area(): number;
}
class Circle extends Shape {
constructor(private radius: number) {
super();
}
area(): number {
return Math.PI * this.radius * this.radius;
}
}
console.log(new Circle(5).area());
/******************************************************
* 26. Generics
******************************************************/
function identity<T>(value: T): T {
return value;
}
console.log(identity<number>(100));
console.log(identity<string>("Hello"));
/******************************************************
* 27. Generic Interface
******************************************************/
interface Box<T> {
value: T;
}
const numberBox: Box<number> = { value: 10 };
/******************************************************
* 28. Generic Class
******************************************************/
class DataStore<T> {
private items: T[] = [];
add(item: T) { this.items.push(item); }
getAll() { return this.items; }
}
const store = new DataStore<string>();
store.add("Java");
store.add("Spring Boot");
console.log(store.getAll());
/******************************************************
* 29. keyof
******************************************************/
type Product = { id: number; name: string; };
type ProductKeys = keyof Product;
/******************************************************
* 30. typeof
******************************************************/
const company = { name: "Infosys", employees: 1000 };
type Company = typeof company;
/******************************************************
* 31. Record
******************************************************/
const scores: Record<string, number> = {
Math: 95,
English: 90
};
/******************************************************
* 32. Partial
******************************************************/
interface UserProfile {
id: number;
name: string;
}
const profile: Partial<UserProfile> = { name: "John" };
/******************************************************
* 33. Required
******************************************************/
type RequiredProfile = Required<UserProfile>;
/******************************************************
* 34. Pick
******************************************************/
type UserName = Pick<UserProfile, "name">;
/******************************************************
* 35. Omit
******************************************************/
type WithoutId = Omit<UserProfile, "id">;
/******************************************************
* 36. Promise
******************************************************/
function fetchData(): Promise<string> {
return Promise.resolve("Success");
}
fetchData().then(console.log);
/******************************************************
* 37. Async Await
******************************************************/
async function getData() {
const data = await fetchData();
console.log(data);
}
getData();
/******************************************************
* 38. Optional Chaining
******************************************************/
const customer = { address: { city: "Chennai" } };
console.log(customer?.address?.city);
/******************************************************
* 39. Nullish Coalescing
******************************************************/
let city = null;
console.log(city ?? "Bangalore");
/******************************************************
* 40. Type Assertion
******************************************************/
let input: unknown = "Hello";
let length = (input as string).length;
console.log(length);
/******************************************************
* 41. Map
******************************************************/
const map = new Map<number, string>();
map.set(1, "Java");
map.set(2, "React");
console.log(map);
/******************************************************
* 42. Set
******************************************************/
const set = new Set<number>();
set.add(10);
set.add(20);
set.add(10);
console.log(set);
/******************************************************
* 43. Destructuring
******************************************************/
const person = { id: 1, fullName: "Alex" };
const { id, fullName } = person;
console.log(id, fullName);
/******************************************************
* 44. Spread Operator
******************************************************/
const arr1 = [1,2];
const arr2 = [3,4];
const arr3 = [...arr1, ...arr2];
console.log(arr3);
/******************************************************
* 45. Modules
******************************************************/
// export class EmployeeService {}
// import { EmployeeService } from "./EmployeeService";
/******************************************************
* 46. Decorators
******************************************************/
// @Log
/******************************************************
* 47. Namespaces
******************************************************/
// namespace Utility {}
/******************************************************
* 48. Type Guards
******************************************************/
function print(value: string | number) {
if (typeof value === "string") console.log(value.toUpperCase());
else console.log(value.toFixed(2));
}
print("typescript");
print(100);
/******************************************************
* 49. Optional Properties
******************************************************/
interface Customer {
name: string;
email?: string;
}
/******************************************************
* 50. Readonly Interface
******************************************************/
interface EmployeeReadonly {
readonly id: number;
name: string;
}
const er: EmployeeReadonly = { id: 1, name: "John" };
console.log(er);
  • Covers the most frequently asked TypeScript interview concepts.
  • Demonstrates both language syntax and object-oriented programming features.
  • Includes modern TypeScript features such as utility types, generics, async/await, optional chaining, and type guards.
  • Can serve as a single-file interview revision reference.