Rules Hub
Code Ruleset
← 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 ruleP2stack specificStack: React + TypeScript
refstype-safetyhooks
Always give useRef an explicit type
Specify a ref type when its initializer does not express the intended element or future value domain; use inference when it already does.
PR: hegnar-forum-web · org-mining-3rd-2026-06Created: Jun 18, 2026
Bad example
Old codetsx
| 1 | const eventSourceRef = useRef(); // untyped |
Explanation (EN)
An untyped empty ref does not express the values that its later assignments must support.
Objašnjenje (HR)
Good example
New codetsx
| 1 | const eventSourceRef = useRef<EventSource | null>(null); |
Explanation (EN)
Objašnjenje (HR)
Notes (EN)
For a ref initialized without enough type information, specify its future element/value domain and a valid initial value.
Exceptions / Tradeoffs (EN)
A nonempty initializer may already infer the intended type; do not add redundant generics to every ref.