Skip to content
TypeScript ts interview 5 min read

Common TypeScript Interview Questions

These are the questions that come up in almost every TypeScript interview, regardless of seniority. They probe whether you understand the core type system rather than just the syntax. The answers below are short and to the point — exactly how you should deliver them in a live interview — with small code snippets where a concrete example settles the question faster than prose. Read them, then practice saying each answer out loud in under a minute.

Core questions

What is TypeScript and how does it relate to JavaScript?

TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript. Every valid .js file is valid TypeScript, but TypeScript adds a structural type system, interfaces, generics, enums, and modern language features that the compiler strips out during transpilation. The types exist only at compile time — there is no runtime type checking.

What is the difference between any and unknown?

Both accept any value, but unknown is type-safe. With any the compiler stops checking, so you can call anything on it. With unknown you must narrow the value before using it.

let a: any = "hello";
a.toUpperCase();        // allowed — no checking

let u: unknown = "hello";
// u.toUpperCase();     // ❌ Error: 'u' is of type 'unknown'
if (typeof u === "string") u.toUpperCase(); // ✅ narrowed

Prefer unknown for values of uncertain shape (API responses, catch clauses) and reserve any for genuine escape hatches.

What is the difference between interface and type?

Interfaces describe object shapes and support declaration merging and extends. Type aliases can do everything interfaces do for objects, plus express unions, intersections, tuples, mapped, and conditional types.

Featureinterfacetype
Object shapesYesYes
Unions / intersectionsNoYes
Declaration mergingYesNo
Extends / implementsYesVia &

Use interface for public, extendable object contracts; use type when you need unions or computed types.

What are generics and why are they useful?

Generics let you write reusable code that works over many types while preserving type information. A type parameter acts as a placeholder filled in at the call site.

function identity<T>(value: T): T {
  return value;
}
const n = identity(42);   // n: number
const s = identity("hi"); // s: string

Without generics you would fall back to any and lose the link between input and output types.

How do enums work in TypeScript?

An enum is a named set of constants. Numeric enums auto-increment from 0; string enums require explicit values. Regular enums emit runtime objects, so they exist in the compiled JavaScript.

enum Direction { Up, Down }          // numeric, emits an object
enum Status { Active = "ACTIVE" }    // string

const enum Color { Red, Green }      // inlined, no runtime object
type Role = "admin" | "user";        // union-of-literals alternative

Prefer const enum or a union of string literals when you want zero runtime cost.

What is type inference?

TypeScript infers types from context so you do not need to annotate everything. The initializer of a variable, the return of a function, and contextual typing of callbacks are all inferred.

let count = 5;                    // inferred number
const names = ["a", "b"];         // string[]
[1, 2, 3].map((x) => x * 2);      // x inferred as number

Annotate function parameters and public APIs; let inference handle local variables.

What are union and intersection types?

A union (A | B) is a value that is one of several types; an intersection (A & B) is a value that satisfies all the types at once.

type Id = string | number;            // union
type Staff = Person & Employee;       // intersection

You must narrow a union before using members specific to one branch.

What is a discriminated union?

A union of object types that share a common literal “tag” field. Checking the tag narrows the union to one exact variant, enabling safe, exhaustive handling.

type Shape =
  | { kind: "circle"; r: number }
  | { kind: "square"; side: number };

function area(s: Shape) {
  switch (s.kind) {
    case "circle": return Math.PI * s.r ** 2;
    case "square": return s.side ** 2;
  }
}

What does the readonly modifier do?

It marks a property or array as immutable after construction. The compiler rejects reassignment, but it is a compile-time guarantee only — nothing prevents mutation at runtime.

interface Point { readonly x: number; }
const p: Point = { x: 1 };
// p.x = 2; // ❌ Error: Cannot assign to 'x'

What are utility types? Name a few.

Built-in generic types that transform other types. Common ones: Partial<T> (all props optional), Required<T>, Readonly<T>, Pick<T, K>, Omit<T, K>, Record<K, V>, and ReturnType<F>.

type User = { id: number; name: string };
type Draft = Partial<User>;        // { id?: number; name?: string }
type NameOnly = Pick<User, "name">; // { name: string }

What is the never type?

never represents values that never occur — a function that always throws or never returns, or an impossible branch. It is the empty set and is assignable to every type, but nothing (except never) is assignable to it. It powers exhaustiveness checks.

function fail(msg: string): never { throw new Error(msg); }

What is structural typing?

TypeScript types are compatible based on their shape, not their declared name (“duck typing”). If an object has all the required members, it is assignable, regardless of which class or interface it came from.

interface Named { name: string; }
const dog = { name: "Rex", legs: 4 };
const n: Named = dog; // ✅ extra 'legs' is fine

Best Practices

  • Keep answers under a minute; lead with the definition, then a one-line example.
  • Always contrast any vs unknown — interviewers use it to gauge type-safety awareness.
  • Mention runtime impact of enum vs const enum vs union literals unprompted.
  • When asked “interface or type?”, give a rule of thumb, not a long debate.
  • Show you understand inference by saying where you would and would not annotate.
  • Use the word “structural” — it signals you grasp how TypeScript actually compares types.
  • Tie generics back to “preserving type information” rather than “reusability” alone.
Last updated June 29, 2026
Was this helpful?