TypeScript Financial Management & Budgeting 3 — Questions and Answers
Question 1: A TypeScript budget API returns `Promise<Transaction[]>`. When fetching with `async/await`, what is the correct type of the resolved variable?
- Promise<Transaction[]>
- Transaction[] (Correct answer)
- Transaction
- Awaited<Transaction[]>
Correct answer: Transaction[]
Awaiting a `Promise<T>` yields type `T`, so awaiting `Promise<Transaction[]>` gives `Transaction[]`.
Question 2: You want to prevent accidental mutation of a budget plan object in TypeScript. Which approach makes ALL nested properties immutable at the type level?
- Readonly<BudgetPlan>
- const budgetPlan = ...
- Object.freeze(budgetPlan)
- DeepReadonly<BudgetPlan> via a custom recursive type (Correct answer)
Correct answer: DeepReadonly<BudgetPlan> via a custom recursive type
Built-in `Readonly<T>` only makes top-level properties readonly; a recursive `DeepReadonly<T>` type is needed for full deep immutability.
Question 3: Which TypeScript compiler option would catch an uninitialized `totalBudget` variable before it's used in a calculation?
- noImplicitAny
- strictNullChecks
- strictPropertyInitialization (Correct answer)
- noUnusedLocals
Correct answer: strictPropertyInitialization
`strictPropertyInitialization` ensures class properties are assigned in the constructor, catching uninitialized state at compile time.
Question 4: A financial service class has a private `_balance: number`. TypeScript access modifiers are:
- Enforced at runtime by the JavaScript engine
- Only a compile-time concept with no runtime enforcement (Correct answer)
- Converted to WeakMap at compile time
- Enforced by Node.js but not browsers
Correct answer: Only a compile-time concept with no runtime enforcement
TypeScript's `private`, `protected`, and `public` modifiers are erased at compilation; the emitted JavaScript has no access restrictions.
Question 5: You need a TypeScript function that calculates tax differently based on income bracket. Which technique allows different parameter and return type combinations for the same function name?
- Generic constraints
- Function overloads (Correct answer)
- Conditional types
- Declaration merging
Correct answer: Function overloads
Function overloads let you declare multiple signatures for a function, enabling different input/output type pairs for the same function.
Question 6: In a TypeScript expense tracker, `keyof Transaction` produces which type if `Transaction = { id: number; amount: number; category: string }`?
- number | string
- 'id' | 'amount' | 'category' (Correct answer)
- string[]
- { id: number; amount: number; category: string }
Correct answer: 'id' | 'amount' | 'category'
`keyof T` produces a union of the string literal types of all keys in T.
Question 7: A budget app receives JSON from an API. What is the inferred TypeScript type of `JSON.parse(responseText)`?
- unknown
- object
- any (Correct answer)
- Record<string, unknown>
Correct answer: any
`JSON.parse()` returns `any` in TypeScript, so the result must be validated or cast before use in a type-safe way.
A TypeScript budget API returns `Promise`.
When fetching with `async/await`, what is the correct type of the resolved variable?