Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Prefer explicit null over conditional object spreading
Ensure consistent object shapes by assigning `null` to missing properties instead of conditionally omitting the key using spread syntax.
Bad example
| 1 | return items.map((item) => { |
| 2 | // Bad: Variable used for conditional spread |
| 3 | const metadata = item.meta ? format(item.meta) : null; |
| 4 |
|
| 5 | return { |
| 6 | id: item.id, |
| 7 | name: item.name, |
| 8 | // Bad: The 'metadata' key is completely missing if null |
| 9 | ...(metadata ? { metadata } : {}), |
| 10 | }; |
| 11 | }); |
Explanation (EN)
Omitting a schema-required nullable key forces consumers to handle a shape the contract did not promise.
Objašnjenje (HR)
Izostavljanje nullable ključa koji shema zahtijeva prisiljava potrošače na obradu neobećanog oblika objekta.
Good example
| 1 | return items.map((item) => ({ |
| 2 | id: item.id, |
| 3 | name: item.name, |
| 4 | // Good: Key always exists, value is explicitly null if missing |
| 5 | metadata: item.meta ? format(item.meta) : null, |
| 6 | })); |
Explanation (EN)
Assigning `null` explicitly ensures the object always has the same keys (consistent shape). This satisfies strict TypeScript interfaces (e.g., `metadata: Meta | null`) and simplifies frontend logic, as developers can rely on the key being present.
Objašnjenje (HR)
Eksplicitno dodjeljivanje `null` osigurava da objekt uvijek ima iste ključeve (konzistentan oblik). To zadovoljava stroga TypeScript sučelja (npr. `metadata: Meta | null`) i pojednostavljuje frontend logiku, jer se programeri mogu osloniti na prisutnost ključa.
Notes (EN)
Use an explicit nullable property when the API or storage schema requires the key to be present and uses null to represent missing data. Inspect consumers before changing object shape.
Exceptions / Tradeoffs (EN)
Balance against conditional-object-key-prefer-undefined-over-empty-spread: include the key with explicit null when a stable object shape matters to consumers or storage. Balance against prefer-logical-and-for-conditional-object-properties: assign explicit null when consumers rely on a stable object shape with the key always present. Do not introduce null into PATCH or configuration payloads where absence means leave unchanged and null means clear. Performance alone is not proof this shape is required.