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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
const greeting: string = "Hello";
console.log(greeting);- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
npx tsc --noEmit- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
const count = 10;
const label = "ready";- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
function upper(value: string | undefined) {
return value === undefined ? "" : value.toUpperCase();
}- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
function read(value: unknown) {
if (typeof value === "string") return value.toUpperCase();
return "not a string";
}- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
function unreachable(value: never): never {
throw new Error(String(value));
}- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
type A = { id: string };
type B = { active: boolean };
type Both = A & B;- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
type Result = { ok: true; data: string } | { ok: false; error: string };- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
function isString(value: unknown): value is string {
return typeof value === "string";
}- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
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.
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
type Status = "idle" | "loading" | "done";- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Which construct naturally names a union like `"on" | "off"`?