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).
Type side-effect wrapper handlers as returning void
Handlers whose job is to run side effects (setState + optional callback) should be typed to return void, not leak an inner callback's return type.
Bad example
| 1 | // Inferred return type leaks whatever onChange returns, |
| 2 | // implying the value is meaningful when it isn't. |
| 3 | const handleValueChange = (action: SetStateAction<Value>) => { |
| 4 | setValue(action); |
| 5 | return onChange?.(action); |
| 6 | }; |
Explanation (EN)
Returning the optional callback's result gives the handler an accidental, misleading return type. Callers might think the value matters.
Objašnjenje (HR)
Vracanje rezultata opcionalnog callbacka daje handleru slucajan, zavaravajuci povratni tip. Pozivatelji bi mogli misliti da vrijednost nesto znaci.
Good example
| 1 | // Explicitly void: the handler exists to perform effects. |
| 2 | const handleValueChange = (action: SetStateAction<Value>): void => { |
| 3 | setValue(action); |
| 4 | onChange?.(action); |
| 5 | }; |
Explanation (EN)
An explicit void return type documents that the handler is purely for side effects and stops the optional callback's return value from leaking into the signature.
Objašnjenje (HR)
Eksplicitan void povratni tip dokumentira da handler postoji samo za side-efekte i sprjecava da povratna vrijednost opcionalnog callbacka procuri u potpis.
Notes (EN)
Don't write `return callback?.()` in a handler whose contract is side-effectful; call it on its own line so the void type holds.
Bilješke (HR)
Ne pisi `return callback?.()` u handleru ciji je ugovor side-efektni; pozovi ga u zasebnoj liniji da void tip vrijedi.