TypeScript Quality Assurance & Improvement 2 — Questions and Answers
Question 1: Which TypeScript compiler option enforces that all switch statement cases are exhaustively handled for union types?
- --noImplicitAny
- --strictNullChecks
- never type in default branch (Correct answer)
- --noFallthroughCasesInSwitch
Correct answer: never type in default branch
Using the `never` type in the default branch of a switch causes a compile-time error if any union member is unhandled.
Question 2: What is the purpose of `readonly` arrays (`ReadonlyArray<T>`) in TypeScript QA practices?
- They improve runtime performance
- They prevent accidental mutation of array contents (Correct answer)
- They allow arrays to hold only primitive types
- They enable lazy evaluation of array elements
Correct answer: They prevent accidental mutation of array contents
`ReadonlyArray<T>` prevents mutation methods like `push` or `splice` from being called, catching unintended side effects at compile time.
Question 3: Which tool is commonly used to enforce consistent TypeScript code style and catch potential bugs through static analysis?
- Babel
- Prettier
- ESLint with @typescript-eslint plugin (Correct answer)
- Webpack
Correct answer: ESLint with @typescript-eslint plugin
ESLint with the `@typescript-eslint` plugin provides TypeScript-aware linting rules that catch type-related issues and enforce style.
Question 4: What does enabling `strict` mode in tsconfig.json actually do?
- It only enables strictNullChecks
- It enables a single master strict flag that has no sub-options
- It enables a set of strict type-checking flags including strictNullChecks and noImplicitAny (Correct answer)
- It prevents all type assertions
Correct answer: It enables a set of strict type-checking flags including strictNullChecks and noImplicitAny
`strict: true` is shorthand for enabling strictNullChecks, noImplicitAny, strictFunctionTypes, strictBindCallApply, and several other flags simultaneously.
Question 5: In TypeScript testing, what is the benefit of using `as const` assertions on test fixture objects?
- It makes the object mutable at runtime
- It narrows the type to literal types, preventing accidental value changes in fixtures (Correct answer)
- It converts the object to a JSON string automatically
- It disables type checking for that object
Correct answer: It narrows the type to literal types, preventing accidental value changes in fixtures
`as const` makes all properties readonly with literal types, ensuring test fixtures aren't accidentally modified between tests.
Question 6: What TypeScript feature helps catch missing property handling when a new field is added to an interface used across many files?
- Optional chaining
- Structural typing enforcement at compile time (Correct answer)
- The `in` operator
- Type widening
Correct answer: Structural typing enforcement at compile time
TypeScript's structural typing means adding a required property to an interface causes compile errors everywhere that object type is constructed without the new field.
Question 7: Which approach best improves type safety when consuming data from an external REST API in TypeScript?
- Casting the response directly with `as MyType`
- Using `any` for all API responses
- Using a runtime validation library like Zod to parse and type-check responses (Correct answer)
- Declaring the response type as `object`
Correct answer: Using a runtime validation library like Zod to parse and type-check responses
Runtime validators like Zod both validate the shape of external data and infer TypeScript types, bridging the gap between compile-time types and runtime reality.
Which TypeScript compiler option enforces that all switch statement cases are exhaustively handled for union types?