Episode 1 — Fundamentals / 1.25 — TypeScript Essentials
1.25 — Exercise Questions: TypeScript Essentials
Practice questions for all twelve subtopics in Section 1.25. Mix of short-answer, prediction, code-writing, and configuration tasks.
How to use this material (instructions)
- Read lessons in order —
README.md, then1.25.athrough1.25.l. - Answer closed-book first — then compare to the matching lesson.
- Type it out — use the TypeScript Playground or a local setup to verify your answers.
- Interview prep —
1.25-Interview-Questions.md. - Quick review —
1.25-Quick-Revision.md.
1.25.a — What is TypeScript (Q1–Q5)
Q1. Define TypeScript in one sentence.
Q2. What is the relationship between TypeScript and JavaScript? Is every JS file valid TS?
Q3. What happens to type annotations when TypeScript compiles to JavaScript?
Q4. Can TypeScript code run directly in a browser? Explain why or why not.
Q5. Name three scenarios where TypeScript is preferred over plain JavaScript, and two where plain JS is fine.
1.25.b — Thinking in TypeScript (Q6–Q10)
Q6. What does "types-first mindset" mean? How does it differ from the typical JS approach?
Q7. Given this error message, explain what went wrong:
error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
Q8. Why is TypeScript described as "a linter on steroids"?
Q9. What is "gradual typing" and how does it help when migrating a large JS project?
Q10. You have a function that fetches a user. Write only its type signature (no implementation) using a types-first approach:
- Takes a user ID (number)
- Returns a Promise of a User object (with
id,name,email) or null
1.25.c — Installing and Setting Up (Q11–Q16)
Q11. What is the difference between npm install -D typescript and npm install -g typescript?
Q12. What command creates a tsconfig.json file?
Q13. What does npx tsc --watch do?
Q14. What is the difference between ts-node and tsx? When would you choose each?
Q15. You have an existing JavaScript project. List the three key tsconfig.json settings you would enable for a gradual migration.
Q16. What are @types/* packages? Give an example of when you need one and when you do not.
1.25.d — Basic Types (Q17–Q28)
Q17. Write the type annotation for each variable:
let city = ???; // "Paris"
let population = ???; // 2161000
let isCapital = ???; // true
Q18. What is the difference between any and unknown?
Q19. What does void mean as a return type? How is it different from undefined?
Q20. When does a function have a return type of never?
Q21. What is the difference between string[] and Array<string>?
Q22. Define a tuple type for a coordinate: latitude (number) and longitude (number).
Q23. Write a string enum called LogLevel with values DEBUG, INFO, WARN, ERROR.
Q24. What is a literal type? Write a type that only accepts "success" or "failure".
Q25. What is type inference? When can you skip writing a type annotation?
Q26. What does as const do when applied to an object or array?
Q27. What is the difference between value as string and <string>value? When can you NOT use the second form?
Q28. Given let items: string[] = [], why is the type annotation needed here?
1.25.e — Interfaces vs Type Aliases (Q29–Q36)
Q29. Write an interface for a Product with id (number), name (string), price (number), and optional description (string).
Q30. Write the same shape as a type alias instead of an interface.
Q31. Create an interface DiscountedProduct that extends Product with an additional discount (number) property.
Q32. What is declaration merging? Which one supports it — interface or type?
Q33. Can a type alias define a union type? Can an interface? Write an example.
Q34. What does readonly do on a property? Is it deep or shallow?
Q35. Write an index signature for an object where keys are strings and values are numbers.
Q36. When would you use interface and when would you use type? List two situations for each.
1.25.f — Union and Intersection Types (Q37–Q44)
Q37. What does the type string | number mean?
Q38. You have a variable of type string | number. Can you call .toUpperCase() on it directly? Why or why not?
Q39. Write a typeof type guard that narrows string | number and calls the appropriate method on each.
Q40. Write a discriminated union for two shapes: Circle (with radius) and Square (with side). Include a type discriminant property.
Q41. Write a function that uses a switch on the discriminant to calculate the area of each shape. Include an exhaustive check with never.
Q42. What is the difference between string | number (union) and A & B (intersection)?
Q43. What does strictNullChecks do? Why should you enable it?
Q44. What is the difference between name?: string and name: string | undefined?
1.25.g — Generic Functions (Q45–Q51)
Q45. What problem do generics solve? Why not just use any?
Q46. Write a generic function wrap<T> that takes a value and returns it in an object { value: T }.
Q47. Write a generic function first<T> that takes an array T[] and returns the first element or undefined.
Q48. Write a generic function with two type parameters: makePair<A, B>(a: A, b: B): [A, B].
Q49. Does TypeScript require you to specify <string> when calling identity("hello")? Why or why not?
Q50. Write the generic arrow function syntax for the identity function. How do you fix the JSX ambiguity in .tsx files?
Q51. Name three built-in TypeScript types that use generics.
1.25.h — Generic Interfaces and Constraints (Q52–Q59)
Q52. Write a generic interface ApiResponse<T> with properties data: T, status: number, and error: string | null.
Q53. What does <T extends { length: number }> mean? Give an example of a type that satisfies it and one that does not.
Q54. What does keyof User produce if User has properties id, name, and email?
Q55. Write a mapped type that makes all properties of T optional (without using Partial).
Q56. What does Partial<User> do? Write the equivalent mapped type.
Q57. What is the difference between Pick<User, "name" | "email"> and Omit<User, "id">?
Q58. Write a Record<string, number> and explain what it means.
Q59. What is a default type parameter? Write a generic type Container<T = string>.
1.25.i — TypeScript Tooling (Q60–Q64)
Q60. Name five things the TypeScript Language Server provides in your editor.
Q61. What is the TypeScript Playground, and when would you use it?
Q62. What is the difference between tsx and ts-node?
Q63. What is a .d.ts file? When do you encounter them?
Q64. What do source maps do, and why are they useful for TypeScript projects?
1.25.j — tsconfig Explained (Q65–Q72)
Q65. What command creates a tsconfig.json file?
Q66. What does target: "ES2020" control?
Q67. What does module: "commonjs" vs module: "ESNext" control?
Q68. What does strict: true enable? Name three specific checks it includes.
Q69. When would you set noEmit: true in tsconfig?
Q70. What is the difference between outDir and rootDir?
Q71. Write a tsconfig.json suitable for a React project using Vite (include at least 6 key options).
Q72. What does extends do in tsconfig? Give an example.
1.25.k — TypeScript Compiler (Q73–Q78)
Q73. What are the two jobs that tsc performs?
Q74. True or false: tsc will NOT produce .js files if there are type errors. Explain.
Q75. What does tsc --noEmit do? When would you use it?
Q76. Why do modern bundlers like Vite and esbuild NOT catch TypeScript type errors?
Q77. Compare tsc, ts-node, and tsx in a table with columns: type-checks?, produces files?, speed.
Q78. What is incremental compilation and how do you enable it?
1.25.l — Linting and Formatting (Q79–Q86)
Q79. What is the difference between what ESLint catches and what TypeScript catches?
Q80. What two @typescript-eslint packages do you need for ESLint with TypeScript?
Q81. What does the @typescript-eslint/no-explicit-any rule do?
Q82. What is the difference between ESLint and Prettier?
Q83. Why do you need eslint-config-prettier?
Q84. How do husky and lint-staged work together?
Q85. Write a lint-staged configuration that runs ESLint fix and Prettier on .ts files.
Q86. List the recommended order of checks in a CI pipeline for a TypeScript project.
Answer hints
| Q | Hint |
|---|---|
| Q3 | Type annotations are stripped — output is plain JS |
| Q18 | any skips checking; unknown requires narrowing |
| Q28 | Empty array — TypeScript cannot infer the element type |
| Q32 | Only interface supports declaration merging |
| Q38 | No — must narrow first; only common methods available |
| Q44 | ? means property can be absent; | undefined means property must be present |
| Q49 | No — TypeScript infers the type from the argument |
| Q56 | { [K in keyof T]?: T[K] } |
| Q74 | False — tsc emits even with errors (use --noEmitOnError to prevent) |
| Q76 | They only strip types without analyzing them |
← Back to 1.25 — TypeScript Essentials (README)