TypeScript Financial Management & Budgeting 4 — Questions and Answers
Question 1: You're implementing a financial report generator as a TypeScript class. Which keyword declares a method that subclasses MUST implement?
- virtual
- abstract (Correct answer)
- override
- interface
Correct answer: abstract
The `abstract` keyword on a method in an abstract class forces all concrete subclasses to provide their own implementation.
Question 2: A payroll TypeScript module exports `calculateGross` and `calculateNet`. To import ONLY `calculateNet` with a renamed alias `netPay`, the correct syntax is:
- import { calculateNet } as netPay from './payroll'
- import { calculateNet: netPay } from './payroll' (Correct answer)
- import netPay = calculateNet from './payroll'
- import calculateNet as netPay from './payroll'
Correct answer: import { calculateNet: netPay } from './payroll'
Named import aliasing uses `{ originalName: alias }` syntax inside the import braces.
Question 3: Which TypeScript mapped type creates a version of `BudgetItem` where every property's type is wrapped in a `Promise`?
- Async<BudgetItem>
- { [K in keyof BudgetItem]: Promise<BudgetItem[K]> } (Correct answer)
- Promise<keyof BudgetItem>
- Awaited<BudgetItem>
Correct answer: { [K in keyof BudgetItem]: Promise<BudgetItem[K]> }
A mapped type iterates over all keys K of T and maps each value type to a new type, here wrapping each in `Promise<>`.
Question 4: In a TypeScript budget app, you use `as const` on an array of expense categories. What effect does this have?
- Converts the array to a tuple with readonly literal types (Correct answer)
- Makes the array immutable at runtime
- Prevents any type inference
- Marks the array as a class-level constant
Correct answer: Converts the array to a tuple with readonly literal types
`as const` narrows the type to a readonly tuple with exact literal types, rather than a mutable `string[]`.
Question 5: A TypeScript financial service uses dependency injection. The constructor accepts `logger: ILogger`. This pattern is beneficial because:
- It forces the logger to be a class, not an interface
- It allows any object matching ILogger to be injected, improving testability (Correct answer)
- It prevents the logger from being undefined at runtime
- TypeScript interfaces are faster at runtime than classes
Correct answer: It allows any object matching ILogger to be injected, improving testability
Programming to interfaces decouples the service from concrete implementations, allowing mock loggers in tests and real ones in production.
Question 6: You want to extract the return type of an existing async financial calculation function `calculateROI`. Which utility type achieves this?
- Parameters<typeof calculateROI>
- Awaited<ReturnType<typeof calculateROI>> (Correct answer)
- ReturnType<calculateROI>
- InstanceType<typeof calculateROI>
Correct answer: Awaited<ReturnType<typeof calculateROI>>
`ReturnType<>` extracts the return type (a Promise), and `Awaited<>` unwraps the Promise to get the resolved value type.
Question 7: A budget dashboard component receives `data: Transaction[] | undefined`. To safely map over it without a null check, which operator is used?
- data!.map(...)
- data?.map(...) (Correct answer)
- data ?? [].map(...)
- data || [].map(...)
Correct answer: data?.map(...)
Optional chaining (`?.`) short-circuits and returns `undefined` if `data` is undefined, avoiding a runtime TypeError.
You're implementing a financial report generator as a TypeScript class.
Which keyword declares a method that subclasses MUST implement?