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).
backend ruleP1universalStack: typescript
readabilitytypesrefactoring
Extract a helper instead of dense inline double-casting
Pull hard-to-read inline logic with multiple casts into a small named helper that documents intent and isolates the unsafe casts.
PR: hegnar-ws · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | return { items, total: typeof (items as any)?._meta?.total === 'object' ? ((items as any)._meta.total as { value: number }).value : ((items as any)?._meta?.total ?? 0) }; |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | return { items, total: extractTotal(items) }; |
| 2 |
|
| 3 | private extractTotal(items: Item[]): number { |
| 4 | const raw: unknown = (items as any)?._meta?.total; |
| 5 | if (typeof raw === 'number') return raw; |
| 6 | if (typeof raw === 'object' && raw !== null && 'value' in raw) return (raw as { value: number }).value; |
| 7 | return 0; |
| 8 | } |
Explanation (EN)
Objašnjenje (HR)