TypeScript Data Analysis & Reporting 4 — Questions and Answers
Question 1: In a TypeScript reporting library, you want a type that represents the possible aggregate functions ('sum', 'avg', 'min', 'max', 'count'). What is the most type-safe definition?
- type AggregateFunc = string
- type AggregateFunc = 'sum' | 'avg' | 'min' | 'max' | 'count' (Correct answer)
- enum AggregateFunc { SUM, AVG }
- const AggregateFuncs = ['sum','avg']
Correct answer: type AggregateFunc = 'sum' | 'avg' | 'min' | 'max' | 'count'
A string literal union type restricts values to exactly the five valid aggregate functions and is exhaustively checkable with switch statements.
Question 2: What TypeScript technique allows you to create a type-safe lookup table for report formatters where each formatter's input type matches the corresponding field type in a data schema?
- Use a plain object with string keys
- Use a mapped type: { [K in keyof Schema]: (val: Schema[K]) => string } (Correct answer)
- Cast each formatter with 'as any'
- Use an array of tuples
Correct answer: Use a mapped type: { [K in keyof Schema]: (val: Schema[K]) => string }
A mapped type over the schema's keys ensures each formatter receives exactly the type of the field it formats, catching type mismatches at compile time.
Question 3: When a TypeScript data processing function might return either a Report object or throw, which pattern best communicates both outcomes to callers without relying on exceptions?
- Return Report | undefined
- Return a Result type: { ok: true; data: Report } | { ok: false; error: string } (Correct answer)
- Return null on failure
- Throw a typed custom error class
Correct answer: Return a Result type: { ok: true; data: Report } | { ok: false; error: string }
A discriminated union Result type forces callers to handle both success and failure paths explicitly without relying on exception control flow.
Question 4: Which TypeScript feature helps ensure that a switch statement over a report metric type handles all possible cases and will cause a compile error if a new metric is added?
- Type assertions in the default case
- Exhaustiveness checking with 'never' in the default case (Correct answer)
- Using 'any' to skip unhandled cases
- Optional chaining
Correct answer: Exhaustiveness checking with 'never' in the default case
Assigning the unhandled value to a variable of type 'never' in the default case causes a compile error if any union member is not handled.
Question 5: A TypeScript function receives an object where values may be numbers or arrays of numbers. How do you safely compute the sum regardless of which form is used?
- Use 'as number' cast on all values
- Check Array.isArray(value) to narrow the type before summing (Correct answer)
- Use 'typeof value === number'
- Use parseFloat on every value
Correct answer: Check Array.isArray(value) to narrow the type before summing
Array.isArray() is a TypeScript type guard that narrows the value to number[] in the true branch and number in the false branch.
Question 6: You are building a generic TypeScript function to compute statistics (min, max, sum) from an array. Which constraint ensures only numeric arrays are accepted?
- <T extends unknown[]>
- <T extends number[]> (Correct answer)
- <T extends Array<any>>
- <T>
Correct answer: <T extends number[]>
Constraining T to number[] ensures arithmetic operations inside the function are valid and the compiler rejects non-numeric arrays.
Question 7: When exporting a TypeScript data analysis module, why should you export types alongside functions instead of just the functions?
- Types are required for JavaScript interop
- Consumers can type their variables and parameters without re-deriving types from return values (Correct answer)
- Exporting types increases bundle size significantly
- TypeScript only works with exported types
Correct answer: Consumers can type their variables and parameters without re-deriving types from return values
Exported types let consumers annotate their own variables and function parameters explicitly, improving IDE support and catching contract violations early.
In a TypeScript reporting library, you want a type that represents the possible aggregate functions ('sum', 'avg', 'min', 'max', 'count').
What is the most type-safe definition?