TypeScript Financial Management & Budgeting 5 — Questions and Answers
Question 1: You're building a TypeScript generic `Ledger<T>` class for financial records. Which constraint ensures T always has an `id` and `amount` property?
- class Ledger<T extends object>
- class Ledger<T implements {id: any; amount: number}>
- class Ledger<T extends {id: any; amount: number}> (Correct answer)
- class Ledger<T = {id: any; amount: number}>
Correct answer: class Ledger<T extends {id: any; amount: number}>
Generic constraints use `extends` to require T to be a structural subtype of the specified shape.
Question 2: A TypeScript codebase uses `strict: true` in tsconfig. Which behavior does this enable that would catch a missing budget category check?
- Disallows all use of the any type
- Enables strictNullChecks, preventing null/undefined from being assigned to non-nullable types (Correct answer)
- Requires every function to have a return type annotation
- Prevents use of JavaScript files in the project
Correct answer: Enables strictNullChecks, preventing null/undefined from being assigned to non-nullable types
`strict: true` enables `strictNullChecks` among others, requiring explicit handling of null and undefined values.
Question 3: Which TypeScript `tsconfig` option should be set to `true` to ensure your financial module is compiled to a format compatible with both Node.js and modern bundlers?
- esModuleInterop (Correct answer)
- isolatedModules
- allowSyntheticDefaultImports
- resolveJsonModule
Correct answer: esModuleInterop
`esModuleInterop` generates helper code for CommonJS/ESM interop and enables default import compatibility across module systems.
Question 4: In a TypeScript financial app, you define `type Currency = 'USD' | 'EUR' | 'GBP'`. A switch statement handles each case. TypeScript will warn if:
- You use === instead of ==
- A new currency is added to the union but the switch has no matching case (with exhaustiveness check) (Correct answer)
- The default case is missing
- The cases are not in alphabetical order
Correct answer: A new currency is added to the union but the switch has no matching case (with exhaustiveness check)
Exhaustiveness checking (using a `never` type in the default case) causes TypeScript to error when a union member is not handled.
Question 5: A TypeScript interface `IFinancialService` is implemented by `BudgetService`. You call `service.processPayment()` where `service: IFinancialService`. This demonstrates:
- Nominal typing — BudgetService is valid because it explicitly implements the interface
- Structural typing — BudgetService is valid because its shape matches IFinancialService (Correct answer)
- Duck typing — only checked at runtime
- Both nominal and structural typing simultaneously
Correct answer: Structural typing — BudgetService is valid because its shape matches IFinancialService
TypeScript uses structural (duck) typing: compatibility is determined by the shape of types, not their declared names.
Question 6: When decorating a TypeScript financial service method with `@log`, what TypeScript compiler option must be enabled?
- experimentalDecorators (Correct answer)
- emitDecoratorMetadata
- useDefineForClassFields
- allowJs
Correct answer: experimentalDecorators
`experimentalDecorators: true` in tsconfig is required to use the decorator syntax in TypeScript.
Question 7: A budget summary object is typed as `Readonly<{ total: number; breakdown: number[] }>`. Which operation will TypeScript reject?
- Reading summary.total
- Passing summary to a function expecting a mutable object
- summary.breakdown.push(500) (Correct answer)
- Spreading summary into a new object
Correct answer: summary.breakdown.push(500)
`Readonly<T>` makes top-level properties readonly, but the `breakdown` array itself is still mutable — however, pushing to it is allowed; the assignment `summary.breakdown = []` would be rejected, not push.
You're building a TypeScript generic `Ledger` class for financial records.
Which constraint ensures T always has an `id` and `amount` property?