Discriminated unions used
Why it matters
Optional boolean flags for mutually exclusive states — isLoading, isError, isSuccess on the same object — allow impossible combinations that the type system cannot prevent: { isLoading: true, isSuccess: true } is a valid TypeScript value, and code that doesn't defensively check every combination will produce undefined behavior. ISO 25010:2011 §6.5.4 classifies this as a reliability defect. Discriminated unions encode the constraint that only one state can be active at a time directly in the type, eliminating an entire class of conditional-logic bugs.
Severity rationale
Medium because optional boolean state flags allow impossible combinations that TypeScript cannot detect, producing runtime behavior that depends on which flag the developer happened to check first.
Remediation
Replace multi-flag state types with a discriminated union keyed on a status or type field. TypeScript then exhaustively checks all cases at switch sites:
// Bad: flags allow impossible combinations
type FetchState = {
isLoading?: boolean
isError?: boolean
data?: User
error?: string
}
// Good: only one state possible at a time
type FetchState =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success'; data: User }
| { status: 'error'; error: string }
switch (state.status) {
case 'idle': return <IdleView />
case 'loading': return <Spinner />
case 'success': return <UserView user={state.data} />
case 'error': return <ErrorView message={state.error} />
}
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Code Quality Essentials Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- iso-25010:2011 · maintainability.modifiability — Maintainability — Modifiability
Taxons
History
- 2026-04-18·v1.0.0·Initial import from code-quality-essentials·automated