Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Add object keys conditionally with undefined or a single conditional spread, not `: {}`
To include a key only sometimes, use a ternary returning undefined or a single conditional spread, rather than spreading an empty object in the else branch.
Bad example
| 1 | const config = { |
| 2 | mode: 'production', |
| 3 | ...(isBuild ? { base: `${BASE_URL}/` } : {}), |
| 4 | }; |
Explanation (EN)
An empty fallback spread can be simplified, but choosing a different object shape requires a verified consumer contract.
Objašnjenje (HR)
Prazni fallback spread može se pojednostaviti, ali promjena oblika objekta zahtijeva provjeren ugovor potrošača.
Good example
| 1 | const config = { |
| 2 | mode: 'production', |
| 3 | base: isBuild ? `${BASE_URL}/` : undefined, |
| 4 | }; |
| 5 | // or, for a single conditional key: |
| 6 | const config2 = { |
| 7 | mode: 'production', |
| 8 | ...(isBuild && { base: `${BASE_URL}/` }), |
| 9 | }; |
Explanation (EN)
Use the explicit undefined form only when consumers treat it like omission; conditional spread preserves absence.
Objašnjenje (HR)
Izričiti undefined koristi samo ako ga potrošači tretiraju kao odsutnost. Uvjetni spread čuva stvarnu odsutnost ključa.
Notes (EN)
Before simplifying a conditional object spread, inspect serialization, validators, own-key checks and object merging to establish whether an absent key and an undefined value are equivalent.
Bilješke (HR)
Prije pojednostavljivanja uvjetnog spreada provjeri serijalizaciju, validatore, postojanje vlastitih ključeva i spajanje objekata kako bi utvrdio jesu li odsutan ključ i undefined doista ekvivalentni.
Exceptions / Tradeoffs (EN)
Balance against prefer-explicit-null-over-conditional-spreads: omit a key (undefined/conditional spread) when consumers and serialization treat 'absent' as correct. JSON omission does not establish equivalence for Object.keys, in, hasOwnProperty, defaults or custom serializers. Prefer conditional spread when presence matters.