How to Pass a TypeScript Interview: Top Questions with Examples
TypeScript has firmly established itself as the standard for enterprise development in JavaScript. If you are preparing for an interview, it is important not just to know the syntax, but also to understand how types, generics, and utilities work. In this article, we have broken down the top questions often asked in technical interviews, with detailed answers and code examples.
This material will be useful for both junior developers and those who want to refresh their knowledge before an important meeting.
1. What is TypeScript and why is it needed?
TypeScript is a strictly typed superset of JavaScript that compiles to plain JS. The main goal is to add static typing, which allows you to:
- Catch errors at compile time, rather than at runtime.
- Improve code readability and maintainability.
- Get autocompletion and refactoring in the IDE.
Example:
// JavaScriptfunction add(a, b) { return a + b; // a and b can be anything}
// TypeScriptfunction add(a: number, b: number): number { return a + b; // compilation error if a string is passed}In an interview, it is important to emphasize that TypeScript does not replace JavaScript, but complements it, making the code safer.
2. Basic types and their usage
TypeScript offers a wide range of basic types. Questions on this topic test your understanding of the type system.
List the main types:
number,string,booleannull,undefinedany— disables type checking (try to avoid it)void— for functions that return nothingnever— for functions that never complete (e.g., throw an exception)
Example with union types:
type Status = 'active' | 'inactive' | 'pending';let userStatus: Status = 'active'; // only one of three valuesOften asked about the difference between interface and type. Answer: interface can be extended (extends), while type cannot. For objects, interface is preferable; for primitives and unions, type is better.
3. Generics — the foundation of flexibility
Generics allow you to create components that work with different types without losing type safety. This is one of the most common questions.
Example of a simple generic:
function identity<T>(arg: T): T { return arg;}const result = identity<string>('hello'); // type stringconst numberResult = identity(42); // type number (type inference)Question: How to constrain a generic?
Use the extends keyword:
function getLength<T extends { length: number }>(arg: T): number { return arg.length;}getLength('hello'); // 5getLength([1,2,3]); // 3// getLength(123); // error: number has no lengthYou will also be asked about generics in interfaces:
interface ApiResponse<T> { data: T; status: number; error?: string;}const userResponse: ApiResponse<{ id: number; name: string }> = { data: { id: 1, name: 'Alice' }, status: 200};4. Utility Types
Built-in TypeScript utilities simplify type manipulations. Knowing them is a sign of an experienced developer.
Top 5 utilities you should know:
Partial<T>— makes all fields optional.Required<T>— makes all fields required.Pick<T, K>— selects only the specified keys.