Posted in

Top 30 TypeScript Interview Questions and Answers for 2026

Prepare for Your TypeScript Interview: Basic to Advanced Questions

This comprehensive guide features 30 essential TypeScript interview questions covering conceptual, practical, and scenario-based topics. Questions progress from basic to advanced, suitable for freshers, 1-3 years experience, and 3-6 years professionals preparing for roles at companies like Zoho, Salesforce, Atlassian, and Swiggy.

Basic TypeScript Interview Questions (1-10)

1. What is TypeScript and how does it differ from JavaScript?

TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript. It adds static typing, interfaces, and advanced features while maintaining full JavaScript compatibility. TypeScript catches type errors at compile time, unlike JavaScript’s runtime error detection.[1][2]

2. What does it mean that TypeScript is a “superset” of JavaScript?

Every valid JavaScript code is also valid TypeScript code. TypeScript can interpret existing JavaScript with no or minimal modifications, allowing gradual adoption in existing projects.[1]

3. What are the main benefits of static typing in TypeScript?

Static typing provides early error detection, better autocompletion, improved refactoring, and enhanced tooling support. It prevents runtime type errors and improves code maintainability.[2][3]

4. What are TypeScript’s primitive types?

TypeScript primitive types include string, number, boolean, null, undefined, symbol, and bigint. These form the foundation for all type annotations.[4]

5. How do you declare variables with explicit types in TypeScript?

Use type annotations with a colon after the variable name. Here’s an example:

const productId: number = 123;
const productName: string = 'Laptop';
const isAvailable: boolean = true;

6. What is the difference between let, const, and var in TypeScript?

let allows reassignment within block scope, const prevents reassignment but allows mutation of object properties, and var has function scope and can be hoisted. TypeScript recommends let and const.[1]

7. Explain TypeScript interfaces with an example.

Interfaces define the shape of objects. They provide type checking without affecting runtime code:

interface Product {
  id: number;
  name: string;
  price?: number; // optional property
}

const laptop: Product = { id: 1, name: 'Dell XPS' };

8. What are TypeScript enums and when should you use them?

Enums create named constants for related values, improving readability and type safety:

enum OrderStatus {
  PENDING = 'pending',
  SHIPPED = 'shipped',
  DELIVERED = 'delivered'
}

const status: OrderStatus = OrderStatus.SHIPPED;

9. How does optional chaining work in TypeScript?

Optional chaining (?.?) safely accesses nested properties that might be null or undefined, preventing runtime errors:

interface User {
  profile?: { email?: string };
}

const user: User = {};
const email = user?.profile?.email; // undefined, no error

10. What is the any type and why should you avoid it?

any disables type checking, allowing any operations. It defeats TypeScript’s purpose. Use specific types or unknown instead for better safety.[2]

Intermediate TypeScript Interview Questions (11-20)

11. Explain TypeScript generics with a practical example.

Generics create reusable components that work with multiple types:

function identity<T>(value: T): T {
  return value;
}

const num = identity<number>(42);
const str = identity<string>('hello');

12. What are union types in TypeScript?

Union types (|) allow a variable to be one of several types:

let value: string | number;
value = 'hello'; // valid
value = 123;     // valid
value = true;    // compile error

13. How do you implement TypeScript classes?

TypeScript classes support access modifiers and inheritance:

class Product {
  constructor(public id: number, public name: string) {}
  
  getDetails(): string {
    return `${this.name} (ID: ${this.id})`;
  }
}

14. What is type assertion in TypeScript?

Type assertion tells the compiler you know more about the type than it does:

let value: any = 'this is a string';
let strLength: number = (value as string).length;

15. Explain mapped types in TypeScript.

Mapped types transform existing types:

type Readonly<T> = {
  readonly [P in keyof T]: T[P];
};

interface User { name: string; age: number; }
type ReadonlyUser = Readonly<User>;

16. How do you handle function overloads in TypeScript?

Function overloads define multiple call signatures:

function greet(name: string): string;
function greet(name: string, age: number): string;
function greet(name: string, age?: number): string {
  return age ? `Hello ${name}, you are ${age}` : `Hello ${name}`;
}

17. What are index signatures in TypeScript?

Index signatures allow dynamic property access:

interface StringDictionary {
  [key: string]: string | number;
}

const dict: StringDictionary = {
  name: 'Laptop',
  price: 999
};

18. Explain TypeScript namespaces.

Namespaces organize code logically:

namespace Utils {
  export function formatPrice(price: number): string {
    return `$${price.toFixed(2)}`;
  }
}

console.log(Utils.formatPrice(19.99));

19. What is the difference between interface and type?

Both define types, but interface supports declaration merging and extends easily, while type supports unions, intersections, and primitives better.[1]

20. How do you configure strict null checks in TypeScript?

Enable "strictNullChecks": true in tsconfig.json or use the --strictNullChecks compiler flag to catch null/undefined errors at compile time.[3][5]

Advanced TypeScript Interview Questions (21-30)

21. What are conditional types in TypeScript?

Conditional types use ternary logic to determine types:

type NonNullable<T> = T extends null | undefined ? never : T;
type T1 = NonNullable<string | null>; // string
type T2 = NonNullable<string>; // string

22. Explain utility types like Partial and Pick.

Utility types manipulate existing types:

interface Product {
  id: number;
  name: string;
  price: number;
}

type PartialProduct = Partial<Product>; // all properties optional
type ProductPreview = Pick<Product, 'id' | 'name'>; // only id and name

23. How do you create discriminated unions?

Discriminated unions use a common property to narrow types:

type Shape = 
  | { kind: 'circle'; radius: number }
  | { kind: 'square'; side: number };

function area(shape: Shape): number {
  if (shape.kind === 'circle') return Math.PI * shape.radius ** 2;
  return shape.side ** 2;
}

24. What are template literal types?

Template literal types create string literal unions dynamically:

type Event = 'click' | 'hover' | 'focus';
type EventName = `on${Capitalize<Event>}`; 
// "onClick" | "onHover" | "onFocus"

25. Explain infer keyword usage.

infer extracts types from other types:

type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type Func = () => string;
type Returns = ReturnType<Func>; // string

26. How do you implement custom type guards?

Type guards narrow types within conditional blocks:

function isProduct(obj: any): obj is Product {
  return typeof obj === 'object' && 'id' in obj && 'name' in obj;
}

function processItem(item: any) {
  if (isProduct(item)) {
    console.log(item.name); // TypeScript knows item has name
  }
}

27. What are key remapping utilities in mapped types?

Key remapping transforms property names:

type Mapped<T> = { [K in keyof T as `prefix_${string & K}`]: T[K] };
type User = { name: string; age: number };
type MappedUser = Mapped<User>; // { "prefix_name": string; "prefix_age": number }

28. Scenario: At Atlassian, how would you type a generic API response handler?

Create a generic response type with proper error handling:

interface ApiResponse<T> {
  success: boolean;
  data?: T;
  error?: string;
}

function handleResponse<T>(response: ApiResponse<T>): T {
  if (!response.success) throw new Error(response.error);
  return response.data!;
}

29. Scenario: Design a type-safe configuration system for a Paytm-like payment service.

Use discriminated unions for different payment modes:

type PaymentConfig =
  | { mode: 'card'; cardNumber: string; expiry: string }
  | { mode: 'upi'; vpa: string; pin?: string };

function processPayment(config: PaymentConfig): boolean {
  switch (config.mode) {
    case 'card': return validateCard(config);
    case 'upi': return validateUPI(config);
  }
}

30. How do you create a type that extracts function parameters?

Use conditional types with infer:

type Parameters<T> = T extends (...args: infer P) => any ? P : never;
type Func = (id: number, name: string) => void;
type Params = Parameters<Func>; // [id: number, name: string]

Leave a Reply

Your email address will not be published. Required fields are marked *