Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP2universalStack: TypeScript
typescripttype-guarddiscriminated-unionreadability
Use a named type guard for discriminating union responses
When a function returns one of several shapes, narrow with a documented type-guard function rather than scattering inline `'key' in obj` checks at every call site.
PR: hegnar-components · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | const res = await fetchThing(); |
| 2 | if ('articles' in res) { |
| 3 | setItems(res.articles); |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | const isSuccess = (r: ApiResponse): r is ApiSuccess => 'articles' in r; |
| 2 |
|
| 3 | const res = await fetchThing(); |
| 4 | if (isSuccess(res)) { |
| 5 | setItems(res.articles); |
| 6 | } |
Explanation (EN)
Objašnjenje (HR)