Top 50 TypeScript Interview Questions (2025)
TypeScript Fundamentals (1–15)
1. What is TypeScript and why use it? TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript. It catches type errors at compile time instead of runtime, improves IDE tooling (autocomplete, refactoring), and makes large codebases easier to maintain and reason about.
2. What is the difference between type and interface? Both describe object shapes. interface supports declaration merging and is better for object/class contracts. type supports unions, intersections, mapped types, and conditional types — making it more versatile. Prefer interface for public API shapes; type for complex compositions.
3. What is type inference in TypeScript? TypeScript automatically deduces the type of a value from its initializer, return expression, or context. const x = 5 infers number. Inference reduces annotation noise while retaining full type safety.
4. What is the any type and why is it dangerous? any opts a variable out of type checking completely. It disables autocomplete, silences errors, and removes all benefits of TypeScript. Use unknown instead — it forces you to narrow the type before using the value.
5. What is the difference between any and unknown? unknown is the type-safe alternative to any. You can assign anything to unknown, but you cannot use it without first narrowing it (typeof, instanceof, a type guard). any bypasses all checks entirely.
6. What is a union type? A union type (A | B) means a value can be one of the listed types. TypeScript narrows unions through type guards, discriminated unions, or control flow analysis.
7. What is an intersection type? An intersection type (A & B) combines multiple types into one — the result must satisfy all of them. Commonly used to merge interfaces or mix in behaviours.
8. What are generics in TypeScript? Generics are type parameters that make components reusable over many types while preserving type safety. function identity
9. What are TypeScript utility types? Built-in generic types that transform other types. Partial
10. What is a type guard? A runtime check that narrows a union type. typeof x === 'string', instanceof MyClass, or a custom x is MyType predicate function are all type guards.
11. What is a discriminated union? A union where each member has a common literal property (the discriminant). switch/if on the discriminant narrows to the exact member: type Result = { ok: true; value: T } | { ok: false; error: string }.
12. What is the never type? never represents values that can never exist. Exhaustive switch statements use never as a default — if TypeScript allows assigning to never, you've handled all cases. Functions that always throw or loop forever have return type never.
13. What is readonly in TypeScript? readonly prevents a property from being reassigned after initialization. Readonly
14. What is a tuple type? A fixed-length array where each position has a specific type: [string, number] is a two-element array of a string then a number. Useful for function return types and structured data pairs.
15. What is the satisfies operator (TypeScript 4.9)? satisfies validates that an expression matches a type without widening its inferred type. const palette = { red: [255,0,0], blue: '#0000ff' } satisfies Record
Generics (16–25)
16. How do you constrain a generic? Use extends: function getLength
17. What is a conditional type? T extends U ? X : Y — if T is assignable to U, the type is X, else Y. Used in utility types: NonNullable
18. What are mapped types? Transform every property of an existing type: { [K in keyof T]: NewType }. Built-in examples are Readonly
19. What is keyof? keyof T produces a union of all property names of T as string | number | symbol literal types. function get
20. What is typeof in type position? At the type level, typeof variable extracts the type of a value. type Config = typeof defaultConfig. Useful when you have a const object and want to derive its type without manually writing it.
21. What is a template literal type? Constructs new string literal types via template syntax: type EventName = on${Capitalize matches 'onClick', 'onChange', etc.
22. What is infer in conditional types? infer introduces a type variable inside a conditional type: type ReturnType
23. What is a recursive type? A type that references itself. type NestedArray
24. What is function overloading in TypeScript? Multiple function signatures followed by one implementation signature. TypeScript shows callers only the overload signatures — the implementation signature is internal. Useful for functions with multiple call patterns returning different types.
25. What is a generic default? Type parameters can have defaults: function makeState
Classes and Access Modifiers (26–35)
26. What are access modifiers in TypeScript? public (default), protected (class + subclasses), private (class only), and readonly. TypeScript's private is erased at runtime — use # (ES private fields) for runtime enforcement.
27. What is the difference between abstract class and interface? Abstract classes can have implementation (method bodies, constructors, instance fields) and use access modifiers. interfaces are purely structural. A class can implement many interfaces but extend only one abstract class.
28. What is parameter property shorthand? constructor(private name: string) simultaneously declares and assigns the name field, removing boilerplate. Equivalent to declaring the field and assigning in the constructor body.
29. What are decorators in TypeScript? Annotations (@) applied to classes, methods, properties, or parameters that can add metadata or modify behaviour. Used extensively in Angular and NestJS. The official decorators proposal (Stage 3) is enabled with --experimentalDecorators.
30. What is the override keyword? Added in TypeScript 4.3. Marking a method override ensures it actually overrides a parent method — TypeScript errors if the parent method doesn't exist or has a different signature. Prevents silent drift when parent methods are renamed.
Types vs Interfaces Advanced (31–40)
31. What is declaration merging with interface? Declaring the same interface name twice merges their members. Used to extend third-party types: declare module 'express' { interface Request { user?: User } }. type does not support merging.
32. What are index signatures? { [key: string]: number } allows any string key with number values. Useful for dynamic objects. Use Record
33. What is Exclude and Extract? Exclude
34. What is ReturnType
35. What is Parameters
Advanced TypeScript Patterns (36–50)
36. What is a branded type? A nominal type created by intersecting a base type with a unique marker: type UserId = string & { __brand: 'UserId' }. Prevents accidentally mixing IDs of different kinds even though both are strings.
37. What is the Builder pattern with TypeScript? Chaining methods that each return this (or a narrowed subtype) to build an object incrementally with full type safety at each step.
38. What is variance in TypeScript generics? Covariance means you can use a subtype where a supertype is expected. Contravariance means the reverse. TypeScript uses structural typing so variance is inferred — function parameters are contravariant, return types are covariant.
39. What are assertion functions? function assertIsString(val: unknown): asserts val is string — TypeScript narrows val to string for the rest of the scope after calling this function, similar to type guards but for void functions that throw on failure.
40. What is declaration files (.d.ts)? Type-only files that describe the shape of JavaScript modules without implementation. Published as part of a library (or via @types/... on DefinitelyTyped) so TypeScript knows a module's API even if the source is plain JS.
41. What is strict mode in TypeScript? The --strict flag enables: noImplicitAny, strictNullChecks, strictFunctionTypes, strictPropertyInitialization, noImplicitThis, and alwaysStrict. strictNullChecks is the most impactful — null and undefined are not assignable to other types, eliminating null reference errors.
42. What is strictNullChecks? With strictNullChecks, null and undefined are their own types and not assignable to anything else. The non-null assertion operator (!) overrides the check: value! tells TypeScript you're sure it's not null.
43. What is the as keyword? Type assertion: tells TypeScript to treat a value as a specific type, overriding inference. Use sparingly — as unknown as T is a double assertion that bypasses all safety. Prefer type narrowing when possible.
44. What is module augmentation? Extending types of external modules without modifying their source: declare module 'lodash' { interface LoDashStatic { myFn(): void } }. Merged into the original module's types.
45. What is the difference between type widening and narrowing? Widening is TypeScript broadening an inferred type (let x = 'hello' → string). Narrowing is TypeScript restricting a type based on runtime checks (typeof, instanceof). as const prevents widening.
46. What is const assertion? as const makes all inferred types as narrow as possible: const dirs = ['north','south'] as const — type is readonly ['north', 'south'] not string[]. Essential for discriminated unions with literal types.
47. What is a type predicate? function isString(x: unknown): x is string — the x is string part tells TypeScript to narrow x to string in the true branch when this function is used as a condition.
48. What is the TypeScript project references feature? Splits a large project into independently compiled sub-projects. Each subproject has its own tsconfig with references to its dependencies. Enables incremental compilation — only changed subprojects recompile.
49. What is the difference between interface extending and type intersecting an interface? interface B extends A copies A's shape into B's scope with inheritance semantics (conflict detection, proper error messages). type B = A & { extra: string } is purely structural and can combine types in more flexible ways without inheritance.
50. What is the TypeScript compiler API? A programmatic API to parse, type-check, and transform TypeScript code. Used by build tools, linters, code generators, and codemods. ts.createProgram() creates a program; diagnostics give compiler errors; the type checker resolves types.
---
Practice all 50 with AI feedback at [JSPrep Pro](/auth).