How to Pass a TypeScript Interview: Top Questions
TypeScript has firmly established itself as one of the most in-demand tools in modern web development. If you're preparing for a technical interview, knowledge of the basics and advanced features of TypeScript is a must. In this article, we'll break down the top questions often asked in interviews and provide code examples to reinforce the material.
1. Basic Types and Their Differences from JavaScript
In an interview, you will definitely be asked about basic TypeScript types and their behavior. The main difference from JavaScript is static typing. Let's look at the key points.
1.1. Primitive Types: string, number, boolean
TypeScript adds explicit type annotations. For example:
let name: string = 'Anna';let age: number = 30;let isActive: boolean = true;Without an annotation, TypeScript infers the type automatically (type inference).
1.2. The any and unknown Types
any disables type checking — it's best to avoid it. unknown is a safer alternative that requires a check before use:
let data: unknown = 'string';// console.log(data.toUpperCase()); // Error!if (typeof data === 'string') { console.log(data.toUpperCase()); // Ok}1.3. never and void
void— the function returns nothing.never— the function never finishes (throws an error or infinite loop).
function throwError(msg: string): never { throw new Error(msg);}2. Interfaces and Types (interface vs type)
This is one of the most common questions. Both mechanisms allow you to describe the structure of objects, but there are differences.
2.1. When to use interface
- For declaring contracts for objects and classes.
- Ability to extend (declaration merging):
interface User { name: string;}interface User { age: number;}// User now contains name and age2.2. When to use type
- For unions, intersections, and primitive aliases.
- Does not support declaration merging.
type Status = 'active' | 'inactive';type Person = { name: string } & { age: number };Interview question: "Can interface be replaced with type?" — Yes, but for classes and extension, interface is better.
3. Generics
Generics allow you to create reusable components that work with any types. This is a must-have for senior positions.
3.1. Simple Generic Function
function firstElement<T>(arr: T[]): T { return arr[0];}const num = firstElement([1, 2, 3]); // numberconst str = firstElement(['a', 'b']); // string3.2. Generic Constraints (extends)
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key];}const user = { name: 'Ivan', age: 25 };console.log(getProperty(user, 'name')); // Ivan3.3. Generics in Interfaces
interface Repository<T> { getById(id: number): T; save(item: T): void;}Often asked: "Why are generics needed if any exists?" — Generics preserve type safety.
4. Utility Types and Advanced Features
Built-in utility types (Partial, Required, Pick, Omit) are indicators of a deep understanding of TypeScript.
4.1. Partial and Required
interface Task { title: string; done: boolean;}const partialTask: Partial<Task> = { title: 'Learn TypeScript' };const requiredTask: Required<Task> = { title: 'Learn TypeScript', done: false };