TypeScript Data Analysis & Reporting 2 — Questions and Answers
Question 1: Which TypeScript utility type transforms all properties of an interface to be read-only, useful for immutable report data structures?
- Partial<T>
- Readonly<T> (Correct answer)
- Required<T>
- Mutable<T>
Correct answer: Readonly<T>
Readonly<T> makes all properties of type T read-only, preventing accidental mutation of report data.
Question 2: In TypeScript, what is the best way to type a function that aggregates an array of sales records and returns a summary object with total and average?
- Use 'any' return type for flexibility
- Define an explicit return type interface like SalesSummary (Correct answer)
- Use unknown return type
- Return a tuple instead of an object
Correct answer: Define an explicit return type interface like SalesSummary
Defining an explicit return type interface ensures the aggregation function always returns a predictable, well-typed summary object.
Question 3: When building a TypeScript data pipeline, which pattern allows you to chain multiple transformation steps on a dataset while preserving type safety?
- Using 'as any' between each step
- Method chaining with generic functions that preserve input/output types (Correct answer)
- Casting to unknown between transformations
- Using JavaScript's loose equality for comparisons
Correct answer: Method chaining with generic functions that preserve input/output types
Generic functions that accept and return typed data allow method chaining while TypeScript infers the correct type at each step.
Question 4: What TypeScript feature would you use to create a type that maps each key of a report schema to a formatter function for that field's value?
- Conditional types
- Mapped types (Correct answer)
- Template literal types
- Intersection types
Correct answer: Mapped types
Mapped types iterate over an existing type's keys and assign a new type to each, perfect for creating per-field formatter maps.
Question 5: A TypeScript function receives raw CSV data as a string and must return typed row objects. Which approach is most type-safe?
- Parse and cast with 'as unknown as T'
- Return Record<string, string> and cast later
- Use a generic parser with a validation schema that returns T | null (Correct answer)
- Use eval() for dynamic property access
Correct answer: Use a generic parser with a validation schema that returns T | null
A generic parser with runtime validation returns T when parsing succeeds or null on failure, maintaining type safety through the boundary.
Question 6: When typing a pivot table operation in TypeScript, which structure best represents a result where row keys and column keys are both dynamic?
- Map<string, Map<string, number>> (Correct answer)
- Array<Array<number>>
- Record<string, number>
- Set<string>
Correct answer: Map<string, Map<string, number>>
Map<string, Map<string, number>> naturally models a two-dimensional pivot where both axes have string keys and values are numeric.
Question 7: Which TypeScript compiler option should be enabled to catch implicit 'any' types that can silently corrupt data analysis results?
- strict: false
- noImplicitAny: true (Correct answer)
- skipLibCheck: true
- allowJs: true
Correct answer: noImplicitAny: true
noImplicitAny forces every variable and parameter to have an explicit or inferred type, preventing silent 'any' from hiding type errors in data pipelines.
Which TypeScript utility type transforms all properties of an interface to be read-only, useful for immutable report data structures?