53 Topics
Chapter•180 Minutes

TypeScript Interview Questions and Answers

A 50-question interview bank covering core concepts, type system behavior, generics, compiler settings, runtime boundaries, and production design. Each question includes a detailed answer and a knowledge check.

Q01. What is TypeScript, and how does it relate to JavaScript?

TypeScript is a statically checked superset of JavaScript that adds a type system and compile-time tooling. It is typically transformed into JavaScript for execution. Its types are erased, so runtime validation must be implemented separately for untrusted values.

TypeScript source is checked and transformed; JavaScript runs in the runtime. Types improve static correctness but do not add automatic runtime validation.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

TypeScript compilation
const greeting: string = "Hello";
console.log(greeting);
💡 The type annotation is removed from emitted JavaScript; the runtime executes the string value.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

Which language normally executes in a browser after standard TypeScript compilation?

Q02. What is the difference between type checking and transpilation?

Type checking analyzes whether expressions satisfy their types. Transpilation transforms syntax into a target language or version. Some tools transpile TypeScript without performing full type checking, so projects may need a separate tsc --noEmit step.

A fast transpiler may emit JavaScript even if the project has type errors. CI should explicitly run the type checker when the build tool does not.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Separate type-check step
npx tsc --noEmit
💡 This checks types without emitting output, which is useful when a separate tool transpiles source.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

Which command is commonly used to type-check without emitting files?

Q03. What is type inference?

Type inference is the compiler's ability to derive types from initializers, expressions, and contextual information. It reduces annotation noise while preserving static checking. Public boundaries and complex APIs may still benefit from explicit annotations.

The compiler infers count as a number from its initializer. Contextual typing can infer callback parameters from the API receiving the callback.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Inference
const count = 10;
const label = "ready";
💡 The compiler derives useful types from initializers.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

What is inferred for `const count = 10` in ordinary code?

Q04. What does strictNullChecks do?

It treats null and undefined as distinct types rather than allowing them wherever a value of another type is expected. Code must narrow nullable values or provide a fallback before using them as non-null values.

With strict null checks, string | undefined is not interchangeable with string; guard it or use a deliberate default.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Narrow nullish values
function upper(value: string | undefined) {
  return value === undefined ? "" : value.toUpperCase();
}
💡 The conditional narrows value to string in the final branch.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

How should code handle a possibly undefined string before calling `toUpperCase()`?

Q05. What is the difference between any and unknown?

any disables most checking for a value. unknown can hold any value but requires narrowing before unsafe operations. Prefer unknown at untrusted boundaries and validate it into a known domain type.

With unknown, accessing .name directly is rejected until the program checks the shape. With any, the same access is allowed even if it fails at runtime.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Unknown requires proof
function read(value: unknown) {
  if (typeof value === "string") return value.toUpperCase();
  return "not a string";
}
💡 The runtime check proves the value is a string before the string method is used.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

Which type forces callers to narrow before accessing arbitrary properties?

Q06. What is never used for?

never represents a value that cannot occur. It appears as the return type of functions that never complete normally and as the remainder of an exhaustively narrowed union. Assigning a supposedly impossible value to never can detect missing union cases.

When every Shape variant is handled, the default branch should be never. Adding a variant without a case then becomes a compile-time error.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Exhaustive check
function unreachable(value: never): never {
  throw new Error(String(value));
}
💡 The never parameter is a useful compile-time signal for impossible cases.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

What should the remaining value of a fully exhausted discriminated union be?

Q07. What is the difference between a union and an intersection?

A union A | B means a value may satisfy either alternative; only safe shared members are available before narrowing. An intersection A & B requires a value to satisfy both types. Use unions for alternatives and intersections for compatible composition.

A union models different states; an intersection combines requirements. Conflicting intersection properties can create an impossible type.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Union and intersection
type A = { id: string };
type B = { active: boolean };
type Both = A & B;
💡 Both requires id and active, whereas A | B permits either alternative.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

Which type requires a value to satisfy both A and B?

Q08. What is a discriminated union?

A discriminated union is a union of object types sharing a property whose literal value identifies the variant. Switching on that property narrows the object and supports exhaustive handling. It is useful for async states, commands, events, and API results.

A status field with loading, success, and error variants lets each case require the fields appropriate to that state.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Discriminated union
type Result = { ok: true; data: string } | { ok: false; error: string };
💡 The literal ok property identifies the variant.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

Which field most commonly distinguishes variants in a discriminated union?

Q09. What is a type predicate?

A type predicate is a function return annotation such as value is User that tells TypeScript a true result narrows the value to a type. The implementation must genuinely check the required conditions because the compiler trusts the predicate declaration.

The predicate does not convert an object. It communicates a narrowing relationship to the compiler, so incorrect logic can create unsoundness.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Type predicate
function isString(value: unknown): value is string {
  return typeof value === "string";
}
💡 The predicate tells TypeScript how a true result narrows the input.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

What does `value is User` mean in a return annotation?

Q10. What is the difference between interface and type?

Both can describe object shapes. Interfaces support declaration merging and extension; type aliases can name unions, tuples, primitives, intersections, and other type expressions. For ordinary object contracts, either may work, so choose based on intent and project conventions.

Use an interface for an extendable object contract if that suits the API; use a type alias for unions such as status variants.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

Type alias union
type Status = "idle" | "loading" | "done";
💡 A type alias can name union types that interfaces cannot directly represent.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

Which construct naturally names a union like `"on" | "off"`?