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).
fullstack ruleP1universalStack: TypeScript
correctnessnull-handlingapi-design
When a function signature returns T | null, actually return null for the empty case
If you declared a return type of `T | null` to signal 'nothing here', return null in that case instead of an object full of null/empty fields that the caller must re-check.
PR: hegnar-ws · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | function parseVideo(el: Element): VideoElement | null { |
| 2 | return { mediaId: el.getAttribute('data-media-id') ?? null, player: null }; |
| 3 | // never actually returns null |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | function parseVideo(el: Element): VideoElement | null { |
| 2 | const mediaId = el.getAttribute('data-media-id'); |
| 3 | if (!mediaId) return null; |
| 4 | return { mediaId, player: el.getAttribute('data-player') }; |
| 5 | } |
Explanation (EN)
Objašnjenje (HR)