Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Annotate callbacks with their function type, not just the param
Use the callback's full function type (e.g. RefCallback<T>) so both the parameter is inferred and the return type is constrained.
Bad example
| 1 | // only the param is typed; return type is unconstrained, |
| 2 | // so accidentally returning a non-cleanup value compiles |
| 3 | const registerRef = useCallback((node: HTMLDivElement | null) => { |
| 4 | if (node) setup(node); |
| 5 | }, []); |
Explanation (EN)
Annotating only the parameter leaves the return type wide open and forces you to spell out the param type by hand.
Objašnjenje (HR)
Tipiziranje samo parametra ostavlja povratni tip potpuno otvorenim i prisiljava te da ručno ispisuješ tip parametra.
Good example
| 1 | // the function-type annotation infers node and restricts the |
| 2 | // return to void | (() => void) |
| 3 | const registerRef = useCallback<React.RefCallback<HTMLDivElement>>((node) => { |
| 4 | setup(node); |
| 5 | }, []); |
Explanation (EN)
Typing the whole callback infers the parameter automatically and enforces a valid ref-callback return, catching mistakes the param-only annotation misses.
Objašnjenje (HR)
Tipiziranje cijelog callbacka automatski zaključuje parametar i nameće valjan povratni tip ref-callbacka, hvatajući greške koje anotacija samo parametra propušta.
Notes (EN)
React's types are globally available, so React.RefCallback<T> (and similar) can be referenced without an explicit import.
Bilješke (HR)
Reactovi tipovi globalno su dostupni, pa se React.RefCallback<T> (i slično) može koristiti bez eksplicitnog importa.