Posted in

Top 30 TypeScript Interview Questions and Answers for All Levels

Master TypeScript Interviews: Basic to Advanced Questions Explained

Prepare for your next TypeScript interview with these 30 carefully curated questions covering basic, intermediate, and advanced concepts. This guide helps freshers, developers with 1-3 years experience, and senior professionals with 3-6 years sharpen their TypeScript skills for technical interviews at companies like Atlassian, Zoho, and Swiggy.

Basic TypeScript Interview Questions (1-10)

Question 1: Why use TypeScript over JavaScript?

TypeScript adds static typing and compile-time error checking, helping developers detect bugs early and making codebases more predictable and scalable.[2][3]

Question 2: What problems does TypeScript solve?

TypeScript addresses JavaScript’s weaknesses in type safety, scalability, and readability by enforcing clear type definitions that prevent subtle runtime bugs.[2]

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

Static typing improves code reliability, provides better auto-completion, and increases developer productivity through early type error detection.[2][3]

Question 4: List the built-in types in TypeScript.

TypeScript’s built-in types include: number, string, boolean, void, null, undefined, symbol, object, any, unknown, never, and bigint.[4]

Question 5: What is the difference between ‘any’ and ‘unknown’ types?

‘any’ allows any operation without type checking, while ‘unknown’ requires type checking before operations, making it safer.[1][2]

Question 6: Explain the ‘never’ type in TypeScript.

The ‘never’ type represents values that never occur, such as functions that always throw errors or infinite loops.[1]

Question 7: What are optional properties in TypeScript interfaces?

Optional properties use the ‘?’ symbol and can be undefined. Example:

interface User {
  name: string;
  age?: number;
}
const user: User = { name: "Alice" };

[7]

Question 8: What is the ‘strictNullChecks’ compiler option?

‘strictNullChecks’ prevents null and undefined assignments to types other than their union, catching common runtime errors at compile time.[2][5]

Question 9: Explain union types in TypeScript.

Union types allow a variable to be one of several types, denoted by ‘|’. Example: string | number.[7]

Question 10: What is type inference in TypeScript?

TypeScript automatically infers types when not explicitly declared, like inferring number from let x = 42;.

Intermediate TypeScript Interview Questions (11-20)

Question 11: What are generics in TypeScript? Explain <T>.

Generics create reusable components working with multiple types. <T> is a type parameter placeholder:

function identity<T>(arg: T): T {
  return arg;
}
const num = identity<number>(42);

[1][4]

Question 12: What are the differences between interfaces and types?

Interfaces define object shapes and support extension; types support unions/intersections but cannot be reopened after declaration.[1]

Question 13: Explain TypeScript modules.

Modules organize code into separate files using import/export. ES modules are recommended for modern projects.[4][6]

// math.ts
export function add(a: number, b: number): number {
  return a + b;
}

// main.ts
import { add } from './math';

Question 14: What is the ‘in’ operator in TypeScript?

The ‘in’ operator checks if a property exists in an object, useful for type narrowing:

function hasProperty(obj: any, prop: string): prop is keyof typeof obj {
  return prop in obj;
}

[7]

Question 15: Explain optional chaining in TypeScript.

Optional chaining (?.) safely accesses nested properties, returning undefined if any part is null/undefined:

const user = { profile: { name: "Alice" } };
console.log(user?.profile?.name); // "Alice"
console.log(user?.address?.street); // undefined

[5]

Question 16: What are mapped types in TypeScript?

Mapped types transform existing types by iterating properties. Common example: Partial<T> makes all properties optional.[6]

type Partial<T> = {
  [P in keyof T]?: T[P];
};

Question 17: Explain conditional types.

Conditional types use ternary logic for dynamic typing:

type TypeName<T> = T extends string ? 'string' : 'unknown';
type T0 = TypeName<string>; // "string"

[6][7]

Question 18: What TypeScript compiler options are essential?

Key options: strict, target, module, outDir, esModuleInterop, and noImplicitAny.[2]

Question 19: Does TypeScript support OOP principles?

Yes, TypeScript supports encapsulation, inheritance, abstraction, and polymorphism with cleaner syntax.[4]

Question 20: What is type assertion in TypeScript?

Type assertion tells the compiler to treat a value as a specific type using ‘as’ or angle-bracket syntax, used when you know more about the type than TypeScript does.

Advanced TypeScript Interview Questions (21-30)

Question 21: How do you create readonly properties in TypeScript?

Use readonly modifier to prevent property modification after initialization:

interface Point {
  readonly x: number;
  readonly y: number;
}
const point: Point = { x: 10, y: 20 };
point.x = 5; // Error!

Question 22: Explain utility types like Pick and Omit.

Pick<T, K> selects specific properties; Omit<T, K> excludes them:

type User = { name: string; age: number; id: number };
type UserPreview = Pick<User, 'name' | 'age'>;

Question 23: What are template literal types?

Template literal types create string literal unions from patterns:

type Event = `on${'Click' | 'Hover' | 'Submit'}`;
// "onClick" | "onHover" | "onSubmit"

Question 24: How does TypeScript handle function overloads?

Function overloads define multiple signatures for different parameter/return combinations:

function greet(name: string): string;
function greet(name: number): number;
function greet(name: any): any {
  return name;
}

Question 25: Explain indexed access types.

Indexed access types extract types from other types:

type Person = { name: string; age: number };
type NameType = Person['name']; // string

Question 26: What is the difference between ‘implements’ and ‘extends’?

‘extends’ inherits from classes/interfaces; ‘implements’ ensures a class meets interface requirements.

Question 27: How do you create discriminated unions?

Discriminated unions use a common literal discriminant for type narrowing:

type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; side: number };
function area(shape: Shape) {
  switch (shape.kind) {
    case 'circle': return Math.PI * shape.radius ** 2;
    case 'square': return shape.side ** 2;
  }
}

Question 28: Explain key remapping in mapped types.

Key remapping renames properties using ‘as’:

type Remap<T> = {
  [K in keyof T as `prefix_${string & K}`]: T[K]
};

Question 29: What are recursive types in TypeScript?

Recursive types reference themselves:

type Tree<T> = T | { value: T; left: Tree<T>; right: Tree<T> };

Question 30: How do you implement custom error types in TypeScript?

Create custom error classes extending Error with typed payloads:

class ValidationError extends Error {
  constructor(public issues: string[]) {
    super('Validation failed');
  }
}

[6]

## Related TypeScript Interview Preparation Posts

Leave a Reply

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