TypeScript Financial Management & Budgeting 2 — Questions and Answers
Question 1: In TypeScript, you have a `Budget` interface with optional fields. Which utility type creates a version where ALL fields are required?
- Partial<Budget>
- Required<Budget> (Correct answer)
- Readonly<Budget>
- NonNullable<Budget>
Correct answer: Required<Budget>
`Required<T>` maps all optional properties to required, the opposite of `Partial<T>`.
Question 2: You're building a financial ledger in TypeScript. Which numeric type concern is most critical when dealing with currency calculations?
- Using BigInt instead of number
- Floating-point precision errors with number (Correct answer)
- Integer overflow with number
- TypeScript numbers are always safe for currency
Correct answer: Floating-point precision errors with number
JavaScript/TypeScript `number` uses IEEE 754 floating-point, causing precision errors in currency math (e.g., 0.1 + 0.2 !== 0.3).
Question 3: A budget tracker stores `amount: string | number`. Which TypeScript pattern safely extracts only numeric amounts for calculation?
- Type assertion: amount as number
- Type guard: typeof amount === 'number' (Correct answer)
- Non-null assertion: amount!
- Optional chaining: amount?.toFixed()
Correct answer: Type guard: typeof amount === 'number'
A type guard narrows the union type at runtime, ensuring only `number` values enter arithmetic operations.
Question 4: You need a TypeScript function that accepts a budget category and returns spending data. Which return type best models 'data or an error message'?
- string | SpendingData
- Promise<SpendingData>
- SpendingData | null
- Result<SpendingData, string> (Correct answer)
Correct answer: Result<SpendingData, string>
A `Result<T, E>` discriminated union explicitly models success/failure, making error handling type-safe and self-documenting.
Question 5: Which TypeScript feature allows you to enforce that budget category names must be one of a fixed set of string literals?
- Enums or string literal union types (Correct answer)
- readonly string[]
- string & object
- abstract class Category
Correct answer: Enums or string literal union types
Enums and string literal unions (e.g., `'rent' | 'food' | 'utilities'`) restrict values to a predefined set at compile time.
Question 6: A financial dashboard uses `Record<Category, number>` to track spending. What does this type enforce?
- Every Category key must map to a number value (Correct answer)
- Keys are numbers, values are Category strings
- Only one entry per category is allowed
- The object is immutable
Correct answer: Every Category key must map to a number value
`Record<K, V>` constructs an object type with keys of type K and values of type V, ensuring complete coverage of all category keys.
Question 7: You're typing an expense approval workflow in TypeScript. Which pattern best represents an expense that can be 'pending', 'approved', or 'rejected' with different associated data per state?
- Union of interfaces with a discriminant field (Correct answer)
- A class with optional properties for each state
- An enum with numeric values
- A generic type with default parameters
Correct answer: Union of interfaces with a discriminant field
Discriminated unions (tagged unions) use a common literal type field to distinguish states, giving type-safe access to state-specific data.
In TypeScript, you have a `Budget` interface with optional fields.
Which utility type creates a version where ALL fields are required?