Lesson 5 of 9
Lesson 05 — Advanced types QA relies on
Title: Enums, void, unknown, never — and why any is risky
Description: Pick the right tool when modeling statuses, logging helpers, uncertain JSON, or impossible branches TypeScript must narrow away.
Why it matters for QA: Reports (Passed, Skipped), flaky classifications, API probes — expressive types communicate intent inside suites.
Type cheat sheet
| Type keyword | Meaning | QA usage |
|---|---|---|
enum | Named numeric/string members | Roles, pipeline statuses |
void | No meaningful return | Utility logging wrappers |
any | Turns checker off | Avoid except migration spikes |
unknown | Safe “I will narrow this” | Raw API bodies before guards |
never | No reachable value | Exhaustive switch helpers |
1. enum for fixed sets
enum TestStatus {
Passed,
Failed,
Skipped,
}
enum Role {
Admin,
User,
Guest,
}
const lastRun: TestStatus = TestStatus.Passed;
String enums are common in web services — learn both styles as you read existing codebases.
2. void for side-effect utilities
function logStep(message: string): void {
console.log(`[STEP] ${message}`);
}
3. Avoid any
// Discouraged — everything compiles, nothing is trustworthy.
let data: any = "hello";
data = 123;
During greenfield QA work prefer unknown + guards or explicit DTO interfaces.
4. unknown stays safe until narrowed
let response: unknown = "from network";
if (typeof response === "string") {
console.log(response.toUpperCase());
}
5. never for deliberate throws
function throwError(message: string): never {
throw new Error(`${JSON.stringify(message)}`);
}
6. Practice
- Replace any with unknown
Instead of using any for a fixture parser, use unknown. Then:
- check the type using typeof
- check arrays using Array.isArray()
👉 This makes the code safer, because you must validate data before using it.