TypeScript Quality Assurance & Improvement 3 — Questions and Answers
Question 1: What is a discriminated union in TypeScript and how does it help QA?
- A union where all members share a common literal property used for type narrowing (Correct answer)
- A union that only includes primitive types
- A union where members cannot overlap
- A union that is automatically resolved by the compiler
Correct answer: A union where all members share a common literal property used for type narrowing
Discriminated unions use a shared literal 'tag' field so TypeScript can narrow the type in switch/if blocks, ensuring all cases are handled.
Question 2: When using Jest with TypeScript, what is the role of `ts-jest` or `babel-jest` with TypeScript preset?
- They replace the TypeScript compiler entirely
- They transform TypeScript files so Jest can execute them without a separate compile step (Correct answer)
- They add type annotations to JavaScript test files
- They generate mock implementations from TypeScript interfaces
Correct answer: They transform TypeScript files so Jest can execute them without a separate compile step
Transformers like `ts-jest` transpile TypeScript source on-the-fly so Jest can run tests without a prior build step.
Question 3: What does the `unknown` type offer over `any` for improving code quality?
- unknown allows all operations without type checks, just like any
- unknown forces you to narrow the type before performing operations on it (Correct answer)
- unknown is only valid as a function return type
- unknown disables null checks for that variable
Correct answer: unknown forces you to narrow the type before performing operations on it
Unlike `any`, `unknown` requires a type guard or assertion before you can access properties or call methods, making unsafe code explicit.
Question 4: Which TypeScript utility type would you use to make all properties of an interface optional for a partial update function?
- Required<T>
- Readonly<T>
- Partial<T> (Correct answer)
- Pick<T, K>
Correct answer: Partial<T>
`Partial<T>` constructs a type with all properties of T set to optional, commonly used for PATCH-style update functions.
Question 5: What is the primary QA benefit of enabling `noUncheckedIndexedAccess` in tsconfig.json?
- It prevents accessing array indices above 100
- It adds `undefined` to the type of indexed array/object access results, forcing null checks (Correct answer)
- It throws a runtime error when accessing out-of-bounds indices
- It disables bracket notation in favor of `.at()` method
Correct answer: It adds `undefined` to the type of indexed array/object access results, forcing null checks
`noUncheckedIndexedAccess` makes TypeScript include `| undefined` in array and index signature types, preventing silent undefined access bugs.
Question 6: What does `tsc --noEmit` accomplish in a CI/CD quality gate?
- Compiles TypeScript and outputs JavaScript files
- Runs type checking without producing any output files, used purely for validation (Correct answer)
- Removes all compiled artifacts from the build directory
- Runs only the tests defined in tsconfig.json
Correct answer: Runs type checking without producing any output files, used purely for validation
`tsc --noEmit` performs a full type-check pass but skips file generation, making it ideal for CI type-checking steps separate from the build.
Question 7: How do TypeScript path mappings (`paths` in tsconfig.json) relate to code quality?
- They replace imports with inline code at compile time
- They allow aliasing long relative import paths, reducing fragile `../../..` chains (Correct answer)
- They enforce that all imports come from node_modules only
- They validate that imported modules have correct types at link time
Correct answer: They allow aliasing long relative import paths, reducing fragile `../../..` chains
Path mappings let you define short aliases like `@utils/*` instead of deep relative paths, making refactoring and imports more maintainable.
What is a discriminated union in TypeScript and how does it help QA?