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).
Do not trust a declared parameter type when the value comes from an untyped row
A helper that declares a parameter as string or number is documenting an intention, not a guarantee, when its caller pulls the value out of a raw query result or an any-typed transform. Validate the value in the helper, and make the signature honest about what it actually receives.
Bad example
| 1 | const buildUrl = (avatar: string, rank: number) => { |
| 2 | if (avatar !== "") return remote(avatar); // 'undefined' passes |
| 3 | return placeholder(rank < 3 ? rank : "default"); // rank is really '1' |
| 4 | }; |
| 5 | // caller: buildUrl(obj.avatar, obj.rank) where obj is an any-typed raw row |
Explanation (EN)
The literal string 'undefined' that a migration can write passes the emptiness check and yields a broken image instead of the placeholder, and the numeric type is a fiction.
Objašnjenje (HR)
Doslovni string 'undefined' koji migracija moze upisati prolazi provjeru praznine i daje pokvarenu sliku umjesto rezervne, a brojcani tip je izmisljotina.
Good example
| 1 | const isUsableAvatar = (v: unknown): v is string => |
| 2 | typeof v === "string" && v !== "" && v !== "undefined"; |
| 3 |
|
| 4 | const buildUrl = (avatar: unknown, rank: string | number) => { |
| 5 | if (isUsableAvatar(avatar)) return remote(avatar); |
| 6 | return placeholder(Number(rank) <= 3 ? Number(rank) : "default"); |
| 7 | }; |
Explanation (EN)
The signature admits what really arrives and the guard covers the values the data source can actually produce.
Objašnjenje (HR)
Potpis priznaje sto stvarno stize, a zastita pokriva vrijednosti koje izvor podataka doista moze proizvesti.
Notes (EN)
Check the writer, not only the reader: if any code path can write a stringified undefined or a numeric string into the column, the type on the parameter proves nothing.
Bilješke (HR)
Provjeri onoga tko pise, ne samo onoga tko cita: ako bilo koji put koda moze u stupac upisati string 'undefined' ili broj kao string, tip na parametru ne dokazuje nista.