TypeScript Quality Assurance & Improvement 4 — Questions and Answers
Question 1: What is a 'type predicate' in TypeScript and what QA problem does it solve?
- A compiler flag that predicates type inference
- A function return type of `param is Type` that narrows the type in calling code (Correct answer)
- An assertion that throws if a type is wrong
- A generic constraint on class constructors
Correct answer: A function return type of `param is Type` that narrows the type in calling code
Type predicates (`x is MyType`) let you write custom type guard functions whose return value narrows the type in the caller's scope.
Question 2: When should you prefer `interface` over `type` alias in TypeScript for improved maintainability?
- When you need to represent a union or intersection
- When you want to use mapped types
- When defining object shapes that may be extended via declaration merging (Correct answer)
- When the type includes function overloads
Correct answer: When defining object shapes that may be extended via declaration merging
Interfaces support declaration merging, which allows separate files or libraries to augment the same interface — useful for extending third-party types.
Question 3: What is the purpose of the `satisfies` operator introduced in TypeScript 4.9?
- It replaces type assertions and removes the original type
- It validates an expression matches a type while preserving the narrowest inferred type (Correct answer)
- It enforces that a class satisfies an interface at runtime
- It removes excess property checks from object literals
Correct answer: It validates an expression matches a type while preserving the narrowest inferred type
`satisfies` checks that a value conforms to a type without widening it, so you get both type safety and access to the literal/inferred type.
Question 4: Which pattern helps prevent 'boolean trap' bugs in TypeScript function signatures?
- Using optional parameters instead of booleans
- Replacing boolean parameters with discriminated union or named option objects (Correct answer)
- Using `never` as the boolean type
- Marking boolean parameters as `readonly`
Correct answer: Replacing boolean parameters with discriminated union or named option objects
Replacing boolean flags with option objects or union types makes call sites self-documenting and prevents passing flags in the wrong order.
Question 5: What does `exactOptionalPropertyTypes` enforce in TypeScript 4.4+?
- Optional properties cannot be `undefined`
- Setting an optional property explicitly to `undefined` is a type error unless `undefined` is in the type (Correct answer)
- Optional properties must have a default value
- All optional properties are also readonly
Correct answer: Setting an optional property explicitly to `undefined` is a type error unless `undefined` is in the type
`exactOptionalPropertyTypes` distinguishes between a property being absent and being explicitly set to `undefined`, catching subtle bugs in object construction.
Question 6: How does enabling `declaration: true` in tsconfig.json assist in quality assurance for library authors?
- It generates runtime type checks alongside the compiled JavaScript
- It emits `.d.ts` files so library consumers get full type information (Correct answer)
- It prevents any `any` types from appearing in output
- It generates unit test stubs for each exported function
Correct answer: It emits `.d.ts` files so library consumers get full type information
Generating `.d.ts` declaration files allows TypeScript consumers of your library to benefit from type checking without access to your source code.
Question 7: What is the risk of using non-null assertion operator (`!`) excessively in TypeScript code?
- It causes runtime type coercion
- It bypasses compile-time null checks, potentially causing runtime null/undefined errors (Correct answer)
- It widens the type to `any`
- It is deprecated and removed in TypeScript 5.0
Correct answer: It bypasses compile-time null checks, potentially causing runtime null/undefined errors
The `!` operator tells TypeScript a value is not null/undefined without verifying it, so misuse reintroduces the null safety bugs TypeScript aims to prevent.
What is a 'type predicate' in TypeScript and what QA problem does it solve?