← All topics

TypeScript Middle interview questions and answers

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

Take this quiz
  1. 1.What is a user-defined type guard?
  2. 2.How do you guarantee that a switch handled every member of a union?
  3. 3.What does <T extends { id: string }> mean in a generic?
  4. 4.How do you type getProp(obj, key) so the result depends on the key?
  5. 5.What is a mapped type?
  6. 6.What does the conditional type T extends string ? 'text' : 'other' do?
  7. 7.What does infer do in type ElementType<T> = T extends (infer U)[] ? U : never?
  8. 8.What does Awaited<Promise<Promise<number>>> return?
  9. 9.How does Exclude<T, U> differ from Omit<T, K>?
  10. 10.How do function overloads work in TypeScript?
  11. 11.What is declaration merging?
  12. 12.What is an abstract class for?
  13. 13.How does protected differ from private in TypeScript classes?
  14. 14.What does the satisfies operator do?
  15. 15.What are template literal types?
  16. 16.Why is an as const object often used instead of an enum?
  17. 17.How does const enum differ from a regular enum?
  18. 18.How do namespaces (namespace) differ from ES modules?
  19. 19.What is an assertion function?
  20. 20.How do you get a native button's props to extend them in your component?
  21. 21.How do you type Redux Toolkit hooks in a TypeScript app?
  22. 22.How do you type a generic list component?
  23. 23.What does Extract<'a' | 'b' | 1 | 2, string> produce?
  24. 24.What does NonNullable<string | null | undefined> return?
  25. 25.How do object, {} and Record<string, unknown> differ as types?
  26. 26.How does TypeScript handle the in operator when narrowing?
  27. 27.What do -readonly and -? do in a mapped type?
  28. 28.How do you rename keys in a mapped type?
  29. 29.Why does Object.keys(user) return string[] rather than (keyof User)[]?
  30. 30.What does the noUncheckedIndexedAccess flag give you?
  31. 31.How does unknown[] differ from any[] in a function parameter?
  32. 32.What are decorators in TypeScript 5?
  33. 33.What does import type { User } from './types' do?
  34. 34.Why is interface preferable to an intersection for extending large object types?
  35. 35.What does Parameters<(a: string, b?: number) => void> return?
  36. 36.How do you specify the type of this for a handler function?
  37. 37.What's the best way to type an API response whose structure differs by status?
  38. 38.What does the ReadonlyArray<T> utility (or readonly T[]) do?
  39. 39.What is unique symbol?
  40. 40.How do you extend the type of the global Window object from a module?
  41. 41.How do you type the result of JSON.parse safely?
  42. 42.How does type A = B differ from interface A extends B {} in IDE tooltips?
  43. 43.What does paths in tsconfig.json give you?
  44. 44.How does moduleResolution: 'bundler' differ from 'node16'/'nodenext'?
  45. 45.What does the isolatedModules flag do?
  46. 46.What does keyof typeof obj do?
  47. 47.When does TS widen a literal type to a general one?
  48. 48.How do you make a generic parameter optional with a default value?
  49. 49.How do you type a factory function that returns the same type it received?
  50. 50.What is contextual typing?
  51. 51.Why does Omit<Circle | Square, 'id'> lose the discriminated union?
  52. 52.What does as const satisfies Record<string, Route> give you for a config object?
  53. 53.When should you prefer a union parameter over overloads?
  54. 54.What are parameter properties, and why are they not "erasable" syntax?
  55. 55.What do the override keyword and noImplicitOverride protect against?
  56. 56.Why might Readonly<Settings> not prevent this change?
  57. 57.How do you derive a TypeScript type from a Zod schema?
  58. 58.What does type-aware linting with typescript-eslint add on top of tsc?
  59. 59.How do you share one strict base configuration between many packages?
  60. 60.How do target and lib differ in tsconfig.json?
  61. 61.What does esModuleInterop fix?
  62. 62.Which option lets you import config from './config.json' with typed contents?
  63. 63.How do you get the instance type and constructor arguments of a class?
  64. 64.How do you narrow the results of Promise.allSettled?
  65. 65.What do the intrinsic types Uppercase, Capitalize etc. allow?
  66. 66.What do noImplicitReturns and noFallthroughCasesInSwitch catch?
  67. 67.How do you type a generator function?
  68. 68.How do you type a generic data-fetching hook?
  69. 69.When is Map<K, V> a better choice than an index signature object?
  70. 70.What do the types/typesVersions and the exports "types" condition in package.json do?
  71. 71.What does export type * from './types' do (TS 5.0+)?
  72. 72.How do you write a type for any JSON value?
  73. 73.How do you implement a type-safe event emitter?
  74. 74.Does narrowing work when a discriminated union is destructured first?
  75. 75.What does switch (true) narrowing (TS 5.3) enable?