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 ruleP1stack specificStack: TypeScript
typescripterror-handlingtype-safetycatch
Catch errors as unknown and narrow before using them
Type catch clause errors as unknown (or leave untyped) and narrow with instanceof Error, instead of typing them as any, since anything can be thrown.
PR: hegnar-zephr-components · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codets
| 1 | } catch (error: any) { |
| 2 | return { error: error.message }; |
| 3 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | } catch (error) { |
| 2 | if (!(error instanceof Error)) throw error; |
| 3 | return { error: error.message }; |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)