🎁 Sign up now — get up to 30 minutes of online AI use free. No credit card required.

TypeScript Interview Questions and Answers (2026): Types, Generics, and Narrowing

October 9, 2026
20 TypeScript interview questions with answers, tested in strict mode: type vs interface, any vs unknown, unions, generics, utility types, and structural typing.
TypeScript Interview Questions and Answers (2026): Types, Generics, and Narrowing
typescript interview questions
typescript interview
type vs interface
any vs unknown typescript
typescript generics interview

TypeScript interviews test whether you understand the type system, beyond the syntax. Below are 20 questions with answers, from the basics to the ones that separate people who use TypeScript from people who know why it works. The code was compiled with the TypeScript compiler in strict mode before publishing, and the error messages quoted are the real ones.

Basics

1. What does TypeScript add to JavaScript?

A static type system that is checked at compile time and then erased. The output is plain JavaScript, so types have no runtime cost and no runtime presence. That last point matters: you can't check a type with typeof at runtime unless you write the check yourself.

2. What is the difference between type and interface?

Both describe object shapes and often work interchangeably. The practical differences:

interfacetype
Declaration mergingYes, two declarations mergeNo, a duplicate name is an error
Unions and primitivesCannot express themCan express them, such as a union of number and string
ExtendingextendsIntersection with &
Mapped and conditional typesNoYes

Declaring interface A twice merges the fields. Declaring type B twice produces error TS2300: Duplicate identifier 'B'. A common rule of thumb: use interface for public object shapes that others may extend, and type for everything else.

3. What is the difference between any, unknown, and never?

  • any turns off checking. Anything goes, and errors hide.
  • unknown is the safe version. You can assign anything to it, but you must narrow it before use. Calling x.toFixed() on an unknown fails with error TS18046: 'x' is of type 'unknown'.
  • never is the type of values that can't exist, such as a function that always throws. It's useful for exhaustive checks.

Prefer unknown over any for values whose type you don't control, such as parsed JSON.

4. What does strict mode do?

It turns on a group of checks, most importantly strictNullChecks (so null and undefined are separate types) and noImplicitAny. With it, using an optional value without checking gives error TS18048: 'x' is possibly 'undefined'. New projects should start with strict on.

5. What is the difference between readonly and const?

const stops a variable from being reassigned. readonly stops a property from being assigned. Writing to a readonly property gives error TS2540: Cannot assign to 'n' because it is a read-only property. Neither makes the underlying object deeply immutable.

Types in practice

6. What are union types and how do you narrow them?

A union means "one of these types". Narrowing uses checks the compiler understands:

type Id = number | string;
function describe(id: Id): string {
  if (typeof id === "string") return id.toUpperCase();
  return id.toFixed(1);
}

Inside the if, id is a string. After it, the compiler knows it's a number. Other narrowing tools are instanceof, the in operator, equality checks, and custom type guards.

7. What is a discriminated union?

A union where each member has a shared literal field, the discriminant, that tells them apart:

type Shape =
  | { kind: "circle"; r: number }
  | { kind: "sq"; s: number };

function assertNever(x: never): never {
  throw new Error("unexpected " + x);
}

function area(s: Shape): number {
  switch (s.kind) {
    case "circle": return Math.PI * s.r ** 2;
    case "sq": return s.s ** 2;
    default: return assertNever(s);
  }
}

If you later add a new shape and forget a case, the default branch stops compiling, because s is no longer never. That's an exhaustive check.

8. What are generics, and why use them?

They let a function or type work over many types while keeping the relationship between them:

function first<T>(xs: T[]): T | undefined { return xs[0]; }
function pick<T, K extends keyof T>(o: T, k: K): T[K] { return o[k]; }

first([1, 2]) returns number | undefined, not any. pick(user, "name") returns the type of name. The extends keyof T constraint stops you from passing a key that doesn't exist.

9. What are the common utility types?

UtilityWhat it does
Partial<T>Makes every property optional
Required<T>Makes every property required
Readonly<T>Makes every property readonly
Pick<T, K>Keeps only the listed keys
Omit<T, K>Removes the listed keys
Record<K, V>An object type with keys K and values V
ReturnType<F>The return type of a function type

10. What are mapped types?

They build a new type by looping over the keys of another. Partial itself is written as { [K in keyof T]?: T[K] }. Knowing that shows you understand utility types instead of memorizing them.

11. What is as const?

It tells the compiler to infer the narrowest type and mark everything readonly. const arr = [1, 2, 3] as const has the type readonly [1, 2, 3], and arr.push(4) fails with error TS2339: Property 'push' does not exist on type 'readonly [1, 2, 3]'. It's useful for lists of allowed values and for configuration objects.

12. What is a type assertion, and when is it dangerous?

value as Type tells the compiler to trust you. It performs no check at runtime. If you're wrong, the error shows up later as a crash. Prefer narrowing with a check, or a type guard function, over asserting.

Language behavior

13. What is the difference between null and undefined?

undefined means a value was never assigned, and null means "no value" set on purpose. With strictNullChecks, they're separate types. Use optional chaining (a?.b) and nullish coalescing (a ?? b) to handle them. Unlike ||, ?? only falls back for null and undefined, so 0 and the empty string are kept.

14. What is the non-null assertion !?

value! tells the compiler a value isn't null or undefined. Like as, it's unchecked. It's fine in a few places where you know better than the compiler, but a runtime check is usually safer.

15. What does keyof do, and what is typeof in a type position?

keyof T produces the union of the property names of T. In a type position, typeof x takes the type of a value, which avoids repeating it. Together, keyof typeof obj gives the keys of an object you defined as a value.

16. How do enums compare with union types?

Enums create a runtime object and values. A union of string literals, such as "idle" | "loading", is erased at compile time and needs no extra code. Many teams prefer string literal unions for simplicity and use enums when they need the runtime object.

Tooling and real projects

17. What does tsconfig.json do, and which options matter?

It tells the compiler which files to include and how to check them. Options interviewers ask about: strict, target (the JavaScript version to emit), module and moduleResolution, noEmit (check only, which is common when another tool builds), esModuleInterop, and skipLibCheck.

18. How do you type something that comes from outside, such as an API response?

Types vanish at runtime, so the compiler can't verify what the network returns. Treat the data as unknown, then validate it with a schema library or a hand-written type guard before using it. Asserting a type on parsed JSON hides bugs.

19. How do you migrate a JavaScript project to TypeScript?

Do it gradually. Add a tsconfig.json with allowJs, rename files one at a time, turn on stricter options over time, and add types at the boundaries first, such as API clients and shared utilities. Avoid a big-bang rewrite.

20. What is structural typing?

TypeScript compares types by shape, not by name. If an object has the fields a type requires, it's accepted, even if it was never declared as that type. It's different from languages that use nominal typing, where the declared name matters.

How to prepare

  1. Open a playground or a small project in strict mode and try each example above, including the ones that should fail.
  2. Be ready to write a generic function and a discriminated union from memory.
  3. Know why you'd choose unknown over any.
  4. Prepare one story about typing something awkward in a real project.

For related practice, see our guides to Node.js interview questions, Angular interview questions, and Python interview questions.

Practice aloud and in your own words. A transcript afterward shows where you rambled or skipped the point. Follow the employer's rules on AI tools in any live interview or assessment.

Practice your answers with YesToTheOffer

Rehearse out loud, get answers built from your own resume, and review your transcript afterward. 30 free minutes, no subscription.

iOS & Android — Coming Soon
See pricing