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 ruleP2universalStack: TypeScript
typestype-guardclean-code
Use a type guard instead of any or a cast for runtime discrimination
When you branch on the shape of a value at runtime, write a `data is T` type guard so the type narrows safely, instead of casting or typing as any. If it is broadly useful, lift it to a shared utility.
PR: hegnar-user-ws · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | if ((data as any).status && (data as any).message) { |
| 2 | return data as ErrorResponseDto; |
| 3 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | function isErrorResponseDto(data: unknown): data is ErrorResponseDto { |
| 2 | return !!data && typeof (data as any).status !== 'undefined' && typeof (data as any).message !== 'undefined'; |
| 3 | } |
| 4 | if (isErrorResponseDto(data)) { |
| 5 | return data; |
| 6 | } |
Explanation (EN)
Objašnjenje (HR)