How to Pass a TypeScript Interview: Top Advanced Level Questions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Pass a TypeScript Interview: Top Advanced Level Questions

\n\n

An interview for a Senior Frontend or Fullstack developer position rarely goes without in-depth TypeScript questions. If you know basic types and interfaces by heart, then to pass a technical interview for a high grade, you need to understand the advanced type system. In this article, we have compiled the top 5 questions and tasks that are often asked in advanced TypeScript interviews. Each item is accompanied by an analysis and code examples.

\n\n

1. Conditional Types and the infer Keyword

\n\n

Conditional types allow you to create types that depend on a condition. In combination with infer, you can "extract" parts of types, similar to Pattern Matching.

\n\n

Question: Write a ReturnType type (without using the built-in one).

\n\n

Analysis: Use a conditional type with infer to capture the return type of a function.

\n\n
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;\n\n// Usage example\nfunction greet(name: string): string {\n  return `Hello, ${name}`;\n}\n\ntype GreetReturn = MyReturnType<typeof greet>; // string
\n\n

How to answer in an interview: Explain that infer R creates a new type variable R that captures the return type from the function signature. If the condition is not met (a non-function is passed), the type becomes never.

\n\n

2. Mapped Types and Modifiers

\n\n

Mapped types allow you to create new types by transforming properties of an existing one. The advanced level involves working with the +, -, as modifiers and keys via keyof.

\n\n

Question: Create a DeepReadonly type that makes all properties of an object (including nested ones) readonly.

\n\n
type DeepReadonly<T> = {\n  readonly [K in keyof T]: T[K] extends Record<string, unknown> \n    ? DeepReadonly<T[K]> \n    : T[K];\n};\n\ninterface User {\n  name: string;\n  address: {\n    city: string;\n    zip: number;\n  };\n}\n\ntype ReadonlyUser = DeepReadonly<User>;\n// All properties become readonly, including address.city
\n\n

How to answer: Show the recursive call to DeepReadonly for nested objects. Explain how Record<string, unknown> is used to check if a value is an object.

\n\n

3. Template Literal Types

\n\n

Template literal types allow you to manipulate string types at compile time. This is a powerful tool for creating strict APIs.

\n\n

Question: Implement an EventName type that takes a string and returns the type "on{String}" with proper Capitalize.

\n\n
type EventName<T extends string> = `on${Capitalize<T>}`;\n\ntype ClickEvent = EventName<'click'>; // "onClick"\ntype FocusEvent = EventName<'focus'>; // "onFocus"\n\n// Advanced example: extracting from a string\ntype ExtractAction<T extends string> = \n  T extends `on${infer Action}` ? Uncapitalize<Action> : never;\n\ntype Action = ExtractAction<'onSubmit'>; // "submit"
\n\n

How to answer: Note the built-in types Capitalize and Uncapitalize. Show how infer works inside template literals for string parsing.

\n\n

4. Generics with Constraints and Variadic Tuple Types

\n\n

Variadic tuple types (introduced in TS 4.0) allow you to work with tuples of arbitrary length. This is indispensable when creating type-safe functions with a variable number of arguments.

\n\n

Question: Write a type for a function

Blogs

Book Recommendations