← All topics

TypeScript Senior interview questions and answers

75 questions for a TypeScript (Senior) interview - with correct answers, explanations and links to the docs.

Take this quiz
  1. 1.With strictFunctionTypes, can you assign (x: Dog) => void to a variable of type (x: Animal) => void?
  2. 2.Why doesn't strictFunctionTypes apply to a method declared as handle(x: Dog): void?
  3. 3.What is ToArray<string | number> if type ToArray<T> = T extends any ? T[] : never?
  4. 4.How do you turn off a conditional type's distribution over a union?
  5. 5.Why does type IsNever<T> = T extends never ? true : false give never for IsNever<never>?
  6. 6.How do you implement DeepPartial<T>?
  7. 7.Why are branded types needed?
  8. 8.How do you keep only the function-valued properties of a type?
  9. 9.What does Split<'a/b/c'> return?
  10. 10.What are variadic tuple types?
  11. 11.What does the const modifier on a type parameter (TS 5.0) give you?
  12. 12.What is the NoInfer<T> utility (TS 5.4) for?
  13. 13.What does infer N extends number do in a conditional type?
  14. 14.In what order should function overloads be written?
  15. 15.How do you add a user field to the Express library's Request type?
  16. 16.What does the verbatimModuleSyntax flag require?
  17. 17.What does the erasableSyntaxOnly flag (TS 5.8) forbid?
  18. 18.What are Project References (references, composite) for?
  19. 19.What's the risk of enabling skipLibCheck: true?
  20. 20.How do you find the cause of slow type checking in a large project?
  21. 21.Can you pass a readonly string[] to a function that accepts string[]?
  22. 22.What does a this return type give you in a class method?
  23. 23.What are the in and out variance annotations on type parameters (TS 4.7) for?
  24. 24.Why can the template type ` ${A}-${B}-${C} ` over three large unions cause an error?
  25. 25.How does the UnionToIntersection trick work?
  26. 26.Why is Record<Status, string> more useful than { [k: string]: string } for a dictionary keyed by status?
  27. 27.What changed about type predicate inference in TS 5.5?
  28. 28.What are using declarations (TS 5.2) for?
  29. 29.What does (typeof users)[number] return if users: User[]?
  30. 30.What does the mapped type { [K in keyof T]: Box<T[K]> } produce if T is the tuple [string, number]?
  31. 31.How do you get the element type of a Set<T> or Map<K, V> from a value?
  32. 32.What is the declare keyword for?
  33. 33.How do you check a type in tests so that you get an error if there's NO compile error?
  34. 34.When is the double assertion x as unknown as T justified?
  35. 35.Why should you avoid the Function type for callbacks?
  36. 36.Why can't you write a.x for a: { x: string } | { y: number }?
  37. 37.What does the exactOptionalPropertyTypes flag change?
  38. 38.What does keyof (A | B) return if A = { id; name } and B = { id; price }?
  39. 39.How do you enable type checking in JS files without migrating to TS?
  40. 40.How do you type a parameter that accepts an abstract class constructor?
  41. 41.Is the narrowing of a let variable preserved inside a callback?
  42. 42.What is a homomorphic mapped type, and what does it preserve?
  43. 43.How do you type a Promise.all-like function over a tuple of promises?
  44. 44.Why is it recommended to annotate return types of exported functions in a library?
  45. 45.Why is isolatedDeclarations (TS 5.5) useful for monorepos?
  46. 46.How does T extends object differ from T extends {} as a generic constraint?
  47. 47.Why is extending an interface faster for the compiler than an intersection?
  48. 48.How do you type a reducer so that payload depends on the action's type?
  49. 49.What does T[keyof T] return for T = { a: string; b: number }?
  50. 50.What is declare module '*.svg' used for?
  51. 51.Why does this compile, and what's the soundness hole?
  52. 52.What does tail-recursion elimination for conditional types (TS 4.5) allow?
  53. 53.How do you extract route params from a path string type?
  54. 54.How does a type-safe builder accumulate information in its type?
  55. 55.TypeScript has no higher-kinded types. How do libraries emulate F<A> where F is generic?
  56. 56.What does the error "The inferred type of 'x' cannot be named without a reference to ..." mean?
  57. 57.Why does a dual ESM/CJS package need separate declaration files (e.g. .d.mts and .d.cts)?
  58. 58.What does rewriteRelativeImportExtensions (TS 5.7) enable?
  59. 59.What is the main goal of TypeScript's native (Go) port?
  60. 60.What do the typescript-eslint no-unsafe-* rules help with?
  61. 61.What does an assertion function require to work as a narrowing call?
  62. 62.What does a this is predicate do in a class method?
  63. 63.What is { id: string } & { id: number }?
  64. 64.How do you get the type of a module's exports without importing its values?
  65. 65.Why do excess property checks miss errors in this case?
  66. 66.Why can a large union of string literals slow down the compiler?
  67. 67.Why can a let variable lose its narrowing inside a callback but a const keeps it?
  68. 68.Why should a public library avoid enum in its API?
  69. 69.How do you type a function that preserves the exact keys of an object passed to it while transforming values?
  70. 70.How do you prevent consumers from depending on internal types of your library?
  71. 71.What problem can strictNullChecks: false in one package cause in a monorepo?
  72. 72.Why are explicit generic constraints better than casting inside a generic function?
  73. 73.What does Object.freeze return in terms of types?
  74. 74.How can you make a discriminated union exhaustive check also catch a missing case at the data layer?
  75. 75.Why is it risky to type an API client with generics alone, like get<T>(url): Promise<T>?