Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Rely on TypeScript inference for primitive state initialization
Avoid explicit generic type arguments for `useState` when the type can be correctly inferred from the initial primitive value.
Bad example
| 1 | const [count, setCount] = useState<number>(0); |
| 2 | const [isLoading, setIsLoading] = useState<boolean>(true); |
| 3 | const [category, setCategory] = useState<string>('news'); |
Explanation (EN)
A primitive generic repeats inference when all later values fit that same type.
Objašnjenje (HR)
Generički tip primitiva ponavlja inferenciju kada sve kasnije vrijednosti pripadaju istom tipu.
Good example
| 1 | const [count, setCount] = useState(0); |
| 2 | const [isLoading, setIsLoading] = useState(true); |
| 3 | const [category, setCategory] = useState('news'); |
Explanation (EN)
React infers the primitive state type while later setter values remain type checked.
Objašnjenje (HR)
React izvodi tip primitivnog stanja, a kasnije vrijednosti settera i dalje provjerava sustav tipova.
Notes (EN)
For a React useState call initialized with a primitive, compare the inferred type with all later setter values before removing its generic.
Bilješke (HR)
Za useState inicijaliziran primitivom usporedi izvedeni tip sa svim kasnijim vrijednostima settera prije uklanjanja generičkog tipa.
Exceptions / Tradeoffs (EN)
Balance against explicit-function-return-types: rely on inference for local useState and obvious primitives where an explicit annotation adds only noise. Keep a generic for a wider union, branded type, nullable state, empty collection or any deliberately different state domain.