TypeScript Interview Questions and Answers

Static typing, generics, narrowing and how TypeScript integrates with modern frameworks.

Practise 10 random 41 peer-reviewed questions
TypeScript Interview Syllabus & Preparation Strategy

Whether you are preparing for entry-level TypeScript interview questions for freshers or senior software engineer interview questions addressing concurrency, scalability, and system architecture, this track provides peer-reviewed model answers with syntax walkthroughs, edge cases, and practical interview tips.

1 What do utility types like Partial, Pick, Omit and Record do? Easy

They transform existing types, avoiding duplication.

interface User { id: number; name: string; email: string; }

Partial<User>;            // all props optional
Required<User>;           // all props required
Readonly<User>;           // all props readonly
Pick<User, 'id' | 'name'>;// subset
Omit<User, 'email'>;      // remove keys
Record<string, number>;   // index signature
ReturnType<typeof fn>;
Awaited<Promise<string>>; // string

In practice: Partial for update payloads, Omit to hide sensitive fields, Pick for view models, Record for lookup maps.

2 What is TypeScript, and how does it differ from JavaScript? Easy

TypeScript is a statically typed superset of JavaScript that adds optional static typing and other features to JavaScript. It differs from JavaScript by introducing type checking at compile-time, which helps catch errors early and improves code maintainability.

3 What are the basic data types in TypeScript? Easy

TypeScript adds static typing on top of JavaScript, categorizing data types into primitives, composite structures, and special utility types:

### 1. Primitives:

  • boolean: true or false.
  • number: 64-bit floats (42, 3.14) and binary/hex values.
  • string: Textual strings ('hello').
  • bigint: Large integers (100n).
  • symbol: Unique identifier primitives.

### 2. Complex & Structural Types:

  • array: Homogeneous lists typed as number[] or Array<string>.
  • tuple: Fixed-length arrays where each element has a known type: [string, number] (e.g. ['Alice', 25]).
  • enum: Named constants (enum Direction { Up, Down, Left, Right }).
  • object: Non-primitive types (Record<string, any> or explicit interfaces).

### 3. Special Utility Types:

  • any: Disables type checking completely (avoid in strict codebases).
  • unknown: Type-safe counterpart of any; requires type narrowing before usage.
  • void: Indicates functions that return no value.
  • never: Represents values that never occur (e.g. functions that always throw or infinite loops).
  • null & undefined: Represent absence of value, strictly checked when strictNullChecks: true.
4 What is tuple datatype in TypeScript? Easy

In TypeScript, a tuple is a type that allows you to define an array with a fixed number of elements of different types. Tuples are similar to arrays, but the types of elements in a tuple are fixed and their order matters. Tuples are useful when you want to work with a specific set of values, each with its own type, and maintain their order throughout the program.

5 What is the purpose of type annotations in TypeScript, and how are they used? Easy

The purpose of type annotations in TypeScript is to specify the types of variables, function parameters, and return values. They help catch errors during development and provide better tooling support. Annotations are used by adding a colon after the variable or function parameter name, followed by the type.

6 What is the difference between interfaces and type aliases in TypeScript? When would you use one over the other? Easy

Interfaces in TypeScript define how an object should look or what a class should implement. They can be extended or implemented by other interfaces and classes. Type aliases, on the other hand, give new names to existing types or combine multiple types together. Use interfaces when you want to describe object structure or class requirements. Use type aliases when you want to create shorter names for types or combine them in a meaningful way.

7 What is the use of the tsconfig.json file? Easy

The tsconfig.json file is used in TypeScript to configure the compiler options for a project. It allows developers to specify settings such as target version, module system, output directory, and more.

8 What is the "any" type in TypeScript, and when should it be used? Easy

The "any" type in TypeScript is a type that represents a value of any type. It essentially disables type checking for that particular value, allowing it to be assigned or used in any context.

9 What is the "readonly" modifier in TypeScript, and how does it affect properties and arrays? Easy

The "readonly" modifier in TypeScript is used to make properties or array elements read-only, meaning they cannot be modified once initialized. It provides a way to enforce immutability and prevent accidental modifications to certain values. When applied to properties, it prevents reassignment, and when applied to arrays, it disallows adding or removing elements after initialization.

10 What is optional properties in an interface in TypeScript? Easy

In TypeScript, you can define optional properties in an interface by appending a question mark (?) to the property name. This indicates that the property is optional and may or may not be present in the object that implements the interface.

//age & email are marked as optional so it will not cause any error if they're not assigned any value
interface Person {
  name: string;
  age?: number;
  email?: string;
}
11 What is the `keyof` operator in TypeScript? Easy

The keyof operator in TypeScript is used to get a union type of all the keys (property names) of an object type. It allows you to access and use the keys of an object type as string literals in type operations and transformations.

interface Person {
    name: string;
    age: number;
  }

  type PersonKeys = keyof Person;
  const key1: PersonKeys = 'name'; //Valid key
  const key3: PersonKeys = 'address'; //Error
  
12 What is enum in TypeScript? Easy

Enums in TypeScript is a way to define a collection of related constants. Enums assign automatic numeric values to each enumerator by default, but you can also customize them. Enums are useful when you have a fixed set of values that you want to refer to using meaningful names instead of explicit values throughout your code.

13 What is Intersection types in TypeScript? Easy

Intersection types allow you to combine multiple types into a single type that has all the properties and methods of each constituent type. It is denoted by the "&" symbol.

interface A {
  propA: number;
}

interface B {
  propB: string;
}

type IntersectionType = A & B;

const obj: IntersectionType = {
  propA: 123,
  propB: "hello",
};
 
14 What is Union types in TypeScript? Easy

Union types allow you to define a type that can hold values of multiple types. It is denoted by the "|" symbol.

type UnionType = string | number;

let val: UnionType;
val = "hello"; // Valid
val = 123;     // Valid
val = true;    // Error, as boolean is not part
15 What is namespace in TypeScript? Easy

In TypeScript, a namespace is a way to organize and group related code elements such as classes, interfaces, functions, and variables under a single name. Namespaces provide a mechanism to avoid naming conflicts and create a logical hierarchy within your codebase.

16 What is modules in TypeScript? Easy

Modules in TypeScript allow you to organize and encapsulate code into separate files. By using the export keyword, you can make specific code elements accessible to other modules, while the import keyword allows you to use those exported elements in your module. Modules make it easier to manage dependencies, reuse code, and maintain a modular structure in your TypeScript projects.

17 What is the difference between interface and type? Medium

Both describe the shape of an object, with subtle differences:

  • interface supports declaration merging and is usually preferred for public object shapes and class contracts.
  • type can express unions, intersections, tuples, mapped and conditional types that interfaces cannot.
interface User { id: number; }
interface User { name: string; } // merges -> { id, name }

type Id = string | number;
type Pair = [number, number];
type ReadonlyUser = Readonly<User>;

Practical guidance: use interface for objects that may be extended or merged, and type for unions, aliases and advanced type manipulation. Performance is comparable; pick a convention and stay consistent.

18 Explain generics and give a realistic use case. Medium

Generics let you write code that works over many types while preserving the relationship between input and output types.

function first<T>(items: T[]): T | undefined {
  return items[0];
}
const n = first([1, 2, 3]);      // number | undefined
const s = first(['a', 'b']);     // string | undefined

// constraint
function pluck<T, K extends keyof T>(items: T[], key: K): T[K][] {
  return items.map((item) => item[key]);
}

Real use case: a typed API client or a React component that accepts a list and a render callback, so the callback parameter inherits the item type automatically.

19 What are unknown, any and never? Medium
  • any disables type checking. It is contagious and should be avoided; use it only as an escape hatch.
  • unknown is the type-safe counterpart of any: you can assign anything to it, but you must narrow before use.
  • never represents a value that can never occur, such as a function that always throws or an exhausted union in a switch. It also appears as the bottom of the type hierarchy.
function fail(msg: string): never { throw new Error(msg); }

function assertNever(x: never): never { throw new Error('Unexpected: ' + x); }

Using never with exhaustive switch checks is a common interview talking point.

20 How do you narrow a union type? Medium

Narrowing reduces a union to a subset within a control-flow branch. Techniques:

  • typeof for primitives.
  • instanceof for classes.
  • The in operator for property presence.
  • Discriminated unions using a literal field, the most scalable pattern.
  • User-defined type guards returning x is T.
  • Literal/array checks and truthiness for null and undefined.
type Shape =
  | { kind: 'circle'; radius: number }
  | { kind: 'square'; side: number };

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

A discriminated union plus exhaustive switching gives compile-time safety when new variants are added.

Showing 20 of 41 questions

Frequently Asked Questions About TypeScript Interviews

What do hiring managers evaluate in TypeScript technical rounds?

Technical interviewers look for foundational fluency, idiomatic syntax, clarity when communicating complex logic, and awareness of performance trade-offs (e.g. memory footprint, render performance, and network latency) in production environments.

What are the best interview tips for practicing TypeScript questions?

Use active recall: summarize each answer in your own words before revealing the model solution. Focus on explaining why a certain approach is chosen rather than just memorizing code syntax.