JobsPing

TypeScript interview practice

Covers types vs interfaces, generics, narrowing, utility types. Questions are written for real interviews at junior, mid and senior level.

Start a TypeScript practice test

15 multiple-choice questions · 15 minutes · instant score with explanations

Sample TypeScript interview questions

Three questions from the bank, with answers. The practice test draws different ones each time.

Question 1 · structural typing · junior

Why is the assignment of variable `d` to `a` valid in the given TypeScript code snippet?

interface Animal {
  name: string;
}
interface Dog extends Animal {
  breed: string;
}
let d: Dog = { name: "Rex", breed: "Lab" };
let a: Animal = d;
  1. A.The assignment is invalid because Animal has fewer properties than Dog (excess property check).
  2. B.TypeScript throws a compilation error because Dog was not declared with "implements Animal".
  3. C.TypeScript requires an explicit type assertion (as Animal) to allow this assignment.
  4. D.The assignment is valid because TypeScript uses structural typing, and Dog has all properties required by Animal.
Show answer

D. The assignment is valid because TypeScript uses structural typing, and Dog has all properties required by Animal.

TypeScript uses structural typing (duck typing), meaning types are compatible based on their shape rather than explicit implementation. Since 'Dog' contains all properties of 'Animal' plus an additional one, a 'Dog' can be safely assigned to an 'Animal'. A common beginner trap is thinking explicit inheritance or nominal typing (like in Java) is strictly required for assignment.

Question 2 · intersections and keyof · senior

What is the type of `D` when intersecting two object types?

type A = { a: string };
type B = { b: number };
type C = A & B;
type D = keyof C;
  1. A."a"
  2. B."a" | "b"
  3. C.never
  4. D."a" & "b"
Show answer

B. "a" | "b"

An intersection type `A & B` represents an object that has all properties of both `A` and `B`. Consequently, `keyof (A & B)` evaluates to the union of their keys: `"a" | "b"`. For unions (`A | B`), `keyof` would yield the intersection of keys, but for intersections, it yields the union of keys.

Question 3 · mapped types · senior

How does this recursive mapped type `Immutable<T>` behave?

type Immutable<T> = {
  readonly [K in keyof T]: T[K] extends object ? Immutable<T[K]> : T[K];
};
  1. A.It turns all properties into optional properties instead.
  2. B.It recursively makes all properties and nested object properties readonly.
  3. C.It fails to compile because `readonly` cannot be used inside mapped types.
  4. D.It only makes top-level properties readonly and ignores nested objects.
Show answer

B. It recursively makes all properties and nested object properties readonly.

Mapped types can iterate over keys using `[K in keyof T]` and apply modifiers like `readonly`. Recursively applying this creates a deep immutable type. The distractor options involve invalid mapped type syntax or failure to handle nested objects.

Ready to apply? Browse remote TypeScript jobs →