← All topics

TypeScript Interview Questions and Answers

Types, generics, narrowing, utility and conditional types, configuration and typing React.

225 questions for TypeScript interviews with answers and explanations - Junior, Middle and Senior levels. Sample questions, online quizzes and coding tasks.

Questions by level

Sample questions

A few questions from each level with the correct answer and an explanation.

TypeScript · Junior

  1. What is TypeScript?

    Correct answer: A superset of JavaScript with static typing that compiles to JS

    Any valid JS is almost always valid TS. Types are checked at compile time and then erased: plain JavaScript runs at runtime, and there are no type checks there. That's why external data (APIs, forms) is still validated at runtime - e.g. with Zod.

  2. Does TypeScript check types while the program is running?

    Correct answer: No: types are erased at compile time; plain JS runs at runtime

    TypeScript is a development-time tool. If an API returns null instead of an object, types won't save you. For runtime checks, use typeof/instanceof and schema libraries (Zod, Valibot), from which you can also infer TS types.

  3. How does unknown differ from any?

    Correct answer: You can't do anything with unknown without narrowing, while any disables checks

    any turns typing off and "infects" everything it touches. unknown is the safe counterpart: the value can be anything, but you can use it only after a check (typeof x === 'string'). For data from JSON.parse and catch (e), unknown is the right choice.

  4. What does the never type mean?

    Correct answer: The type of a value that can't exist: the function never returns control

    never is the empty type. It's returned by functions that always throw or loop forever. It also appears when a union is fully narrowed - exhaustive switch checks are built on this. void means the function finishes but its result isn't used.

All 75 questions with answers →

TypeScript · Middle

  1. What does <T extends { id: string }> mean in a generic?

    Correct answer: A constraint: T must have at least an id: string property

    A constraint lets you safely access item.id inside the function while keeping the full type T in the result. You can pass any object with a suitable id and other fields. Classes and inheritance have nothing to do with it.

  2. How do you type getProp(obj, key) so the result depends on the key?

    Correct answer: function getProp<T, K extends keyof T>(obj: T, key: K): T[K]

    K extends keyof T allows only existing keys, and the indexed access T[K] gives the exact value type: getProp(user, 'age') → number. The any version loses typing, and string lets typos through.

  3. What does the conditional type T extends string ? 'text' : 'other' do?

    Correct answer: Picks one of the types depending on whether T is assignable to string

    Conditional types are a type-level "ternary". Together with infer, they let you extract parts of types. Applied to a union, they distribute over each member (distributive conditional types).

  4. What does infer do in type ElementType<T> = T extends (infer U)[] ? U : never?

    Correct answer: Declares a type variable U that TS infers from matching the pattern

    infer is used only in the extends part of a conditional type: TS matches T against the pattern and "captures" the needed part. ElementType<string[]> → string. That's how ReturnType, Parameters and Awaited work.

All 75 questions with answers →

TypeScript · Senior

  1. With strictFunctionTypes, can you assign (x: Dog) => void to a variable of type (x: Animal) => void?

    Correct answer: No: function parameters are checked contravariantly

    The variable promises to accept any Animal, but the function can only handle Dog - a call with Cat would break. So subtyping by parameter goes the opposite way: (x: Animal) => void can be assigned where (x: Dog) => void is expected, but not vice versa.

  2. Why doesn't strictFunctionTypes apply to a method declared as handle(x: Dog): void?

    Correct answer: Method-syntax parameters are checked bivariantly for compatibility

    Method bivariance was kept so that many built-in types (e.g. Array<T>) stay compatible. If you declare a function property handle: (x: Dog) => void instead, the check is strict. That's why property syntax is often preferred for callbacks in interfaces.

  3. What is ToArray<string | number> if type ToArray<T> = T extends any ? T[] : never?

    Correct answer: string[] | number[]

    A conditional type over a "naked" type parameter distributes over a union: the condition is applied to each member separately. This is a distributive conditional type - Exclude and Extract are built on it.

  4. How do you turn off a conditional type's distribution over a union?

    Correct answer: Wrap both sides in a tuple: [T] extends [U] ? X : Y

    Distribution happens only when the checked type is a "naked" parameter. The tuple [T] makes it composite, so the union is checked as a whole: [string | number] extends [string] → false. There's no compiler setting for this.

All 75 questions with answers →

Combined quizzes with this topic

Questions from several topics at once - just like an interview for a specific role.

Coding tasks

Write your solution right in the browser, and tests check it instantly. Interview classics: debounce, throttle, deepClone, Promise.all, EventEmitter, an LRU cache and more.

Open tasks →