TypeScript Data Analysis & Reporting 3 — Questions and Answers
Question 1: You need to type a generic sorting function for report rows that works on any object with at least one numeric field. Which TypeScript constraint achieves this?
- <T extends object>
- <T extends Record<string, number>> (Correct answer)
- <T extends { [key: string]: unknown }>
- <T extends { value: number }>
Correct answer: <T extends Record<string, number>>
Record<string, number> constrains T to objects whose values are all numbers, ensuring the sorting function can safely compare any field.
Question 2: In a TypeScript reporting app, what is the purpose of using 'as const' on a tuple of column names?
- It makes the array mutable
- It widens string literals to the string type
- It narrows types to literal values, enabling safer indexed access (Correct answer)
- It converts the tuple to a regular array
Correct answer: It narrows types to literal values, enabling safer indexed access
as const preserves string literal types in tuples, allowing TypeScript to enforce that only valid column names are used as indexes.
Question 3: Which approach correctly types an async function that fetches paginated report data and handles both success and error states?
- Promise<any>
- Promise<ReportPage | null>
- Promise<ReportPage> with try/catch returning typed errors (Correct answer)
- void
Correct answer: Promise<ReportPage> with try/catch returning typed errors
A Promise<ReportPage> return type with try/catch allows callers to handle typed success data while errors are caught and handled explicitly.
Question 4: A data analyst asks you to create a TypeScript type for a metric that can be either a raw number or a formatted string percentage. What is the correct type?
- number & string
- number | string
- MetricValue = { raw: number; formatted: string } (Correct answer)
- any
Correct answer: MetricValue = { raw: number; formatted: string }
A discriminated object type with both raw and formatted fields keeps both representations together and avoids unsafe union coercions.
Question 5: When using Array.prototype.reduce() in TypeScript to build a frequency map from an events array, what is the correct way to type the accumulator?
- Let TypeScript infer it automatically
- Provide an explicit generic type argument: reduce<Record<string, number>>(..., {}) (Correct answer)
- Cast the result with 'as Record<string, number>' after reduce
- Use a plain object with no type annotation
Correct answer: Provide an explicit generic type argument: reduce<Record<string, number>>(..., {})
Providing an explicit generic to reduce tells TypeScript the accumulator's shape from the start, preventing implicit any on the accumulated object.
Question 6: Which TypeScript pattern lets you define a report column configuration that enforces the column key must exist on the row data type T?
- interface Column { key: string }
- interface Column<T> { key: keyof T } (Correct answer)
- type Column = { key: any }
- class Column<T> { key: T }
Correct answer: interface Column<T> { key: keyof T }
Using keyof T as the type of 'key' ensures only valid property names of the row type can be used as column keys.
Question 7: You want to type a function that groups an array of records by a specified field and returns an object where each key maps to an array of matching records. What signature is most accurate?
- function groupBy(data: any[], key: string): object
- function groupBy<T>(data: T[], key: keyof T): Record<string, T[]> (Correct answer)
- function groupBy<T>(data: T[]): Map<string, T>
- function groupBy(data: unknown[]): any
Correct answer: function groupBy<T>(data: T[], key: keyof T): Record<string, T[]>
The generic signature with keyof T enforces that only valid keys of the record type are used, and the return type accurately describes the grouped result.
You need to type a generic sorting function for report rows that works on any object with at least one numeric field.
Which TypeScript constraint achieves this?